import java.util.*

fun main(args:Array<String>):Unit {
    val (n, q) = readLine()!!.split(' ').map(String::toInt)
    val stacks = Array(n){LinkedList<Int>() as Deque<Int>}
    repeat(q){
        val query = readLine()!!.split(' ').map(String::toInt)
        when(query.first()){
            0 -> stacks[query[1]].addLast(query.last())
            1 -> if (stacks[query.last()].isNotEmpty()) println(stacks[query.last()].first)
            else -> if (stacks[query.last()].isNotEmpty()) stacks[query.last()].removeFirst()
        }
    }
}

