#include<bits/stdc++.h>
using namespace std;
int n,vis=0;
struct f{
int a,b,c;
}a[100005];
int heap[100005],head[100005];
void up(int pos){
while(pos!=1&&heap[pos]<heap[pos>>1]){
swap(heap[pos],heap[pos>>1]);pos>>=1;
}
}
void down(int pos){
while((pos<<1)<=vis){
int ch=pos<<1;
if(ch+1<=vis && heap[ch+1]<heap[ch])
ch++;
if(heap[ch]<heap[pos])
swap(heap[ch],heap[pos]);
else break;
pos=ch;
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].a;
a[i].b=i;
if(a[i].a==1)
cin>>a[i].c;
}
for(int i=1;i<=n;i++){
if(a[i].a==1){
vis++;
heap[vis]=a[i].c;
up(vis);
}
else if(a[i].a==2){
cout<<heap[1]<<endl;
}
else if(a[i].a==3){
swap(heap[vis],heap[1]);vis--;
down(1);
}
}
return 0;
}