#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=200005;
stack<int> stk;
int a[N];
int maxn=-1e18,n,i=1;
signed main(){
cin>>n;
int n1=n;
while(n1--){
maxn=-1e18;
int op;
cin>>op;
if(op==0){
int x;
cin>>x;
maxn=max(maxn,x);
stk.push(i);
a[i]=x;
i++;
}
if(op==1){
if(stk.size()>0){
stk.pop();
a[i]=0;
}
}
if(op==2){
if(stk.size()==0) cout<<0<<endl;
else{
for(int j=1;j<=i;j++){
maxn=max(maxn,a[j]);
}
cout<<maxn<<endl;
}
}
}
return 0;
}