#include<bits/stdc++.h>
using namespace std;
ifstream F("heapuri.in");
ofstream G("heapuri.out");
#define N 200010
int n,l,r,a[N],h[N],p[N],i,x,c;
void A(int x)
{
for(;x>1&&a[h[x]]<a[h[x>>1]];swap(h[x],h[x>>1]),p[h[x]]=x,p[h[x>>1]]=x>>1,x>>=1);
}
void B(int x)
{
for(int y=0;x!=y;swap(h[x],h[y]),p[h[x]]=x,p[h[y]]=y) {
y=x;
if(y*2<=l&&a[h[x]]>a[h[y*2]])
x=y*2;
if(y*2<l&&a[h[x]]>a[h[y*2+1]])
x=y*2+1;
}
}
int main()
{
for(F>>n;n;--n) {
F>>c;
if(c==1)
F>>x,a[++r]=x,h[++l]=r,p[r]=l,A(l);
else if(c==2) {
F>>x,a[x]=-1,A(p[x]),p[h[1]]=0,h[1]=h[l--],p[h[1]]=1;
if(l>1)
B(1);
} else
G<<a[h[1]]<<'\n';
}
return 0;
}