// #include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <string.h>
#include <stack>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <climits>
#include <ctime>
#include <unordered_map>
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ll long long
#define ld long double
#define uns unsigned
#define F first
#define S second
#define sz(x) (int)x.size()
#define str(x) (int)strlen(x)
#define all(x) x.begin(), x.end()
#define sqr(x) ((x) * (x))
#define bit(x) __builtin_popcountll(x)
#define itr ::iterator
#define rt return
#define sf scanf
#define pf printf
#define nl '\n'
#define forit(it,S) for(__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)
#define SABR inline ll IN(){ll x=0,ch=getchar(),f=1;while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar();if (ch=='-'){f=-1;ch=getchar();}while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}inline void OUT(ll x){if (x<0) putchar('-'),x=-x;if (x>=10) OUT(x/10),putchar(x%10+'0');else putchar(x+'0');}
#define ios ios_base::sync_with_stdio(0);
#define int long long
#define int long long
#define NeverGiveUp main
#define Memset(x, y, n) for (int mmst = 1; mmst <= n; mmst++) x[mmst] = y;
#define sp system("pause");
#define Fname "inverse"
#define RockyBalboa
//#define TNT
using namespace std;
typedef pair <int, int> pi;
typedef vector <int> vi;
typedef pair <double, double> pd;
const int N = (int)1e5 + 17;
const int MX = (int) 1e6 + 17;
const int MOD = (int) 1e9 + 7;
const ll oo = LLONG_MAX;
const int INF = INT_MAX;
const ld Pi = 3.14159265358979323846264338327950288419716939937510;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};
void IOI2017(){
#ifdef RockyBalboa
freopen(Fname".in", "r", stdin);
freopen(Fname".out", "w", stdout);
#endif
#ifdef TNT
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
SABR
const int sz = 200002;
int n;
pi t[sz * 4], x[sz];
vector <pi> cmp;
pair <char, int> in[2 * N];
inline void Update(int pos, pi val, int v = 1, int tl = 1, int tr = sz){
if (tl == tr) t[v] = val;
else{
int tm = tl + tr >> 1;
if (pos <= tm) Update(pos, val, v + v, tl, tm);
else Update(pos, val, v + v + 1, tm + 1, tr);
t[v].F = t[v + v].F + t[v + v + 1].F;
}
}
inline int Get(int l, int r, int v = 1, int tl = 1, int tr = sz){
if (l <= tl && tr <= r) rt t[v].F;
if (tl > r || tr < l) rt 0;
int tm = tl + tr >> 1;
rt Get(l, r, v + v, tl, tm) + Get(l, r, v + v + 1, tm + 1, tr);
}
inline int Kth(int k, int v = 1, int tl = 1, int tr = sz){
if (tl == tr) rt t[v].S;
int tm = tl + tr >> 1;
if (t[v + v].F >= k) rt Kth(k, v + v, tl, tm);
else rt Kth(k - t[v + v].F, v + v + 1, tm + 1, tr);
}
NeverGiveUp(){
ios
cin >> n;
for (int i = 1; i <= n; i++){
cin >> in[i].F >> in[i].S;
if (in[i].F != 'K')
cmp.pb({in[i].S, i});
}
sort(all(cmp));
for (int i = 0, j = 1; i < sz(cmp); i++){
if (i > 0 && cmp[i].F != cmp[i - 1].F) j++;
x[cmp[i].S] = {j, cmp[i].F};
}
for (int i = 1; i <= n; i++){
if (in[i].F == 'I') Update(x[i].F, {1, x[i].S});
if (in[i].F == 'C') cout << Get(0, x[i].F - 1) << nl;
if (in[i].F == 'D') Update(x[i].F, {0, 0});
if (in[i].F == 'K'){
if (t[1].F < in[i].S) cout << "invalid\n";
else cout << Kth(in[i].S) << nl;
}
}
rt 0;
}
Ly8gICNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgoKI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZG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oPT0nLScpe2Y9LTE7Y2g9Z2V0Y2hhcigpO313aGlsZSAoaXNkaWdpdChjaCkpe3g9KHg8PDEpKyh4PDwzKStjaC0nMCc7Y2g9Z2V0Y2hhcigpO31yZXR1cm4geCpmO31pbmxpbmUgdm9pZCBPVVQobGwgeCl7aWYgKHg8MCkgcHV0Y2hhcignLScpLHg9LXg7aWYgKHg+PTEwKSBPVVQoeC8xMCkscHV0Y2hhcih4JTEwKycwJyk7ZWxzZSBwdXRjaGFyKHgrJzAnKTt9CiNkZWZpbmUgaW9zIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CgojZGVmaW5lIGludCBsb25nIGxvbmcKCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICNkZWZpbmUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGludCBsb25nIGxvbmcKI2RlZmluZSBOZXZlckdpdmVVcCBtYWluCgojZGVmaW5lIE1lbXNldCh4LCB5LCBuKSBmb3IgKGludCBtbXN0ID0gMTsgbW1zdCA8PSBuOyBtbXN0KyspIHhbbW1zdF0gPSB5OwoKI2RlZmluZSBzcCBzeXN0ZW0oInBhdXNlIik7CgojZGVmaW5lIEZuYW1lICJpbnZlcnNlIgojZGVmaW5lIFJvY2t5QmFsYm9hCi8vI2RlZmluZSBUTlQKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp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lmICh0bCA9PSB0cikgdFt2XSA9IHZhbDsKICAgICAgICBlbHNlewogICAgICAgICAgICBpbnQgdG0gPSB0bCArIHRyID4+IDE7CiAgICAgICAgICAgIGlmIChwb3MgPD0gdG0pIFVwZGF0ZShwb3MsIHZhbCwgdiArIHYsIHRsLCB0bSk7CiAgICAgICAgICAgIGVsc2UgVXBkYXRlKHBvcywgdmFsLCB2ICsgdiArIDEsIHRtICsgMSwgdHIpOwogICAgICAgICAgICB0W3ZdLkYgPSB0W3YgKyB2XS5GICsgdFt2ICsgdiArIDFdLkY7CiAgICAgICAgfQogICAgfQogICAgaW5saW5lIGludCBHZXQoaW50IGwsIGludCByLCBpbnQgdiA9IDEsIGludCB0bCA9IDEsIGludCB0ciA9IHN6KXsKICAgICAgICBpZiAobCA8PSB0bCAmJiB0ciA8PSByKSBydCB0W3ZdLkY7CiAgICAgICAgaWYgKHRsID4gciB8fCB0ciA8IGwpIHJ0IDA7CiAgICAgICAgaW50IHRtID0gdGwgKyB0ciA+PiAxOwogICAgICAgIHJ0IEdldChsLCByLCB2ICsgdiwgdGwsIHRtKSArIEdldChsLCByLCB2ICsgdiArIDEsIHRtICsgMSwgdHIpOwogICAgfQogICAgaW5saW5lIGludCBLdGgoaW50IGssIGludCB2ID0gMSwgaW50IHRsID0gMSwgaW50IHRyID0gc3opewogICAgICAgIGlmICh0bCA9PSB0cikgcnQgdFt2XS5TOwogICAgICAgIGludCB0bSA9IHRsICsgdHIgPj4gMTsKICAgICAgICBpZiAodFt2ICsgdl0uRiA+PSBrKSBydCBLdGgoaywgdiArIHYsIHRsLCB0bSk7CiAgICAgICAgZWxzZSBydCBLdGgoayAtIHRbdiArIHZdLkYsIHYgKyB2ICsgMSwgdG0gKyAxLCB0cik7CiAgICB9Ck5ldmVyR2l2ZVVwKCl7CiAgICBpb3MKICAgIGNpbiA+PiBuOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKXsKICAgICAgICBjaW4gPj4gaW5baV0uR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