#include<bits/stdc++.h>
using namespace std;
long long op,n,x,maxx;
stack<int>s;
int main(){
cin>>n;
while (n--){
cin>>op;
if (op==0){
cin>>x;
if (s.size()==0) s.push(x);
else if (x>s.top()) s.push(x);
}
if (op==1){
if (s.top()==x && x!=0) s.pop();
if (s.top()==x && x==0) cout<<"0\n";
}
if (op==2) cout<<s.top()<<'\n';
}
}