#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#define fi first
#define se second
#define in(x) getint(&x)
#define pii pair<int,int>
#define FOR(i,m,n) for(int i=m;i<=n;i++)
using namespace std;
typedef long long ll;
const int maxn = 100010;
const int inf = 1e9;
const int mod = 1e9+7;
template <typename T> inline void getint(T* _num){
(*_num) = 0; int _op = 1; bool _ok = 0; char _c;
while(1){_c = getchar(); if(_c == '-') _op = -1;if(_c <= 57 && _c >= 48)
{ _ok = 1; (*_num) = (*_num)*10+_c-48;}else if(_ok){(*_num)*=_op;return;}}}
template <typename T> void outint(T _out)
{if(_out >= 10) outint(_out/10); putchar(_out%10+48);}
struct splay_node{
int value, size, cnt;
splay_node *pre, *ch[2];
void update(){
size = ch[0]->size + ch[1]->size + cnt;
}
int get_wh(){
return pre->ch[0] == this ? 0 : 1;
}
void set_ch(int wh, splay_node *child);
} pool[maxn], *root, *null;
void splay_node :: set_ch(int wh, splay_node *child){
ch[wh] = child;
if(child != null) child->pre = this;
update();
}
int top = 0;
inline splay_node *get_new(int value){
splay_node *now = pool + (++top);
now->value = value;
now->size = 1;
now->cnt = 1;
now->pre = now->ch[0] = now->ch[1] = null;
return now;
}
inline void rotate(splay_node *now){
splay_node *old_father = now->pre, *grand = now->pre->pre;
int wh = now->get_wh();
old_father->set_ch(wh, now->ch[wh^1]);
now->set_ch(wh^1, old_father);
now->pre = grand;
if(grand != null)
grand->ch[grand->ch[0] == old_father ? 0:1] = now;
else root = now;
}
inline void splay(splay_node *now, splay_node *tar){
for(; now->pre != tar; rotate(now))
if(now->pre->pre != tar)
now->get_wh() == now->pre->get_wh() ? rotate(now->pre) : rotate(now);
if(tar == null) root = now;
}
void insert(int value){
splay_node *last = null, *now = root;
splay_node *newone = get_new(value);
while(now != null){
last = now;
if(newone->value == now->value){
now->cnt ++, now->size ++;
splay(now, null);
return;
}
if(newone->value < now->value) now = now->ch[0];
else now = now->ch[1];
}
if(last == null) { root = newone; return; }
else{
if(newone->value < last->value) last->set_ch(0, newone);
else last->set_ch(1, newone);
splay(newone, null);
}
}
inline splay_node *find(int value){
splay_node *now = root;
while(now != null){
if(now->value == value) break;
if(value < now->value) now = now->ch[0];
else now = now->ch[1];
}
if(now != null) splay(now, null);
return now;
}
inline int pre(int value){
int ans = -inf;
splay_node *now = root;
while(now != null)
if(now->value < value){
ans = max(ans, now->value);
now = now->ch[1];
}else now = now->ch[0];
return ans;
}
inline int next(int value){
int ans = inf;
splay_node *now = root;
while(now != null)
if(now->value > value){
ans = min(ans, now->value);
now = now->ch[0];
}else now = now->ch[1];
return ans;
}
void del(int value){
splay_node *now = find(value);
if(now == null) return;
if(now->cnt > 1) { now->cnt--; now->size--; return; }
if(now->ch[0] == null && now->ch[1] == null) root = null;
else if(now->ch[1] == null){
now->ch[0]->pre = null;
root = now->ch[0];
}else if(now->ch[0] == null){
now->ch[1]->pre = null;
root = now->ch[1];
}else{
splay_node *tmp = now->ch[0];
while(tmp->ch[1] != null) tmp = tmp->ch[1];
splay(tmp, now);
tmp->set_ch(1, now->ch[1]);
tmp->pre = null;
root = tmp;
}
}
inline int get_rank(int value){
splay_node *now = root;
int left_size = 0;
while(now != null){
if(value == now->value){
int ans = left_size + now->ch[0]->size + 1;
splay(now, null);
return ans;
}
else if(value < now->value) now = now->ch[0];
else left_size += now->ch[0]->size + now->cnt, now = now->ch[1];
}
return -1;
}
inline int kth(int k){
splay_node *now = root;
int left_size = 0;
while(now != null){
int tmp = left_size + now->ch[0]->size;
if(tmp + 1 <= k && k <= tmp + now->cnt){
splay(now, null);
return now->value;
}
if(k <= tmp) now = now->ch[0];
else left_size = tmp + now->cnt, now = now->ch[1];
}
return -1;
}
int main(){
null = pool;
null->value = 0;
null->size = 0;
null->cnt = 0;
null->pre = null->ch[0] = null->ch[1] = null;
root = null;
int q; in(q);
while(q--){
int order, val;
in(order), in(val);
switch(order){
case 1: insert(val); break;
case 2: del(val); break;
case 3: printf("%d\n", get_rank(val)); break;
case 4: printf("%d\n", kth(val)); break;
case 5: printf("%d\n", pre(val)); break;
case 6: printf("%d\n", next(val)); break;
}
}
return 0;
}