#include <fstream>
#include <unordered_map>
#include <vector>
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
int n,i,op,x,nr;
struct nod
{
int val;
int ord;
};
vector<nod>v;
unordered_map<int,int>poz;
void swp(int a,int b)
{
swap(poz[v[a].ord],poz[v[b].ord]);
swap(v[a],v[b]);
}
void up(int a)
{
while(v[a].val<v[a/2].val && a>1)
{
swp(a,a/2);
a/=2;
}
}
void down(int a)
{
while(true)
{
int st=2*a,dr=2*a+1;
if(dr<=v.size()-1)
{
if(v[st].val<v[dr].val && v[a].val>v[st].val)
{
swp(a,st);
a=st;
}
else if(v[st].val>=v[dr].val && v[a].val>v[dr].val)
{
swp(a,dr);
a=dr;
}
else
break;
}
if(st<=v.size()-1 && v[a].val>v[st].val)
{
swp(a,st);
a=st;
}
else
break;
}
}
void inserare(int x)
{
nr++;
v.push_back({x,nr});
poz[nr]=v.size()-1;
up(poz[nr]);
}
void sterg(int x)
{
int last=v.size()-1,p=poz[x];
if(p==last)
{
v.pop_back();
return;
}
swp(p,last);
v.pop_back();
up(p);
down(p);
}
int main()
{
v.push_back({0,0});
cin>>n;
for(i=1;i<=n;i++)
{
cin>>op;
if(op==1)
{
cin>>x;
inserare(x);
}
if(op==2)
{
cin>>x;
sterg(x);
}
if(op==3)
cout<<v[1].val<<'\n';
}
return 0;
}