#include<bits/stdc++.h>
using namespace std;
stack<int>p1;
stack<int>p2;
int main()
{
int n,x,y,max=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(x==0){
scanf("%d",&y);
p1.push(y);
if(y>=max){
max=y;
p2.push(max);
}
}
else if(x==1){
if(p1.top()==max){
p2.pop();
if(!p2.empty())
max=p2.top();
}
p1.pop();
}
else{
if(p2.empty()){
max=0;
printf("0");
}
else
printf("%d\n",max);
}
}
return 0;
}