#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);
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwp1c2luZyBuYW1lc3BhY2UgX19nbnVfcGJkczsKCnR5cGVkZWYgdHJlZTwKaW50LApudWxsX3R5cGUsCmxlc3M8aW50PiwKcmJfdHJlZV90YWcsCnRyZWVfb3JkZXJfc3RhdGlzdGljc19ub2RlX3VwZGF0ZT4gb3JkZXJlZF9zZXQ7CgptYWluKCkKewogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjaW4udGllKDApOwogICAgaW50IG47CiAgICBjaW4gPj4gbjsKICAgIG9yZGVyZWRfc2V0IG1lOwogICAgd2hpbGUobi0tKQogICAgewogICAgICAgIGNoYXIgdDsKICAgICAgICBpbnQgeDsKICAgICAgICBjaW4gPj4gdCA+PiB4OwogICAgICAgIGlmKHQgPT0gJ0snKQogICAgICAgICAgICBpZih4ID4gbWUuc2l6ZSgpKQogICAgICAgICAgICAgICAgY291dCA8PCAiaW52YWxpZFxuIjsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgY291dCA8PCAqbWUuZmluZF9ieV9vcmRlcih4IC0gMSkgPDwgIlxuIjsKICAgICAgICBpZih0ID09ICdDJykKICAgICAgICAgICAgY291dCA8PCBtZS5vcmRlcl9vZl9rZXkoeCkgPDwgIlxuIjsKICAgICAgICBpZih0ID09ICdJJykKICAgICAgICAgICAgbWUuaW5zZXJ0KHgpOwogICAgICAgIGlmKHQgPT0gJ0QnKQogICAgICAgICAgICBtZS5lcmFzZSh4KTsKICAgIH0KfQoK