#include<bits/stdc++.h>
using namespace std;
int a[1000001];
int tot;
void work(int x){
while(a[x]<a[x/2]&&x/2>=1){
swap(a[x],a[x/2]);
x/=2;
}
}
void work1(int x){
while(a[x]<min(a[x*2],a[x*2+1])&&x*2<=tot){
if(a[x*2]<a[x*2+1]){
swap(a[x*2],a[x]);
x=x*2;
}
else{
if(x*2+1<=tot){
swap(a[x],a[x*2+1]);
x=x*2+1;
}
}
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int op;
scanf("%d",&op);
if(op==1){
int x;
scanf("%d",&x);
a[++tot]=x;
work(tot);
}
if(op==2){
printf("%d\n",a[1]);
}
else if(op==3){
swap(a[1],a[tot]);
a[tot]=999999999;
tot--;
work1(1);
}
}
return 0;
}