#include <fstream>
#include <iostream>
#include <vector>
#include <cassert>
#include <cstring>
#include <set>
#include <unordered_map>
#include <memory>
#include <deque>
using namespace std;
void pushUp(int p, int heap[], int pos[], int val[]) {
for (;p > 0 && val[heap[p >> 1]] > val[heap[p]]; p >>= 1) {
swap(heap[p >> 1], heap[p]);
swap(pos[heap[p >> 1]], pos[heap[p]]);
}
}
void pushDown(const int heapSize, int heap[], int pos[], int val[]) {
int p = 0;
while (p < heapSize) {
int left = (p << 1);
int right = left | 1;
if (left >= heapSize)
return;
if (val[heap[left]] < val[heap[p]] &&
(right >= heapSize || val[heap[left]] <= val[heap[right]])) {
swap(heap[p], heap[left]);
swap(pos[heap[p]], pos[heap[left]]);
p = left;
continue;
}
if (right >= heapSize || val[heap[right]] >= val[heap[p]])
return;
swap(heap[p], heap[right]);
swap(pos[heap[p]], pos[heap[right]]);
p = right;
}
}
void debug(ofstream& g, int l, int op, int x, int heapSize, const int heap[], const int pos[], const int val[]) {
return;
g << "\n-----------\nDEBUG\n---------\n";
g << "op, x: " << op << ' ' << x << '\n';
g << "l, heapSize: " << l << ' ' << heapSize << '\n';
for (int i = 0; i < heapSize; i++)
g << "heap[" << i << "]=" << heap[i] << ' ';
g << '\n';
for (int i = 0; i < l; i++)
g << "val[" << i << "]=" << val[i] << ' ';
g << '\n';
for (int i = 0; i < l; i++)
g << "pos[" << i << "]=" << pos[i] << ' ';
g << "\n------\n";
}
int main() {
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int N;
f >> N;
int heap[N];
int pos[N];
int val[N];
int heapSize = 0;
int addCount = 0;
for (int i = 0; i < N; i++) {
int op;
f >> op;
if (op == 3) {
assert(heapSize > 0);
g << val[heap[0]] << '\n';
continue;
}
int x;
f >> x;
if (op == 1) {
int p = heapSize;
pos[addCount] = p;
heap[p] = addCount;
val[addCount] = x;
heapSize++;
addCount++;
pushUp(p, heap, pos, val);
debug(g, addCount, op, x, heapSize, heap, pos, val);
} else {
assert(heapSize > 0);
int p = pos[x - 1];
val[heap[p]] = -1;
pushUp(p, heap, pos, val);
debug(g, addCount, op, x, heapSize, heap, pos, val);
p = heapSize - 1;
swap(heap[0], heap[p]);
swap(pos[heap[0]], pos[heap[p]]);
heapSize--;
pushDown(heapSize, heap, pos, val);
debug(g, addCount, op, x, heapSize, heap, pos, val);
}
}
f.close();
g.close();
return 0;
}