#include <set>
#include <bitset>
#include <list>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <ios>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <complex>
#include <numeric>
#include <functional>
#include <cmath>
#include <ctime>
#include <climits>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cassert>
using namespace std;
#define CFF freopen ("aaa.txt", "r", stdin)
#define CPPFF ifstream cin ("aaa.txt")
#define LL long long
#define ULL unsigned long long
#define DB(ccc) cout << #ccc << " = " << ccc << endl
const int INF = 0x7FFFFFFF;
const int MOD = 1e9 + 7;
const double EPS = 1e-6;
const double PI = 2 * acos (0.0);
const int maxn = 1e5 + 66;
struct node
{
int l, r, v, size, rnd, w;
}tr[maxn];
int n, size, root, res;
void UpDate (int rt)//更新结点信息
{
tr[rt].size = tr[tr[rt].l].size + tr[tr[rt].r].size + tr[rt].w;
}
void rturn (int &rt)
{
int tt = tr[rt].l; tr[rt].l = tr[tt].r; tr[tt].r = rt;
tr[tt].size = tr[rt].size; UpDate(rt); rt = tt;
}
void lturn (int &rt)
{
int tt = tr[rt].r; tr[rt].r = tr[tt].l; tr[tt].l = rt;
tr[tt].size = tr[rt].size; UpDate(rt); rt = tt;
}
void Insert (int &rt, int x)
{
if (rt == 0)
{
rt = ++size;
tr[rt].size = tr[rt].w = 1; tr[rt].v = x; tr[rt].rnd = rand();
return ;
}
tr[rt].size++;
if (tr[rt].v == x) tr[rt].w++;//每个结点顺便记录下与该节点相同值的数的个数
else if (x > tr[rt].v)
{
Insert (tr[rt].r, x);
if (tr[tr[rt].r].rnd < tr[rt].rnd) lturn (rt);//维护堆性质
}
else
{
Insert (tr[rt].l, x);
if (tr[tr[rt].l].rnd < tr[rt].rnd) rturn (rt);
}
}
void Del (int &rt, int x)
{
if (rt == 0) return ;
if (tr[rt].v == x)
{
if (tr[rt].w > 1)
{
tr[rt].w--; tr[rt].size--; return ;//若不止相同值的个数有多个,删去一个
}
if (tr[rt].l * tr[rt].r == 0) rt = tr[rt].l + tr[rt].r;//有一个儿子为空
else if (tr[tr[rt].l].rnd < tr[tr[rt].r].rnd)
rturn (rt), Del (rt, x);
else lturn (rt), Del (rt, x);
}
else if (x > tr[rt].v)
tr[rt].size--, Del (tr[rt].r, x);
else tr[rt].size--, Del (tr[rt].l, x);
}
int QueryRank (int rt, int x)
{
if (rt == 0) return 0;
if (tr[rt].v == x) return tr[tr[rt].l].size + 1;
else if (x > tr[rt].v)
return tr[tr[rt].l].size + tr[rt].w + QueryRank (tr[rt].r, x);
else return QueryRank (tr[rt].l, x);
}
int QueryNum (int rt, int x)
{
if (rt == 0) return 0;
if (x <= tr[tr[rt].l].size)
return QueryNum (tr[rt].l, x);
else if (x > tr[tr[rt].l].size + tr[rt].w)
return QueryNum (tr[rt].r, x - tr[tr[rt].l].size - tr[rt].w);
else return tr[rt].v;
}
void QueryPro (int rt, int x)
{
if (rt == 0) return ;
if (tr[rt].v < x)
{
res = rt; QueryPro (tr[rt].r, x);
}
else QueryPro (tr[rt].l, x);
}
void QuerySub (int rt, int x)
{
if (rt == 0) return ;
if (tr[rt].v > x)
{
res = rt; QuerySub (tr[rt].l, x);
}
else QuerySub (tr[rt].r, x);
}
int main()
{
// CFF;
int n;
scanf ("%d", &n);
for (int i = 1; i <= n; i++)
{
int oper, val;
scanf ("%d%d", &oper, &val);
if (oper == 1) Insert (root, val);
else if (oper == 2) Del (root, val);
else if (oper == 3) printf ("%d\n", QueryRank (root, val));
else if (oper == 4) printf ("%d\n", QueryNum (root, val));
else if (oper == 5)
{
res = 0;
QueryPro (root, val);
printf ("%d\n", tr[res].v);
}
else if (oper == 6)
{
res = 0;
QuerySub (root, val);
printf ("%d\n", tr[res].v);
}
}
return 0;
}