#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int main(){
long long N,max;
long long x,y;
stack<long long>SOS={};
stack<long long>BOB={};
stack<long long>C={};
cout<<"Please input the value of 'N'"<<endl;
cin>>N;
for(long long i=0;i<N;i++){
cin>>x;
if(x==0){
cin>>y;
SOS.push(y);
}
if(x==1){
if(!SOS.empty()){
SOS.pop();
}
}
if(x==2){
max=0;
while(!BOB.empty()){BOB.pop();}
while(!SOS.empty()){
BOB.push(SOS.top());
SOS.pop();
if(BOB.top()>max){max=BOB.top();}
}
C.push(max);
while(!BOB.empty()){
SOS.push(BOB.top());
BOB.pop();
}
}
}
while(!BOB.empty()){BOB.pop();}
while(!C.empty()){
BOB.push(C.top());
C.pop();
}
while(!BOB.empty()){cout<<BOB.top()<<endl;BOB.pop();}
return 0;
}