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

q.times do
  com = gets.split.map(&:to_i)
  case com[0]
  when 0
    if idx = a[com[1]].bsearch_index{|n| n > com[2]}
      a[com[1]].insert(idx, com[2])
    else
      a[com[1]]<<com[2]
    end
  when 1
    puts a[com[1]][-1] if a[com[1]][0]
  else
    a[com[1]].pop if a[com[1]][0]
  end
end
