#include<bits/stdc++.h>
using namespace std;
int n;
int flag,x,top;
int f[200005];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>flag;
if(flag==0)
{
cin>>x;
f[++top]=max(f[top-1],x);
}
else if(flag==1&&top!=0)
{
top--;
}
else
{
cout<<f[top]<<endl;
}
}
return 0;
}