#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstdlib>
using namespace std;
const int N = 1e5 + 5;
int cnt, root, x, y, z;
struct Node{
int l, r, val, key, size;
}fhq[N];
void update(int now)
{
fhq[now].size = fhq[fhq[now].l].size+fhq[fhq[now].r].size+1;
}
void split(int now, int val, int& x, int& y)
{
if (!now) x = y = 0;
else
{
if (fhq[now].val <= val)
{
x = now;
split(fhq[now].r, val, fhq[now].r, y);
}
else
{
y = now;
split(fhq[now].l, val, x, fhq[now].l);
}
update(now);
}
}
int merge(int x, int y)
{
if (!x || !y) return x + y;
if (fhq[x].key >= fhq[y].key)
{
fhq[x].r = merge(fhq[x].r, y);
update(x);
return x;
}
else
{
fhq[y].l = merge(x, fhq[y].l);
update(y);
return y;
}
}
int new_node(int val)
{
fhq[++cnt].size = 1;
fhq[cnt].val = val;
fhq[cnt].key = rand();
return cnt;
}
void insert(int val)
{
split(root, val, x, y);
root = merge(merge(x, new_node(val)), y);
}
void del(int val)
{
split(root, val, x, z);
split(x, val-1, x, y);
y = merge(fhq[y].l, fhq[y].r);
root = merge(merge(x, y), z);
}
int get_rank(int val)
{
split(root, val-1, x, y);
printf("%d\n", fhq[x].size+1);
root = merge(x, y);
}
int get_num(int rank)
{
int now = root;
while (now)
{
int t = fhq[fhq[now].l].size+1;
if (t == rank) break;
else if (t > rank) now = fhq[now].l;
else
{
rank -= t;
now = fhq[now].r;
}
}
printf("%d\n", fhq[now].val);
}
int get_pre(int val)
{
split(root, val-1, x, y);
int now = x;
while (fhq[now].r) now = fhq[now].r;
printf("%d\n", fhq[now].val);
root = merge(x, y);
}
int get_next(int val)
{
split(root, val, x, y);
int now = y;
while (fhq[now].l) now = fhq[now].l;
printf("%d\n", fhq[now].val);
root = merge(x, y);
}
int main()
{
srand((unsigned)time(NULL));
int n;
scanf("%d", &n);
while (n--)
{
int opt, k;
scanf("%d%d", &opt, &k);
if (opt == 1) insert(k);
else if (opt == 2) del(k);
else if (opt == 3) get_rank(k);
else if (opt == 4) get_num(k);
else if (opt == 5) get_pre(k);
else get_next(k);
}
return 0;
}