#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;

main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    ordered_set me;
    while(n--)
    {
        char t;
        int x;
        cin >> t >> x;
        if(t == 'K')
            if(x > me.size())
                cout << "invalid\n";
            else
                cout << *me.find_by_order(x - 1) << "\n";
        if(t == 'C')
            cout << me.order_of_key(x) << "\n";
        if(t == 'I')
            me.insert(x);
        if(t == 'D')
            me.erase(x);
    }
}

