#include<bits/stdc++.h>
using namespace std;
int main()
{
set<int,greater<int> > s;
stack<int> st;
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
if(x==0)
{
int y;
cin>>y;
st.push(y);
s.insert(y);
}
else if(x==1)
{
if(st.size()!=0)
{
int y=st.top();
s.erase(y);
st.pop();
}
}
else
{
if(!s.empty()) cout<<*s.begin()<<endl;
else cout<<0;
}
}
return 0;
}