#include<iostream>
#include <fstream>
using namespace std;
const int N=1000010;
int h[N],size;
void down(int t){
int u=t;
if(2*t<=size&&h[u]>h[2*t]){
u=2*t;
}
if(2*t+1<=size&&h[u]>h[2*t+1]){
u=2*t+1;
}
if(t!=u){
swap(h[t],h[u]);
down(t);
}
}
void up(int x){
while(x/2&&h[x/2]>h[x]){
swap(h[x/2],h[x]);
x/=2;
}
}
int main(){
int n;
cin>>n;
while(n--){
int op;
cin>>op;
if(op==1){
int x;
cin>>x;
if(!size){
h[++size]=x;
}else{
h[++size]=x;
up(size);
}
}
if(op==2){
cout<<h[1]<<endl;
}
if(op==3){
h[1]=h[size];
size--;
down(1);
}
}
return 0;
}