//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f7f7f7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define PiL pair <ll , ll>
#define PLL pair <ll , ll>
#define Pii pair <int , int>
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)
template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;
const int N = 3e5 + 5;
struct splay_Tre
{
int ch[N][2], siz[N], fa[N], val[N], cnt[N], rt, tot;
int get (int x) {return x == ch[fa[x]][1];}
void push_up(int x) {siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + cnt[x] ; }
void clear (int x) {ch[x][0] = ch[x][1] = fa[x] = siz[x] = val[x] = cnt[x] = 0; }
void rotate (int x)
{
int y = fa[x] , z = fa[y], chk = get(x);
ch[y][chk] = ch[x][chk ^ 1];
fa[ch[x][chk ^ 1]] = y;
ch[x][chk ^ 1] = y;
fa[y] = x;
fa[x] = z;
if (z) ch[z][y == ch[z][1]] = x;
push_up(y);
push_up(x);
}
void splay (int x, int to)
{
int tof = fa[to];
while (fa[x] != tof)
{
if (fa[fa[x]] == tof) rotate(x);
else if (get(x) == get(fa[x])) rotate(fa[x]) , rotate(x);
else rotate(x) , rotate(x);
}
if (rt == to) rt = x;
}
//k的排名
int rk (int k)
{
int res = 0 , cur = rt;
while (1)
{
if (k < val[cur])
cur = ch[cur][0];
else
{
res += siz[ch[cur][0]];
if (k == val[cur])
{
splay(cur, rt);
return res + 1;
}
res += cnt[cur];
cur = ch[cur][1];
}
}
}
//排名为k的数
int kth (int k)
{
int cur = rt;
while (1)
{
if (ch[cur][0] && k <= siz[ch[cur][0]])
cur = ch[cur][0];
else
{
k -= cnt[cur] + siz[ch[cur][0]];
if (k <= 0)
{
splay(cur,rt);
return val[cur];
}
cur = ch[cur][1];
}
}
}
void insert (int k)
{
if (!rt)
{
val[++ tot] = k;
cnt[tot] ++;
rt = tot;
push_up(rt);
return ;
}
int cur = rt , f = 0;
while (1)
{
if (val[cur] == k)
{
cnt[cur] ++;
push_up(cur);
push_up(f);
splay(cur, rt);
break;
}
f = cur;
cur = ch[cur][val[cur] < k];
if (! cur)
{
val[++ tot] = k;
cnt[tot] ++;
fa[tot] = f;
ch[f][val[f] < k] = tot;
push_up(tot);
push_up(f);
splay(tot, rt);
break;
}
}
}
int leftMax ()
{
int cur = ch[rt][0];
while (ch[cur][1]) cur = ch[cur][1];
splay (cur, rt);
return cur;
}
int rightMin()
{
int cur = ch[rt][1];
while (ch[cur][0]) cur = ch[cur][0];
splay (cur , rt);
return cur;
}
void del (int k)
{
rk (k);
if (cnt[rt] > 1)
{
cnt[rt] --;
push_up(rt);
return ;
}
if (!ch[rt][0] && !ch[rt][1])
{
clear (rt);
rt = 0;
return ;
}
if (!ch[rt][0])
{
int cnr = rt;
rt = ch[rt][1];
fa[rt] = 0;
clear(cnr);
return;
}
if (!ch[rt][1])
{
int cnr = rt;
rt = ch[rt][0];
fa[rt] = 0;
clear(cnr);
return;
}
int cnr = rt, x = leftMax();
splay(x, rt);
fa[ch[cnr][1]] = x;
ch[x][1] = ch[cnr][1];
clear(cnr);
push_up(rt);
}
void Pre (int k)
{
insert(k);
write(val[leftMax()]), LF;
del(k);
}
void Nxt (int k)
{
insert(k);
write(val[rightMin()]), LF;
del(k);
}
}tree;
int main (void)
{
int n;
read (n);
while (n --) {
int op, x;
read(op, x);
if (op == 1)
tree.insert(x);
else if (op == 2)
tree.del(x);
else if (op == 3)
write(tree.rk(x)),LF;
else if(op == 4)
write(tree.kth(x)), LF;
else if(op == 5)
tree.Pre(x);
else
tree.Nxt(x);
}
}