#include<set>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define il inline
#define ri register
#define pc(i) putchar(i)
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r>>1)
using namespace std;
const int N=1e5+2;
int n,lsh[N<<2],tot;
struct A{int opt,value;}op[N<<2];
struct B{int val;}tree[N<<3];
il int read()
{
    int as=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar(); }
    while(ch>='0'&&ch<='9') as=(as<<3)+(as<<1)+(ch^48),ch=getchar();
    return as*f;
}
il void wt(int x){if(x<0) x=-x,pc('-');if(x>9) wt(x/10);pc(x%10|48);}
il void pushup(int rt){tree[rt].val=tree[ls].val+tree[rs].val;}
il void update(int rt,int l,int r,int q,int k)//单点修改
{
    if(l==r) return tree[rt].val+=k,void();
    if(q<=mid) update(ls,l,mid,q,k);
    else update(rs,mid+1,r,q,k);
    pushup(rt);
}
il int query_sum(int rt,int l,int r,int ql,int qr)//区间求和(查询x的排名)
{
    if(ql<=l&&r<=qr) return tree[rt].val;int re=0;
    if(ql<=mid) re+=query_sum(ls,l,mid,ql,qr);
    if(qr>mid) re+=query_sum(rs,mid+1,r,ql,qr);
    return re;
}
il int query_num(int rt,int l,int r,int q)//查询第q大
{
    if(l==r) return l;
    if(tree[ls].val>=q) return query_num(ls,l,mid,q);
    else return query_num(rs,mid+1,r,q-tree[ls].val);
}
int main()
{
    freopen("qzxds.in","r",stdin);
    freopen("qzxds.out","w",stdout);
    n=read();
    for(ri int i=1;i<=n;++i)
    {
        op[i]=(A){read(),read()};//opt val
        if(op[i].opt==4) continue;
        lsh[++tot]=op[i].value;//离散化
    }
    sort(lsh+1,lsh+1+tot),tot=unique(lsh+1,lsh+1+tot)-lsh-1;
    for(ri int i=1;i<=n;++i)
    {
        if(op[i].opt!=4) op[i].value=lower_bound(lsh+1,lsh+tot+1,op[i].value)-lsh;
        if(op[i].opt==1) update(1,1,tot,op[i].value,1);
        if(op[i].opt==2) update(1,1,tot,op[i].value,-1);
        if(op[i].opt==3) //查询x数的排名
            if(op[i].value==1)  puts("1");
            else wt(query_sum(1,1,tot,1,op[i].value-1)+1),pc('\n');
        if(op[i].opt==4) wt(lsh[query_num(1,1,tot,op[i].value)]),pc('\n'); //查询排名为 x 的数
        if(op[i].opt==5) wt(lsh[query_num(1,1,tot,query_sum(1,1,tot,1,op[i].value-1))]),pc('\n');//前驱
        if(op[i].opt==6) wt(lsh[query_num(1,1,tot,query_sum(1,1,tot,1,op[i].value)+1)]),pc('\n');//后继
    }
    return 0;
}