#include<bits/stdc++.h>
using namespace std;
long long n,maxx[200005],cnt;
int main(){
cin>>n;
while(n--){
int opt;
cin>>opt;
if(opt==0){
long long x;
cin>>x;
maxx[++cnt]=max(maxx[cnt-1],x);
}
if(opt==1&&cnt>0){
cnt--;
}
if(opt==2){
if(cnt==0){
cout<<"0"<<endl;
}
else cout<<maxx[cnt]<<"\n";
}
}
return 0;
}