#include <bits/stdc++.h>
#define Pre true
#define Nxt false
#define int long long
using namespace std;
const int N = 100010;
int n, m, op, x;
namespace BST
{
struct node
{
node *lef, *rig;
int val, siz;
}*root, nn[N];
int ans, l, cnt = -1;
int le(node *r)
{
if (r->lef == NULL)
return 0;
return r->lef->siz;
}
void init(int x)
{
root = &nn[++cnt];
root->siz = 1;
root->val = x;
root->rig = root->lef = NULL;
}
void insert(node *r, int x)
{
if (x > r->val)
{
if (r->rig == NULL)
{
node *rr = &nn[++cnt];
r->rig = rr;
rr->val = x;
rr->siz = 1;
rr->lef = rr->rig = NULL;
}
else
insert(r->rig, x);
}
else
{
if (r->lef == NULL)
{
node *rr = &nn[++cnt];
r->lef = rr;
rr->val = x;
rr->siz = 1;
rr->lef = rr->rig = NULL;
}
else
insert(r->lef, x);
}
r->siz = 1;
if (r->lef != NULL)
r->siz += r->lef->siz;
if (r->rig != NULL)
r->siz += r->rig->siz;
return;
}
int k;
int query(node *r, int x)
{
if (r->siz == 1)
{
k++;
return -INT_MAX;
}
if (le(r) + 1 + k < x)
{
k += 1 + le(r);
if (r->rig == NULL)
{
if (k == x)
return r->val;
return -INT_MAX;
}
int p = query(r->rig, x);
k++;
if (k == x)
return r->val;
return p;
}
else
{
if (r->lef == NULL)
{
k++;
if (k == x)
return r->val;
return -INT_MAX;
}
int p = query(r->lef, x);
if (k == x)
{
k++;
return r->val;
}
return p;
}
}
void pree(node *r, int x)
{
if (x > r->val)
{
ans = max(r->val, ans);
if (r->rig != NULL)
pree(r->rig, x);
}
else
if (r->lef != NULL)
pree(r->lef, x);
}
void pre(node *r, int x)
{
if (x <= r->val)
{
if (r->lef != NULL)
pre(r->lef, x);
}
else
{
ans = max(r->val, ans);
if (r->rig != NULL)
pre(r->rig, x);
}
}
void nxt(node *r, int x)
{
if (x >= r->val)
{
if (r->rig != NULL)
nxt(r->rig, x);
}
else
{
ans = min(r->val, ans);
if (r->lef != NULL)
nxt(r->lef, x);
}
}
int rank(int x, bool t)
{
if (t == Pre)
{
ans = -INT_MAX;
pre(root, x);
return ans;
}
else
{
ans = INT_MAX;
nxt(root, x);
return ans;
}
}
int qr(node *r, int x, int k)
{
if (x == r->val)
return k;
else if (x < r->val)
{
if (r->lef != NULL)
return qr(r->lef, x, k);
}
else
{
if (r->rig != NULL)
return qr(r->rig, x, k + 1 + le(r));
}
}
int queryrank(int x)
{
ans = -INT_MAX;
pree(root, x);
if (ans == -INT_MAX)
return n + 1;
int res = qr(root, ans, 0);
return res + 2;
}
}
using namespace BST;
signed main()
{
cin >> m;
int siz = 0;
while(m--)
{
scanf("%lld%lld", &op, &x);
switch(op)
{
case 1:
{
printf("%lld\n", queryrank(x));
break;
}
case 2:
{
k = 0;
printf("%lld\n", query(root, x));
break;
}
case 3:
{
printf("%lld\n", BST::rank(x, Pre));
break;
}
case 4:
{
printf("%lld\n", BST::rank(x, Nxt));
break;
}
case 5:
{
if (siz == 0)
{
siz++;
init(x);
}
else
insert(root, x);
n++;
break;
}
}
}
}