#include <fstream>
#include <queue>
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
const int mxN = 2e5 + 1;
struct nod{
int v, ind;
};
bool operator<(nod a, nod b){
return a.v > b.v;
}
priority_queue<nod> Q;
bool del[mxN];
int main(){
int n, q, v, ind = 0, aux;
fin >> n;
for(int i = 1 ; i <= n; i++){
fin >> q;
if(q == 3)
fout << Q.top().v << "\n";
else{
if(q == 1){
fin >> aux;
Q.push({aux, ++ind});
}else{
fin >> v;
del[v] = true;
while(del[Q.top().ind])
Q.pop();
}
}
}
}