n, q = gets.chomp.split.map(&:to_i)
stack = Array.new(n){[]}
q.times{
    query = gets.chomp.split.map(&:to_i)
    case query[0]
    when 0
        stack[query[1]].push query[2]
    when 1
        puts stack[query[1]].last unless stack[query[1]].empty?
    when 2
        stack[query[1]].pop
    end
}

