nq = gets.split.map(&:to_i)
ary = Array.new(nq[0]){[]}

nq[1].times do
  odr = gets.split.map(&:to_i)
  case odr[0]
  when 0
    ary[odr[1]] << odr[2]
  when 1
    unless (ary[odr[1]]).empty?
      puts(ary[odr[1]].max)
    end
  when 2
    unless (ary[odr[1]]).empty?
      ary[odr[1]].delete_at(ary[odr[1]].index(ary[odr[1]].max))
    end
  end
end
