#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,max1=-1;
cin>>n;
stack<int>st1,st2;
st1.push(0);
for(int i=0; i<=n-1; i++)
{
long long x;
cin>>x;
if(x==0)
{
long long y;
cin>>y;
max1=max(max1,y);
st2.push(y);
if(max1>st1.top())st1.push(max1);
}
else if(x==1)
{
if(!st2.empty()&&!st1.empty())
{
if(st2.top()==st1.top())
{
st2.pop();
st1.pop();
}
else st2.pop();
}
}
else if(x==2)
{
if(!st1.empty())cout<<st1.top()<<endl;
else cout<<'0'<<endl;
}
}
return 0;
}