#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5+5;
struct node{
int son[2];
int val;
int cnt;
int siz;
int fa;
}tr[2000005];
int root, tot;
bool Dir(int x){
return tr[tr[x].fa].son[1] == x;
}
void pushup(int x){
tr[x].siz = tr[tr[x].son[0]].siz + tr[tr[x].son[1]].siz + tr[x].cnt;
}
void rorate(int x){
int y = tr[x].fa, z = tr[y].fa, k = Dir(x), w = tr[x].son[k ^ 1];
tr[z].son[Dir(y)] = x;tr[x].fa = z;
tr[x].son[k ^ 1] = y;tr[y].fa = x;
tr[y].son[k] = w;tr[w].fa = y;
pushup(x);pushup(y);
}
void splay(int x, int goal = 0){
while (tr[x].fa != goal){
int y = tr[x].fa, z = tr[y].fa;
if (z != goal){
if (Dir(x) == Dir(y)) rorate(y);
else rorate(x);
}
rorate(x);
}
if (!goal) root = x;
}
void find(int x){
if (!root) return;
int cur = root;
while (tr[cur].son[x > tr[cur].val] && tr[cur].val != x){
cur = tr[cur].son[x > tr[cur].val];
}
splay(cur);
}
void insert(int x){
int cur = root, p = 0;
while (cur && tr[cur].val != x){
p = cur;
cur = tr[cur].son[x > tr[cur].val];
}
if (cur) ++tr[cur].cnt;
else{
cur = ++tot;
if (p) tr[p].son[x > tr[p].val] = cur;
tr[cur].son[0] = tr[cur].son[1] = 0;
tr[cur].val = x;
tr[cur].fa = p;
tr[cur].siz = tr[cur].cnt = 1;
}
splay(cur);
}
int getpre(int x){
find(x);
if (tr[root].val < x) return root;
int cur = tr[root].son[0];
while (tr[cur].son[1]) cur = tr[cur].son[1];
splay(cur);
return cur;
}
int getsuf(int x){
find(x);
if (tr[root].val > x) return root;
int cur = tr[root].son[1];
while (tr[cur].son[0]) tr[cur].son[0];
splay(cur);
return cur;
}
void del(int x){
int p = getpre(x), s = getsuf(x);
splay(p), splay(s, p);
int tdel = tr[s].son[0];
if (tr[tdel].cnt > 1){
--tr[tdel].cnt;
splay(tdel);
}
else tr[s].son[0] = 0;
pushup(s), pushup(root);
}
int getrank(int x){
find(x);
if (tr[root].val < x) return tr[tr[root].son[0]].siz + tr[root].cnt;
else return tr[tr[root].son[0]].siz;
}
int getkth(int k){
int cur = root;
while (1){
if (tr[cur].son[0] && k <= tr[tr[cur].son[0]].siz){
cur = tr[cur].son[0];
}
else{
int tmp = (tr[cur].son[0] ? tr[tr[cur].son[0]].siz : 0) + tr[cur].cnt;
if (k <= tmp){
splay(cur);
return cur;
}
k -= tmp;
cur = tr[cur].son[1];
}
}
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
cin >> n;
insert(-INT_MAX);
insert(INT_MAX);
while (n--){
int op, x;
cin >> op >> x;
if (op == 1){
insert(x);
}
else if (op == 2){
del(x);
}
else if (op == 3){
cout << getrank(x) << '\n';
}
else if (op == 4){
cout << tr[getkth(x + 1)].val << '\n';
}
else if (op == 5){
cout << tr[getpre(x)].val << '\n';
}
else{
cout << tr[getsuf(x)].val << '\n';
}
}
return 0;
}