#include<bits/stdc++.h>
#define DAD(x) ((x)/2)
#define LE(x) ((x)*2)
#define RT(x) ((x)*2+1)
using namespace std;
int n;
int w[100005];
int tot;
int top(){
return w[1];
}
void modify(int x){
if(x==1||w[x]>w[DAD(x)]){
return;
}
swap(w[x],w[DAD(x)]);
modify(DAD(x));
}
void push(int x){
w[++tot]=x;
modify(tot);
}
void repair(int x){
if(LE(x)>tot){
return;
}
int tar=LE(x);
if(RT(x)<=tot){
tar=w[LE(x)]<w[RT(x)]?LE(x):RT(x);
}
if(w[x]>w[tar]){
swap(w[x],w[tar]);
repair(tar);
}
}
void pop(){
swap(w[1],w[tot--]);
repair(1);
}
int main(){
scanf("%d",&n);
while(n--){
int op,m;
scanf("%d",&op);
if(op==1){
scanf("%d",&m);
push(m);
}
else if(op==2){
printf("%d\n",top());
}
else if(op==3){
if(tot!=0){
pop();
}
}
}
return 0;
}