#include <cstdio>
#include <iostream>
using namespace std;
const int infinity = 0x3f3f3f3f;
int lc[200010], rc[200010], sz[200010], w[200010], h[200010];
int n, tot, rt;
void pushup(int x) { sz[x] = sz[lc[x]] + sz[rc[x]] + 1; }
void lotate(int &x) { int lson = lc[x]; lc[x] = rc[lson]; rc[lson] = x; pushup(x); pushup(lson); x = lson; }
void rotate(int &x) { int rson = rc[x]; rc[x] = lc[rson]; lc[rson] = x; pushup(x); pushup(rson); x = rson; }
void insert(int &x, int key)
{
if (x == 0) { x = ++tot; sz[x] = 1; w[x] = key; h[x] = rand(); return; }
if (key <= w[x]) { insert(lc[x], key); if (h[lc[x]] < h[x]) { lotate(x); } }
else { insert(rc[x], key); if (h[rc[x]] < h[x]) { rotate(x); } } pushup(x);
}
void erase(int &x, int key)
{
if (w[x] == key)
{
if (lc[x] == 0 || rc[x] == 0) { x = lc[x] + rc[x]; return; }
else { if (h[lc[x]] < h[rc[x]]) rotate(x), erase(lc[x], key); else lotate(x), erase(rc[x], key); }
}
else { if (w[x] > key) erase(lc[x], key); else erase(rc[x], key); } pushup(x);
}
int qrank(int x, int key) { if (x == 0) { return 1; } if (w[x] >= key) return qrank(lc[x], key); else return qrank(rc[x], key) + sz[lc[x]] + 1; }
int query(int x, int rk)
{ if (sz[lc[x]] + 1 == rk) return w[x]; if (sz[lc[x]] + 1 < rk) return query(rc[x], rk - sz[lc[x]] - 1); else return query(lc[x], rk); }
int left(int x, int key) { if (x == 0) { return -infinity; } if (w[x] < key) return max(w[x], left(rc[x], key)); else return left(lc[x], key); }
int right(int x, int key) { if (x == 0) { return infinity; } if (w[x] > key) return min(w[x], right(lc[x], key)); else return right(rc[x], key); }
int main()
{
scanf("%d", &n);
for (int x, y, i = 1; i <= n; i++)
{
scanf("%d%d", &x, &y);
if (x == 1) insert(rt, y);
if (x == 2) erase(rt, y);
if (x == 3) printf("%d\n", qrank(rt, y));
if (x == 4) printf("%d\n", query(rt, y));
if (x == 5) printf("%d\n", left(rt, y));
if (x == 6) printf("%d\n", right(rt, y));
}
return 0;
}