#include<bits/stdc++.h>
using namespace std;
stack<int> a;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
while(n--){
int x;
cin>>x;
if(x==0){
int y;
cin>>y;
a.push(y);
}
else if(x==1){
a.pop();
}
else{
int maxx=0;
stack<int> temp;
for(int i=1;i<=a.size();i++){
if(a.top()>maxx) maxx=a.top();
temp.push(a.top());
a.pop();
}
for(int i=1;i<=temp.size();i++)
a.push(temp.top()),temp.pop();
cout<<maxx<<endl;
}
}
return 0;
}