#include<bits/stdc++.h>
using namespace std;
int maxx[100000005],sum[10000005];
int main(){
int n,tmp,i=0;cin>>n;stack<int> s;
while(n--){
cin>>tmp;
if(tmp==0){
cin>>maxx[i];
s.push(maxx[i]);
sum[maxx[i]]++;
i++;
}
if(tmp==1){
if(s.size()!=0){
sum[s.top()]--;
if(sum[s.top()]==0){
for(int j=0;j<=i;j++){
if(maxx[i]==s.top()){
maxx[i]=0;
break;
}
}
}
i--;sort(maxx,maxx+i);
s.pop();
}
}
if(tmp==2){
if(s.size()!=0){
sort(maxx,maxx+i);
cout<<maxx[i-1]<<"\n";
}else{
puts("0");
}
}
}
return 0;
}