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

q.times do
  com = gets.split.map(&:to_i)
  if com[0] == 0
    a[com[1]].push com[2]
  elsif com[0] == 1
    puts a[com[1]][0] if !a[com[1]].empty?
  else
    a[com[1]].shift if !a[com[1]].empty?
  end
end
