#include <fstream>
#define MAX (int)(2e5+5)
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n,Heap[MAX*4],A[MAX],PozInHeap[MAX],heapsize,nr;
///A - numerele adaugate in heap in ordine cronologica
///Heap[i] - indicele in A[] a unui numarul de pe un nod
///PozInHeap[i] - pozitia in care se afla un numar in heap dupa indicele sau in A
void Push(int);
void Pop(int);
int main(){
int i,task,x,y;
fin>>n;
while(n--){
fin>>task;
if(task<3)
fin>>x;
switch(task){
case 1:
Push(x);
break;
case 2:
Pop(x);
break;
case 3:
fout<<A[Heap[1]]<<'\n';//afisez varful
break;
}
}
return 0;
}
void Pop(int x){
int poz=PozInHeap[x],y;
while(Heap[poz*2]>0||Heap[poz*2+1]>0){
if(Heap[poz*2]>0)
y=poz*2;
if(Heap[poz*2+1]>0&&A[Heap[poz*2+1]]<A[Heap[poz*2]])
y=poz*2+1;
else if(Heap[poz*2]<=0){
y=poz*2+1;
}
swap(PozInHeap[Heap[poz]],PozInHeap[Heap[y]]);
swap(Heap[poz],Heap[y]);
poz=y;
}
Heap[PozInHeap[x]]=-1;
}
void Push(int x){
A[++nr]=x;
Heap[++heapsize]=nr;
PozInHeap[nr]=heapsize;
x=PozInHeap[nr];
while(x/2&&(A[Heap[x]]<A[Heap[x/2]]||Heap[x/2]==-1)){
if(Heap[x/2]==-1){
PozInHeap[Heap[x]]=x/2;
swap(Heap[x],Heap[x/2]);
x/=2;
continue;
}
swap(PozInHeap[Heap[x]],PozInHeap[Heap[x/2]]);
swap(Heap[x],Heap[x/2]);
x/=2;
}
}