#include<bits/stdc++.h>
using namespace std;
const int maxn=1e7;
long long heap[maxn],heap_size=0,h[maxn],ans=1;
void put(int a){
int now,next;
heap[++heap_size]=a;
now=heap_size;
while(now>1){
next=now/2;
if(heap[now]>=heap[next])break;
swap(heap[now],heap[next]);
now=next;
}
}
int pop(){
int now=1,next,res=heap[1];
heap[1]=heap[heap_size--];
while(now*2<=heap_size){
now=now*2;
if(next<heap_size&&heap[next+1]<heap[next])next++;
if(heap[now]<=heap[next])break;
swap(heap[now],heap[next]);
now=next;
}
return res;
}
int main(){
int n;
scanf("%d",&n);
int o,x;
for(int i=1;i<=n;i++){
scanf("%d",&o);
if(o==1){
scanf("%d",&x);
put(x);
}
if(o==3){
pop();
}
if(o==2){
h[ans++]=heap[1];
}
}
for(int i=1;i<ans;i++){
printf("%d\n",h[i]);
}
return 0;
}