/**
    SPOJ: ORDERSET
    initially array is empty. Then queries are:
    I x: insert x
    D x  delete x
    C x: print the number of elements of S smaller than x 
    K x: print the k'th element
*/
#include<bits/stdc++.h>
using namespace std;
 
typedef struct node
{
    int siz, key, prior;
    struct node *l, *r;
    node(){}
    node(int key_):key(key_),siz(1),prior(rand()), l(NULL), r(NULL){}
}node;
 
typedef node* pnode;
int siz(pnode t)
{
    return t?t->siz:0;
}
void upd_siz(pnode &t)
{
    if(t) t->siz = 1 + siz(t->l) + siz(t->r);
}
void split(pnode t, pnode &l, pnode &r, int key)
{
    if(!t) l = r = NULL;
    else if(key < t->key ) split(t->l, l, t->l, key), r = t;
    else split(t->r, t->r, r, key), l=t;
    upd_siz(t); 
}
void merg(pnode &t, pnode lf, pnode rg)
{
    if(!lf || !rg) t = lf?lf:rg;
    else if(lf->prior > rg->prior) merg(lf->r, lf->r, rg), t = lf;
    else merg(rg->l, lf, rg->l), t = rg;
    upd_siz(t);
}
void eras(pnode &t, int key)
{
    if(!t) return;
    if(t->key==key) {pnode tmp = t;merg(t, t->l, t->r);free(tmp);}
    else eras(key<=t->key?t->l:t->r, key);
    upd_siz(t);
}
void inseert(pnode &t, pnode nt)
{
    if(!t) t = nt;
    else if(nt->prior > t->prior) split(t, nt->l, nt->r, nt->key), t = nt;
    else inseert(nt->key <= t->key?t->l:t->r, nt);
    upd_siz(t);
}
int cnt(pnode t, int key)
{
    pnode l, r;
    split(t, l, r, key-1);
    int ans = siz(l);
    merg(t, l, r);
    return ans;
}
int kth(pnode t, int k)
{
    int cntLeft = siz(t->l);
    if(cntLeft==k-1)
        return t->key;
    else if(cntLeft>=k)
        return kth(t->l, k);
    else return kth(t->r, k - cntLeft - 1);
}
bool Find(pnode t, int x)
{
    if(!t) return 0;
    if(t->key == x)
        return 1;
    if(x <= t->key)
        return Find(t->l, x);
    return Find(t->r, x);
}
 
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n, x;
    cin>>n;
    pnode t = NULL;
    while(n--)
    {
        string op;
        cin>>op>>x;
        if(op[0]=='I' && !Find(t, x))
        {
            inseert(t, new node(x));
            //print(t);
        }
        else if(op[0]=='D' && Find(t, x))
        {
            eras(t, x);
        }
        else if(op[0]=='C')
        {
            cout << cnt(t, x) << "\n";
        }
        else if(op[0]=='K')
        {
            if(x>siz(t))
                cout << "invalid\n";
            else cout << kth(t, x) << "\n";
        }
    }
    return 0;
}
