#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#define N 200001
using namespace std;
int i, n, c, x, nr, l, H[N], A[N], P[N];
void sift(int k)
{
int s = 1;
while (s)
{
s = 0;
if(2 * k <= nr)
{
s = 2 * k;
if(s + 1 <= nr && H[s + 1] < H[s])
s++;
if(H[s] >= H[k])
s = 0;
}
if(s)
{
swap(H[k], H[s]);
swap(A[k], A[s]);
P[A[k]] = k;
P[A[s]] = s;
k = s;
}
}
}
void perc(int k)
{
while(k > 1 && H[k] < H[k/2])
{
swap(H[k], H[k / 2]);
swap(A[k], A[k / 2]);
P[A[k]] = k;
P[A[k / 2]] = k /2;
k /= 2;
}
if(c == 1)
{
P[++l] = k;
A[k] = l;
}
}
void sterg(int k)
{
H[k] = H[nr--];
if(k > 1 && H[k] < H[k / 2])
perc(k);
else sift(k);
}
int main()
{
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
scanf("%d", &c);
if(c == 3) printf("%d\n", H[1]);
else
{
scanf("%d", &x);
if(c == 1)
{
H[++nr]= x;
perc(nr);
}
else
sterg(P[x]);
}
}
return 0;
}