#include<bits/stdc++.h>
#include<stack>
int i,n,op,ans,x;
using namespace std;
int main(){
stack<int> m;
stack<int> b;
cin>>n;
for(i=1;i<=n;i++){
cin>>op;
if(op==0){
cin>>x;
m.push(x);
if(b.empty()||x>b.top())
b.push(x);
else b.push(b.top());
}
else if(op==1){
m.pop();
b.pop();
}
else{
if(b.empty())cout<<0;
else cout<<b.top()<<endl;
}
}
return 0;
}