// https://www.infoarena.ro/problema/heapuri
#pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC optimize("O3,unroll-loops")
#pragma GCC target( \
"avx512f,avx512bw,avx512dq,avx512cd,avx512vl,bmi,bmi2,lzcnt,popcnt,fma")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt,fma")
// #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using int64 = int64_t;
using uint64 = uint64_t;
using int32 = int32_t;
using uint32 = uint32_t;
using int16 = int16_t;
using uint16 = uint16_t;
using pii = pair<int, int>;
using pll = pair<int64, int64>;
#define all(x) (x).begin(), (x).end()
#define allg(x) (x).begin(), (x).end(), greater<int>()
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rfor(i, st, dr) for (auto i = (st); i <= (dr); ++i)
#define for0(i, n) for (auto i = 0; i < (n); ++i)
#define rfor0(i, n) for (auto i = (n) - 1; i >= 0; --i)
#define for1(i, n) for (auto i = 1; i <= (n); ++i)
#define rfor1(i, n) for (auto i = (n); i >= 1; --i)
#define foreach(x, a) for (auto &x : a)
#define cforeach(x, a) for (const auto &x : a)
#define ft first
#define sd second
#define cendl cout << "\n"
#define fendl fout << "\n"
#define FASTIO \
ios::sync_with_stdio(false); \
cin.tie(nullptr);
// #define int int64
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
// FILE* fin = fopen("", "r");
// FILE* fout = fopen("", "w");
const int NRMAX = 200000;
template <typename T> class FastIO {
private:
ifstream &fin;
ofstream &fout;
static constexpr int IN_BUF_SIZE = 262144;
static constexpr int OUT_BUF_SIZE = 262144;
char inBuf[IN_BUF_SIZE];
int inPos = IN_BUF_SIZE, inBytes = IN_BUF_SIZE;
char outBuf[OUT_BUF_SIZE];
int outPos = 0;
inline char getChar() {
if (inPos == inBytes) {
fin.read(inBuf, IN_BUF_SIZE);
inBytes = fin.gcount();
inPos = 0;
if (inBytes == 0)
return -1;
}
return inBuf[inPos++];
}
inline void writeChar(char c) {
if (outPos == OUT_BUF_SIZE) {
fout.write(outBuf, outPos);
outPos = 0;
}
outBuf[outPos++] = c;
}
T read() {
char c;
bool neg = false;
do {
c = getChar();
if (c == -1)
return -1;
} while ((c < '0' || c > '9') && c != '-');
if (c == '-') {
neg = true;
c = getChar();
}
T res = 0;
while (c >= '0' && c <= '9') {
res = res * 10 + (c - '0');
c = getChar();
}
return neg ? -res : res;
}
void write(T x) {
if (x < 0) {
writeChar('-');
x = -x;
}
char temp[20];
int len = 0;
do {
temp[len++] = '0' + x % 10;
x /= 10;
} while (x);
while (len--)
writeChar(temp[len]);
}
public:
FastIO(ifstream &finStream, ofstream &foutStream)
: fin(finStream), fout(foutStream) {}
void flush() {
if (outPos > 0) {
fout.write(outBuf, outPos);
outPos = 0;
}
}
friend FastIO<T> &operator>>(FastIO<T> &io, T &x) {
x = io.read();
return io;
}
friend FastIO<T> &operator<<(FastIO<T> &io, T x) {
io.write(x);
return io;
}
friend FastIO &operator<<(FastIO &io, const char *s) {
for (int i = 0; s[i]; ++i)
io.writeChar(s[i]);
return io;
}
};
template <typename T, typename HashFunc = std::hash<T>> class HashMap {
private:
int MOD;
HashFunc hasher;
std::vector<std::vector<std::pair<T, int>>> v;
size_t hashF(const T &x) const { return hasher(x) % MOD; }
public:
HashMap(int mod) : MOD(mod) { v.resize(MOD); }
void insert(const T &x) {
size_t nr = hashF(x);
for (auto &it : v[nr]) {
if (it.first == x) {
it.second++;
return;
}
}
v[nr].push_back(std::make_pair(x, 1));
}
void erase(const T &x) {
size_t nr = hashF(x);
for (size_t i = 0; i < v[nr].size(); ++i) {
if (v[nr][i].first == x) {
if (--v[nr][i].second > 0)
return;
v[nr][i] = v[nr].back();
v[nr].pop_back();
return;
}
}
}
typename std::vector<std::pair<T, int>>::iterator find(const T &x) {
size_t nr = hashF(x);
for (size_t i = 0; i < v[nr].size(); ++i)
if (v[nr][i].first == x)
return v[nr].begin() + i;
return v[nr].end();
}
int count(const T &x) const {
size_t nr = hashF(x);
for (size_t i = 0; i < v[nr].size(); ++i)
if (v[nr][i].first == x)
return v[nr][i].second;
return 0;
}
int countOnes() const {
int rez = 0;
for (int nr = 0; nr < MOD; ++nr) {
for (size_t i = 0; i < v[nr].size(); ++i) {
if (v[nr][i].second == 1)
++rez;
}
}
return rez;
}
int size() const {
int sum = 0;
for (int i = 0; i < MOD; ++i)
sum += (int)v[i].size();
return sum;
}
bool empty() const { return (size() == 0); }
void clear() {
for (int i = 0; i < MOD; ++i)
v[i].clear();
}
void print() const {
for (int i = 0; i < MOD; ++i) {
if (!v[i].empty()) {
std::cout << i << ": ";
for (size_t j = 0; j < v[i].size(); ++j) {
std::cout << v[i][j].first << " " << v[i][j].second;
std::cout << " ";
}
std::cout << "\n";
}
}
}
int &operator[](const T &x) {
auto it = find(x);
if (it != v[hashF(x)].end())
return it->second;
v[hashF(x)].push_back({x, 0});
return v[hashF(x)].back().second;
}
struct iterator {
int buc;
size_t ind;
HashMap *h;
iterator(int b, size_t i, HashMap *hashPtr) : buc(b), ind(i), h(hashPtr) {}
std::pair<T, int> &operator*() { return h->v[buc][ind]; }
iterator &operator++() {
++ind;
while (buc < h->MOD && ind >= h->v[buc].size()) {
++buc;
ind = 0;
}
return *this;
}
bool operator==(const iterator &other) const {
return h == other.h && buc == other.buc && ind == other.ind;
}
bool operator!=(const iterator &other) const { return !(*this == other); }
std::pair<T, int> *operator->() { return &h->v[buc][ind]; }
};
iterator begin() {
int b = 0;
while (b < MOD && v[b].empty())
b++;
return iterator(b, 0, this);
}
iterator end() { return iterator(MOD, 0, this); }
};
// greater<T> -> max heap
// less<T> -> min heap
template <typename T, typename Compare = std::less<T>> class Heap {
private:
T *heap;
int size;
int capacity;
Compare comp;
int parent(int i) { return (i - 1) / 2; }
int leftChild(int i) { return 2 * i + 1; }
int rightChild(int i) { return 2 * i + 2; }
void shiftUp(int i) {
while (i > 0 && comp(heap[i], heap[parent(i)])) {
std::swap(heap[i], heap[parent(i)]);
i = parent(i);
}
}
void shiftDown(int i) {
while (true) {
int best = i;
int left = leftChild(i);
int right = rightChild(i);
if (left < size && comp(heap[left], heap[best]))
best = left;
if (right < size && comp(heap[right], heap[best]))
best = right;
if (best == i)
break;
std::swap(heap[i], heap[best]);
i = best;
}
}
public:
Heap(int capacity) : heap(new T[capacity]), size(0), capacity(capacity) {}
// mai ar merge un constructor aici pentru cand primeste si un array
~Heap() { delete[] heap; }
// dezactivez copierea ca nu este sigur ca copiez new
// habarnam asa o zis chatgpt am incredere in el
Heap(const Heap &) = delete;
Heap &operator=(const Heap &) = delete;
bool empty() const { return size == 0; }
int getSize() const { return size; }
void insert(const T &nr) {
// as putea sa verific daca e full
if (size == capacity)
throw std::overflow_error("Heap is full");
heap[size] = nr;
shiftUp(size);
++size;
}
void update(int i, const T &nr) {
if (i < 0 || i >= size)
throw std::out_of_range("Invalid index");
heap[i] = nr;
if (i && comp(heap[i], heap[parent(i)]))
shiftUp(i);
else
shiftDown(i);
}
void remove(int i) {
if (i < 0 || i >= size)
throw std::out_of_range("Invalid index");
if (i == size - 1) {
size--;
return;
}
heap[i] = heap[--size];
if (i > 0 && comp(heap[i], heap[parent(i)]))
shiftUp(i);
else
shiftDown(i);
}
T removeTop() {
if (empty())
throw std::underflow_error("Heap is empty");
T top = getTop();
--size;
if (size > 0) {
heap[0] = heap[size];
shiftDown(0);
}
return top;
}
const T &getTop() const {
if (empty())
throw std::underflow_error("Heap is empty");
return heap[0];
}
void buildHeap() {
for (int i = size / 2 - 1; i >= 0; --i)
shiftDown(i);
}
};
int32 main() {
// FASTIO;
FastIO<int> io(fin, fout);
vector<int> v;
v.reserve(NRMAX);
Heap<int> hp(NRMAX);
HashMap<int> mp(NRMAX);
int n, c, x;
io >> n;
while (n--) {
io >> c;
if (c == 1) {
io >> x;
hp.insert(x);
v.pb(x);
} else if (c == 2) {
io >> x; // pozitia care trebuie eliminata
// cout << v[x - 1] << " ";
mp[v[x - 1]] = true;
} else {
while (mp[hp.getTop()]) {
// cout << hp.getTop() << " ";
mp[hp.removeTop()] = 0;
}
// cout << "\n";
io << hp.getTop() << "\n";
}
}
io.flush();
// while (hp.empty()) {
// cout << hp.getTop() << " ";
// hp.removeTop();
// }
// cout << "\n";
return 0;
}