n, q = gets.chomp.split.map(&:to_i)
A = []
n.times{ 
    A.push([])
}

q.times{
    choice, t, x = gets.chomp.split(" ").map(&:to_i)
    case choice
    when 0
        A[t].push(x)
    when 1
        puts A[t][0] unless A[t][0].nil?
    when 2
        A[t].shift
    end
    }
