n, t = gets.chomp.split.map(&:to_i)
queue = Array.new(n){[]}
t.times{
    query = gets.chomp.split.map(&:to_i)
    case query[0]
    when 0
        i, x = query[1..2]
        queue[i].push x
    when 1
        i = query[1]
        puts queue[i].first unless queue[i].empty?
    when 2
        i = query[1]
        queue[i].shift
    end
}
