#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
int main()
{
long long a[10005],n,max=0,x,tot=0;
stack<int>s;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>x;
if(x==0)
{
cin>>x;
s.push(x);
if(s.top()>max)
{
max=x;
}
a[i]=x;
tot++;
}
else if(x==1)
{
s.pop();
a[tot]=0;
tot--;
max=0;
if(s.empty())
{
max=0;
}
else
{
for(int j=1;j<=tot;j++)
{
if(a[j]>max)
{
max=a[j];
}
}
}
}
else if(x==2)
{
cout<<max<<endl;
}
}
return 0;
}