
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
        insert_at = A[t].bsearch_index{|val| val >= x}
        if insert_at.nil?
            A[t].push(x)
        else
            A[t].insert(insert_at, x) 
        end
    when 1
        puts A[t][-1] unless A[t].empty?
    when 2
        A[t].pop unless A[t].empty?
    end
    }


