#include <fstream>
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
int heap[200001], pos[200001], A[200001];
int N, a, b, n = 0, element = 0;
void push(int x)
{
int aux;
while(x / 2 && A[heap[x]] < A[heap[x / 2]])
{
aux = heap[x];
heap[x] = heap[x / 2];
heap[x / 2] = aux;
pos[heap[x]] = x;
pos[heap[x / 2]] = x/2;
x/=2;
}
}
void pop(int x)
{
int aux = x, schimbare = 0;
if(x * 2 <= n && A[heap[x]] > A[heap[x * 2]]) aux = x * 2, schimbare = 1;
if(x * 2 + 1 <= n && A[heap[aux]] > A[heap[x * 2 + 1]]) aux = x * 2 + 1, schimbare = 1;
int bla;
if(schimbare)
{
bla = heap[x];
heap[x] = heap[aux];
heap[aux] = bla;
pos[heap[x]] = x;
pos[heap[aux]] = aux;
pop(aux);
}
}
int main()
{
cin >> N;
for(int i = 1; i <= N; i++)
{
cin >> a;
if(a < 3) cin >> b;
if(a == 1)
{
A[++element] = b;
heap[++n] = element;
pos[element] = n;
push(n);
}
else if(a == 2)
{
A[b] = -1;
push(pos[b]);
heap[1] = heap[n--];
pos[heap[1]] = 1;
pop(1);
}
else
{
cout << A[heap[1]] << '\n';
}
}
}