#include <bits/stdc++.h>
using namespace std;
stack<int>stk;//定义栈
int main(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
int op;
cin >> op;
if(op == 1) {
int x;
cin>>x;
stk.push(x); //将 x 入栈
}
else if(op == 2) {
if(!stk.empty()) stk.pop();//将 栈顶元素 出栈
else cout<<-1<<endl;
}
else if(op == 3) {
if(!stk.empty()) cout<<stk.top()<<endl;//将 栈顶元素输出
else cout<<-1<<endl;
}
}
return 0;
}