Problem:
Problem D:
Restrictive Filesystem
あなたは新型記録媒体の開発チームに所属するプログラマーである．この記録媒体はデータの読み込み及び消去はランダムアクセスが可能である．一方，データを書き込むときは，常に先頭から順番にアクセスしていき，最初に見つかった空き領域にしか書き込むことができない．
あなたはこの記録媒体用のファイルシステムの構築を始めた．このファイルシステムでは，記録媒体の制限から，データは先頭の空き領域から順に書き込まれる．書き込みの途中で別のデータが存在する領域に至った場合は，残りのデータをその後ろの空き領域から書き込んでいく．
データの書き込みはセクタと呼ばれる単位で行われる．セクタには 0 から始まる番号が割り当てられており，この番号は記憶媒体上の物理的な位置を指す．セクタ番号は，記憶媒体の先頭から後方に向かって順に 0，1，2，3，… と割り当てられている．
ファイルシステムには，書き込み，削除，セクタの参照という 3 つのコマンドが存在する．
あなたの仕事はこのファイルシステムの挙動を再現した上で，参照コマンドが実行されたとき対象セクタにはどのファイルが配置されているか出力するプログラムを書くことである．なお，初期状態では記録媒体には何も書き込まれていない．
例えば，Sample Input の最初の例を見てみよう．最初の命令では 0 という識別子を持ったサイズが 2 であるファイルを書き込む．初期状態では記録媒体には何も書き込まれていない，すなわち全てのセクタが空き領域であるから，先頭にある 2 つのセクタ，すなわち 0 番目のセクタと 1 番目のセクタに書き込みが行われる．したがって，書き込みの後の記憶媒体は次のようになっている．
０ ０ 空 空 空 空 空 空 …
2 番目の命令によって，1 という識別子を持つファイルが 2 番目と 3 番目のセクタに書き込まれる．この後の記憶媒体の状態は次のようになる．
０ ０ １ １ 空 空 空 空 …
3 番目の命令によって，0 の識別子を持つファイルが削除される．記憶媒体の状態は次のようになる．
空 空 １ １ 空 空 空 空 …
4 番目の命令によって，2 という識別子を持つファイルを 0 番目，1 番目，4 番目，5 番目のセクタに書き込む．
２ ２ １ １ ２ ２ 空 空 …
最後の命令では，3 番目のセクタが参照される．いま，3 番目のセクタには 1 という識別子のファイルが配置されているので，あなたのプログラムは 1 と出力しなければならない．
Input
入力は複数のデータセットからなる．各データセットは次の形式で与えられる．
N
Command
1
Command
2
...
Command
N
N
は実行されるコマンドの数 (1 ≤
N
≤ 10,000)，
Command
i
は
i
番目に実行されるコマンドをそれぞれ表す．
各コマンドは，コマンド名と 1 つまたは 2 つの引数からなる．コマンド名は 1 つの文字のみからなり，「W」，「D」，「R」のいずれかである．コマンドと引数の間，および引数と引数の間はそれぞれ 1 つのスペースで区切られる．
「W」は書き込みのコマンドを表す．2 つの引数
I
(0 ≤
I
≤ 10
9
) と
S
(1 ≤
S
≤ 10
9
) が与えられる．それぞれ，書き込むファイルの識別子とそのファイルを記憶するのに必要とするセクタの数を示す．
「D」は削除のコマンドを表す．1 つの引数
I
(0 ≤
I
≤ 10
9
) が与えられる．削除するファイルの識別子を示す．
「R」は参照のコマンドを表す．1 つの引数
P
(0 ≤
P
≤ 10
9
) が与えられる．参照するセクタの番号を示す．
10
9
よりも大きな番号を持つセクタにはアクセスする必要はないと仮定してよい．また，同じファイル識別子を持つファイルを複数回書き込むことはないことが保証されている．
入力の終わりは，1 つの 0 を含む 1 行で示される．
Output
各データセットについて，参照のコマンドが現れるごとに，そのコマンドによって参照されたファイルの識別子を 1 行に出力せよ．参照したセクタにファイルが書き込まれていなかった場合は，ファイル識別子の代わりに -1 を出力せよ．
各データセットの後には空行を入れること．
Sample Input
6
W 0 2
W 1 2
D 0
W 2 4
R 3
R 1
1
R 1000000000
0
Output for the Sample Input
1
2

-1

Submitted solutions:

code_snippet: #include <set>
#include <cstdio>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int Q, x, y; char b[4];
int main() {
	while (scanf("%d", &Q), Q) {
		vector<vector<int> > v;
		while (Q--) {
			scanf("%s", b);
			if (b[0] == 'W') {
				scanf("%d%d", &x, &y);
				for (int i = -1; i + 1 < v.size() && y > 0; i++) {
					int r1 = (i != -1 ? v[i][1] : 0), r2 = v[i + 1][0];
					if (r1 == r2) continue;
					if (r2 - r1 < y) {
						v.insert(v.begin() + 1 + i, { r1, r2, x });
						y -= r2 - r1;
					}
					else {
						v.insert(v.begin() + 1 + i, { r1, r1 + y, x });
						y = 0;
					}
				}
				if (y > 0) {
					if (v.size() == 0) v.push_back({ 0, y, x });
					else v.push_back({ v.back()[1], v.back()[1] + y, x });
				}
			}
			else if (b[0] == 'D') {
				scanf("%d", &x);
				vector<vector<int> > w;
				for (int i = 0; i < v.size(); i++) {
					if (v[i][2] != x) w.push_back(v[i]);
				}
				v = w;
			}
			else {
				scanf("%d", &x);
				int ret = -1;
				for (int i = 0; i < v.size(); i++) {
					if (v[i][0] <= x && x < v[i][1]) ret = v[i][2];
				}
				printf("%d\n", ret);
			}
		}
		printf("\n");
	}
	return 0;
}
accuracy: 1
time_ms: 2590
memory_kb: 3212
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;


// 書きかけ
template<typename T>
struct interval_set {
    interval_set() {
        s.emplace(-inf, -inf);
        s.emplace(inf, inf);
    }
    template<typename A, typename D>
    void insert(T l, T r, const A &add, const D &del) {
        if (l == r) return;
        auto it = key(l);
        if (it->l <= l && r <= it->r) return;
        if (it->l <= l && l <= it->r) {
            del(it->l, it->r);
            l = it->l;
            it = s.erase(it);
        } else {
            it = next(it);
        }
        while (it->r < r) {
            del(it->l, it->r);
            it = s.erase(it);
        }
        if (it->l <= r) {
            del(it->l, it->r);
            r = it->r;
            s.erase(it);
        }
        add(l, r);
        s.emplace(node(l, r));
    }
    void insert(T l, T r) {
        insert(l, r, [] (T, T) {}, [] (T, T) {});
    }
    template<typename A, typename D>
    void insert(T k, const A &add, const D &del) {
        insert(k, k + 1, add, del);
    }
    void insert(T k) {
        insert(k, k + 1);
    }
    template<typename A, typename D>
    void erase(T l, T r, const A &add, const D &del) {
        if (l == r) return;
        auto it = key(l);
        if (it->l <= l && r <= it->r) {
            if (it->l < l) {
                add(it->l, l);
                s.emplace(node(it->l, l));
            }
            if (r < it->r) {
                add(r, it->r);
                s.emplace(node(r, it->r));
            }
            del(it->l, it->r);
            s.erase(it);
            return;
        }
        if (it->l <= l && l < it->r) {
            if (it->l < l) {
                add(it->l, l);
                s.emplace(it->l, l);
            }
            del(it->l, it->r);
            it = s.erase(it);
        } else {
            it = next(it);
        }
        while (it->r <= r) {
            del(it->l, it->r);
            it = s.erase(it);
        }
        if (it->l < r) {
            add(r, it->r);
            s.emplace(r, it->r);
            del(it->l, it->r);
            s.erase(it);
        }
    }
    void erase(T l, T r) {
        erase(l, r, [] (T, T) {}, [] (T, T) {});
    }
    template<typename A, typename D>
    void erase(T k, const A &add, const D &del) {
        erase(k, k + 1, add, del);
    }
    void erase(T k) {
        erase(k, k + 1);
    }
    T mex(T k = 0) const {
        auto it = key(k);
        return it->l <= k && k < it->r ? it->r : k;
    }
    bool covered(T l, T r) const {
        if (l == r) return true;
        auto it = key(l);
        return it->l <= l && r <= it->r;
    }
    bool covered(T k) const {
        return covered(k, k + 1);
    }
    pair<T, T> get(T k) const {
        auto it = key(k);
        return it->l <= k && k < it->r ? make_pair(it->l, it->r) : make_pair(-inf, -inf);
    }
    vector<pair<T, T>> intervals() const {
        vector<pair<T, T>> res;
        res.reserve(s.size());
        for (auto [l, r] : s) {
            if (abs(l) != inf && abs(r) != inf) res.emplace_back(l, r);
        }
        return res;
    }
    void clear() {
        s = set<node>();
        s.emplace(-inf, -inf);
        s.emplace(inf, inf);
    }
    void debug() const {
        for (auto [l, r] : intervals()) {
            cout << '(' << l << ',' << r << ')';
        }
        cout << endl;
    }
private:
    static constexpr T inf = numeric_limits<T>::max();
    struct node {
        T l, r;
        node() : node(-inf, -inf) {}
        node(T _l, T _r) : l(_l), r(_r) {}
        constexpr bool operator<(const node &a) const {
            return l == a.l ? r < a.r : l < a.l;
        }
    };
    set<node> s;
    typename set<node>::iterator key(T k) const {
        return prev(s.upper_bound(node(k, inf)));
    }
};

// https://noimi.hatenablog.com/entry/2021/05/02/195143?_ga=2.186800042.36757101.1619952706-2024379568.1619952706

template<typename T, typename S>
struct interval_map {
    interval_map(const S &_e = S()) : e(_e) {
        s.emplace(node(-inf, -inf, e));
        s.emplace(node(inf, inf, e));
    }
    // for i in [l, r): a[i] <- x
    // when s.insert(node(l, r, x)), call add(l, r, x)
    // when s.erase(node(l, r, x)), call del(l, r, x)
    template<typename A, typename D>
    void update(T l, T r, const S &x, const A &add, const D &del) {
        auto it = s.lower_bound(node(l, 0, x));
        while (it != s.end() && it->l <= r) {
            if (it->l == r) {
                if (it->x == x) {
                    del(r, it->r, x);
                    r = it->r;
                    it = s.erase(it);
                }
                break;
            }
            if (it->r <= r) {
                del(it->l, it->r, it->x);
                it = s.erase(it);
            } else {
                if (it->x == x) {
                    r = it->r;
                    del(it->l, it->r, it->x);
                    it = s.erase(it);
                } else {
                    del(it->l, r, it->x);
                    auto a = *it;
                    it = s.erase(it);
                    it = s.emplace_hint(it, r, a.r, a.x);
                }
            }
        }
        if (it != s.begin()) {
            it = prev(it);
            if (it->r == l) {
                if (it->x == x) {
                    del(it->l, it->r, it->x);
                    l = it->l;
                    it = s.erase(it);
                }
            } else if (l < it->r) {
                if (it->x == x) {
                    del(it->l, it->r, it->x);
                    l = min(l, it->l);
                    r = max(r, it->r);
                    it = s.erase(it);
                } else {
                    if (r < it->r) {
                        it = s.emplace_hint(next(it), r, it->r, it->x);
                        it = prev(it);
                    }
                    del(l, min(r, it->r), it->x);
                    auto a = *it;
                    it = s.erase(it);
                    it = s.emplace_hint(it, a.l, l, a.x);
                }
            }
            if (it != s.end()) it = next(it);
            add(l, r, x);
            s.emplace_hint(it, l, r, x);
        }
    }
    void update(T l, T r, const S &x) {
        update(l, r, x, [] (T, T, S) {}, [] (T, T, S) {});
    }
    template<typename A, typename D>
    void update(T k, const S &x, const A &add, const D &del) {
        update(k, k + 1, x, add, del);
    }
    void update(T k, const S &x) {
        update(k, k + 1, x);
    }
    // return (left, right, val) s.t. left <= k < right
    tuple<T, T, S> get(T k) const {
        auto it = key(k);
        return it->l <= k && k < it->r ? node(k, k + 1, e) : make_tuple(it->l, it->r, it->x);
    }
    // val at k
    S operator[](T k) const {
        return key(k)->x;
    }
    // return (left, right, val) s.t. r = min(k < r)
    tuple<T, T, S> mex(T k) const {
        auto it = key(k);
        return make_tuple(it->l, it->r, it->x);
    }
private:
    static constexpr T inf = numeric_limits<T>::max();
    struct node {
        T l, r;
        S x;
        node() : node(-inf, -inf, e) {}
        node(T _l, T _r, const S &_x) : l(_l), r(_r), x(_x) {}
        constexpr bool operator<(const node &a) const {
            return l == a.l ? r < a.r : l < a.l;
        }
    };
    const S e;
    set<node> s;
    typename set<node>::iterator key(T k) const {
        return prev(s.upper_bound(node(k, inf, e)));
    }
};

bool solve() {
    int Q;
    cin >> Q;
    if (Q == 0) return false;
    interval_set<int> A, B;
    map<int, interval_set<int>> mp;
    interval_map<int, int> im;
    const int M = 1e9 + 10;
    A.insert(0, M);
    im.update(0, M, -1);
    int ept = M;
    while (Q--) {
        char C;
        cin >> C;
        if (C == 'W') {
            int L, S;
            cin >> L >> S;
            S = min(S, ept);
            int P = 0;
            while (S > 0) {
                P = B.mex(P);
                int K = A.mex(P) - P;
                K = min(K, S);
                B.insert(P, P + K);
                A.erase(P, P + K);
                mp[L].insert(P, P + K);
                im.update(P, P + K, L);
                S -= K;
                P += K;
                ept -= K;
            }
        } else if (C == 'D') {
            int L;
            cin >> L;
            for (auto [l, r] : mp[L].intervals()) {
                A.insert(l, r);
                B.erase(l, r);
                ept += r - l;
                im.update(l, r, -1);
            }
            mp[L].clear();
        } else {
            int P;
            cin >> P;
            cout << im[P] << '\n';
        }
    }
    cout << '\n';
    return true;
}

int main() {
    while (solve()) {}
}
accuracy: 1
time_ms: 1010
memory_kb: 4864
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
typedef unsigned long long ull;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)
#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)
#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)
#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)
#define each(p,a) for(auto &p:a)
typedef vector<ll> vll;
typedef vector<pair<ll ,ll>> vpll;
typedef vector<vector<ll>> vvll;
typedef set<ll> sll;
typedef map<ll , ll> mpll;
typedef pair<ll ,ll> pll;
typedef tuple<ll , ll , ll> tpl3;
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)
#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)
#define all(a)  (a).begin(),(a).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define sz(x) (ll)x.size()
// << std::fixed << std::setprecision(10)
const ll INF = 1LL << 60;
const ld EPS = 1e-9;
 
ll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}
ll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}
inline ll positive_mod(ll a,ll m){return (a % m + m)%m;}
inline ll popcnt(ull a){ return __builtin_popcountll(a);}
//0indexed
inline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}
inline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}
template<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}
template<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}
template<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}
template<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?" ":"");}return os;}
template<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << "{" << p.first << "," << p.second << "}";return os;}
template<class... T>void input(T&... a){(cin >> ... >> a);}
void print(){cout << endl;}
template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}
template<class T> void pspace(const T& a){ cout << a << ' ';}
void perr(){cerr << endl;}
template<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}
void yes(bool i = true){ return print(i?"yes":"no"); }
void Yes(bool i = true){ return print(i?"Yes":"No"); }
void YES(bool i = true){ return print(i?"YES":"NO"); }
template <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}
template <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}
template <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}
template <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}
//grid探索用
vector<ll> _ta = {0,0,1,-1,1,1,-1,-1};
vector<ll> _yo = {1,-1,0,0,1,-1,1,-1};
bool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}
  
ll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}
ll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} 
const ll MOD9 = 998244353LL;
const ll MOD10 = 1000000007LL;


//ref https://atcoder.jp/contests/abc256/submissions/32647023
/*
verify
insert(l,r),printSet https://atcoder.jp/contests/adt_all_20250520_1/submissions/66027406
getRangeLength, insert(l,r) https://yukicoder.me/submissions/1087100
mex,insert(x) erase(x) https://atcoder.jp/contests/abc330/submissions/66027280
*/
template<class T>
struct RangeSet{
  set<pair<T,T>> se;
  T TINF;

  RangeSet(T tinf):TINF{tinf}{ //TINFの値を定義する
    se.emplace(-TINF,-TINF);
    se.emplace(TINF,TINF);
  }
  T insert(T l,T r) {//半開区間
    auto itl = se.upper_bound({l,TINF}), itr = se.upper_bound({r,TINF});
    if (itl != se.begin() && (--itl)->second < l) itl++;
    T ret = T{0};
    T minus = T{0};
    if (itl != itr) {
        l = min(l , itl->first);
        r = max(r, prev(itr)->second);
        minus += itl->second - itl->first;
        minus += itr->second - itr->first;
        se.erase(itl,itr);
    }
    se.insert({l,r});
    ret += r-l;
    ret -= minus;
    return ret;
  }

  T insert(T x){
    return insert(x,x+1);
  }

  bool count(T x){
    auto it = prev(se.lower_bound({x+1,x+1}));
    return (it->first <= x && x < it->second);
  }

  //ref https://mugen1337.github.io/procon/DataStructure/RangeSet.hpp
  //減少量(正の値)を返す
  T erase(T l,T r){ 
    assert(l<r);
    auto ite=prev(se.lower_bound({l+1,l+1}));
    if(ite->first<=l and r<=ite->second){
      // 完全に1つの区間に包含されている
      if(ite->first<l)  se.emplace(ite->first,l);
      if(r<ite->second) se.emplace(r,ite->second);
      se.erase(ite);
      return r-l;
    }

    T ret=T(0);
    if(ite->first<=l and l<=ite->second){
      ret += ite->second -l;
      if(ite->first<l) se.emplace(ite->first,l);
      ite=se.erase(ite);// 次へ
    }else ite=next(ite);

    while(ite->second<=r){
      ret += ite->second - ite->first;
      ite=se.erase(ite);
    }
    // 右端が区間の間にあるか
    if(ite->first<=r and r<ite->second){
      ret += r-ite->first;
      se.emplace(r,ite->second);
      se.erase(ite);
    }
    return ret;
  }

  T erase(T x){
    return erase(x,x+1);
  }
  // number of range
  int size(){
    return (int)se.size()-2;
  }
  T getRangeLength(T x){
    auto it = prev(se.lower_bound({x+1,x+1}));
    if(it->first <= x && x < it->second){
      return it->second - it->first;
    }else{
      return T{0};
    }
  }
  vector<pair<T,T>> printSet(){
    vector<pair<T,T>>ret;
    for(const auto&[l,r]:se){
      if(l == -TINF || l == TINF)continue;
      ret.emplace_back(l,r);
    }
    return ret;
  }

  T mex(T x = 0){
    auto [nowf,nows] = *prev(se.lower_bound({x+1,x+1}));
    if(nowf <=x  && x < nows){
      return nows;
    }else{
      return x;
    }
  }
};

void solve(ll n){
  RangeSet<ll> se(INF);

  vector<RangeSet<ll>>mp;
  mp.reserve(n+1);
  vll mpid(n+1,-1);

  while(n--){
    Ch(type);
    if(type == 'W'){
      //追加していく
      LL(x,num) ;
      ll id = sz(mp);
      mp.push_back(RangeSet<ll>(INF));
      mpid[id] = x;
      while(num > 0){
        ll l = se.mex(0);
        ll r = se.se.lower_bound(pll{l,l})->first;
        if(r-l >num){
          mp[id].insert(l,l+num);
          se.insert(l,l+num);
          break;
        }else{
          mp[id].insert(l,r);
          se.insert(l,r);
          num -= r-l;
        }
      }
    }else if(type == 'D'){
      LL(x);
      rep(i,sz(mp)){
        if(mpid[i] == x){
          vpll ret = mp[i].printSet();
          for(auto &[l,r]:ret){
            se.erase(l,r);
          }
          mp[i] = RangeSet<ll>(INF);//初期化
          break;
        }
      }
    }else if(type == 'R'){
      //愚直に見る
      LL(x);
      bool found = false;
      rep(z,sz(mp)){
        if(mpid[z] == -1){
          break;
        }
        if(mp[z].count(x)){
          found = true;
          cout << mpid[z] << endl;
          break;
        }
      }
      if(!found){
        cout << -1 << endl;
      }
    }
  }
  cout << endl;
}
 
int main(){
  ios::sync_with_stdio(false);cin.tie(nullptr);
  while(true){
    LL(n);
    if(n == 0)break;
    solve(n);
  }
  


}
accuracy: 1
time_ms: 590
memory_kb: 4740
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)
#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name
#define REP1(n) for(ll i=0;i<(n);i++)
#define REP2(i, n) for (ll i=0;i<(n);i++)
#define REP3(i, a, n) for (ll i=a;i<(n);i++)
#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name
#define RREP1(n) for(ll i=(n)-1;i>=0;i--)
#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)
#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)
#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))
#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)
#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define len(n) (long long)(n).size()
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vc = vector<char>;
using vvc = vector<vc>;
using vvvc = vector<vvc>;
using pll = pair<ll,ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

ll intpow(ll a,ll b){
    ll ans = 1;
    while (b){
        if (b & 1){
            ans *= a;
        }
        a *= a;
        b /= 2;
    }
    return ans;
}
ll modpow(ll a,ll b,ll c){
    ll ans = 1;
    while (b){
        if (b & 1){
            ans *= a;
            ans %= c;
        }
        a *= a;
        a %= c;
        b /= 2;
    }
    return ans;
}

template<class... T>
void input(T&... a){
    (cin >> ... >> a);
}

#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)
#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)
#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}
#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}
#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}
#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}
#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}
#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define PLL(name) pll name;cin>>name.first>>name.second;
#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}

void print(){cout << "\n";}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
    os << "[";
    for (size_t i = 0; i < vec.size(); ++i) {
        os << vec[i];
        if (i + 1 < vec.size()) os << ", ";
    }
    os << "]";
    return os;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {
    os << "[";
    for (size_t j = 0; j < a.size(); ++j) {
        os << "(" << a[j].first << ", " << a[j].second << ")";
		if (j + 1 < a.size()) os << ", ";
    }
	os << "]";
    return os;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {
	os << "[";
    for (size_t i = 0; i < mat.size(); ++i) {
		os << "[";
        for (size_t j = 0; j < mat[i].size(); ++j) {
            os << "(" << mat[i][j].first << ", " << mat[i][j].second << ")";
			if (j + 1 < mat[i].size()) os << ", ";
        }
		os << "]";
		if (i + 1 < mat.size()) os << ", ";
    }
	os << "]";
    return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
    os << "{";
    bool first = true;
    for (const auto& x : s) {
        if (!first) os << ", ";
        os << x;
        first = false;
    }
    os << "}";
    return os;
}

template <typename K, typename V>
std::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {
    os << "{";
    bool first = true;
    for (const auto& [key, val] : m) {
        if (!first) os << ", ";
        os << key << ": " << val;
        first = false;
    }
    os << "}";
    return os;
}

template<class T, class... Ts>
void print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}

void write(){cout << "\n";}
template<class T, class... Ts>
void write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}
void write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(pll x){cout << x.first << ' ' << x.second << '\n';}
void write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\n';}}
void write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}

template <typename T>
T sum(const std::vector<T>& v) {
    return std::accumulate(v.begin(), v.end(), T(0));
}

template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }

using vvvvld = vector<vvvld>;
using S = tuple<ld,ll,ll,ll>;
int main(){
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
    ll oo = 0;
    while(1){
        LL(n);
        if(n == 0){break;}
        //if(oo){print();}
        oo = 1;
        set<pll> s;
        s.emplace(0,10000000000LL);
        map<ll,vpll> mp;
        rep(i,n){
            CHA(c);
            if(c == 'W'){
                LL(l,x);
                while(x){
                    auto y = *(s.begin());
                    s.erase(s.find(y));
                    ll left = y.first;
                    ll right = y.second;
                    if(right - left > x){
                        mp[l].push_back({left,left+x});
                        
                        s.emplace(left+x,right);
                        x = 0;
                    }
                    else{
                        mp[l].push_back({left,right});
                        x -= right - left;
                    }
                }
            }
            else if(c == 'D'){
                LL(l);
                for(auto [left,right]:mp[l]){
                    s.emplace(left,right);
                }
                mp[l].clear();
                set<pll> t;
                for(auto [left,right]:s){
                    if(t.size() == 0){
                        t.emplace(left,right);
                        continue;
                    }
                    auto y = *(prev(s.end()));
                    if(y.second == left){
                        t.erase(s.find(y));
                        t.emplace(y.first,right);
                    }
                    else{
                        t.emplace(left,right);
                    }
                }
                swap(s,t);
            }
            else{
                LL(x);
                
                ll ans = -1;
                for(auto [k,v]:mp){
                    for(auto [left,right]:v){
                        if(left <= x && x < right){
                            ans = k;
                            break;
                        }
                    }
                }
                
                print(ans);
            }
            //cout << mp << endl;
            //cout << s << endl;
        }
        print();
    }
    
}
accuracy: 1
time_ms: 710
memory_kb: 42496
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
while(1) {
    int N;
    cin >> N;
    if (N == 0) return 0;
    vector<pair<int,int>> V;
    V.push_back({0,-1}), V.push_back({inf,-1});
    rep(_,0,N) {
        char C;
        cin >> C;
        if (C == 'W') {
            int I, S;
            cin >> I >> S;
            int L = V.size();
            vector<pair<int,int>> NewV;
            rep(i,0,L) {
                if (V[i].second == -1 && S > 0) {
                    if (V[i+1].first - V[i].first <= S) {
                        S -= (V[i+1].first - V[i].first);
                        NewV.push_back({V[i].first, I});
                    }
                    else {
                        NewV.push_back({V[i].first, I});
                        NewV.push_back({V[i].first + S, -1});
                        S = 0;
                    }
                }
                else {
                    NewV.push_back(V[i]);
                }
            }
            swap(V,NewV);
        }
        if (C == 'D') {
            int I;
            cin >> I;
            vector<pair<int,int>> NewV;
            rep(i,0,V.size()) {
                if (V[i].second == I) NewV.push_back({V[i].first, -1});
                else NewV.push_back(V[i]);
            }
            swap(V,NewV);
        }
        if (C == 'R') {
            int P;
            cin >> P;
            rep(i,0,V.size()-1) {
                if (V[i].first <= P && P < V[i+1].first) {
                    cout << V[i].second << endl;
                }
            }
        }
    }
    cout << endl;
}
}
accuracy: 1
time_ms: 100
memory_kb: 3560
final_rank: 7

code_snippet: #include<bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;

#define all(v) v.begin(),v.end()
using ll = long long;
using ull = unsigned long long;
using vll=vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll,ll>;
using vp=vector<pair<ll, ll>>;
//using mint=modint1000000007;
//using mint=modint998244353;

const ll INF=1ll<<60;
ll mod10=1e9+7;
ll mod99=998244353;
const double PI = acos(-1);

#define rep(i,n) for (ll i=0;i<n;++i)
#define per(i,n) for(ll i=n-1;i>=0;--i)
#define rep2(i,a,n) for (ll i=a;i<n;++i)
#define per2(i,a,n) for (ll i=a;i>=n;--i)

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }

bool solve(){
    ll N;cin>>N;
    if(N==0) return 0;
    vector<string> C(N);
    vll L(N),S(N);
    rep(i,N){
        cin>>C[i];
        if(C[i]=="W") cin>>S[i]>>L[i];
        else cin>>L[i];
    }

    rep(i,N){
        if(C[i]=="R"){
            map<ll,ll> mp;
            ll ans=-1;
            ll sum=0;
            rep(j,i){
                if(C[j]=="W"){
                    if(sum<L[i]+1){
                        mp[S[j]]=min(L[j],L[i]+1-sum);
                        sum+=min(L[j],L[i]+1-sum);
                        if(sum==L[i]+1&&ans==-1){
                            ans=S[j];
                        }
                    }else mp[S[j]]=0;
                }else if(C[j]=="D"){
                   
                    if(L[j]==ans){
                        ans=-1;
                        sum-=mp[L[j]];
                    }else sum-=mp[L[j]];
                }

             
            }
            cout<<ans<<endl;
        }
    }

    cout  << endl;
    
    return 1;
}



int main(){
   cin.tie(0);
   ios::sync_with_stdio(false);
   while(solve());
}
accuracy: 1
time_ms: 1850
memory_kb: 3860
final_rank: 18

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

void solve(int N) {
    vector<pair<int,int>> A;
    for(int i : rep(N)) {
        char type = in();
        if(type == 'W') {
            int I = in(), S = in();
            vector<pair<int,int>> nA;
            for(auto &[len, data] : A) {
                if(S > 0 and data == -1) {
                    int x = min(S, len);
                    nA.push_back({x, I});
                    S -= x;
                    len -= x;
                    if(len > 0) nA.push_back({len, data});
                } else {
                    nA.push_back({len, data});
                }
            }
            if(S > 0) nA.push_back({S, I});
            A = move(nA);
        }

        if(type == 'D') {
            int I = in();
            vector<pair<int,int>> nA;
            for(auto [len, data] : A) {
                if(data == I) {
                    nA.push_back({len, -1});
                } else {
                    nA.push_back({len, data});
                }
            }
            A = move(nA);
        }

        if(type == 'R') {
            int P = in();
            int sum = 0, printed = 0;
            for(auto [len, data] : A) {
                if(sum <= P and P < sum + len) {
                    print(data);
                    printed = 1;
                    break;
                }
                sum += len;
            }
            if(not printed) print(-1);
        }
    }
}

int main() {
    while(true) {
        int N = in();
        if(N == 0) return 0;
        solve(N);
        print();
    }
}
accuracy: 1
time_ms: 170
memory_kb: 3504
final_rank: 8

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void setp(int n) {
    std::cout << std::fixed << std::setprecision(n);
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 3 "a.cpp"

int main(void) {
    while (true) {
        int q;
        cin >> q;
        if (!q)
            break;
        vector<pair<int, int>> dp;
        while (q--) {
            char c;
            cin >> c;
            if (c == 'W') {
                int x, y;
                cin >> x >> y;
                int idx = 0;
                bool flag = false;
                for (auto &p : dp) {
                    if (p.second == -1) {
                        if (y < p.first - idx) {
                            dp.emplace(lower_bound(all(dp), p), idx + y, x);
                            flag = true;
                            break;
                        }
                        y -= p.first - idx;
                        p.second = x;
                        if (y == 0) {
                            flag = true;
                            break;
                        }
                    }
                    idx = p.first;
                }
                if (!flag)
                    dp.emplace_back(idx + y, x);
            } else if (c == 'D') {
                int x;
                cin >> x;
                for (auto &p : dp) {
                    if (p.second == x)
                        p.second = -1;
                }
            } else {
                int x;
                cin >> x;
                bool flag = false;
                for (auto p : dp) {
                    if (x < p.first) {
                        co(p.second);
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                    co(-1);
            }
        }
        cout << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3252
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

void output(const map<int, int>& mp) {
    int l = 0;
    cout << "---------------------" << endl;
    for (const auto& [r, id] : mp) {
        cout << l << ' ' << r << " : " << id << endl;
        l = r;
    }
    cout << "---------------------" << endl;
}

bool solve() {
    int N; cin >> N;
    if (N == 0) return false;

    map<int, int> mp;
    
    for (int _ = 0 ; _ < N ; _++) {
        char c; cin >> c;
        if (c == 'W') {
            int id, size; cin >> id >> size;
            int l = 0;
            for (auto [r, p] : mp) {
                if (size == 0) break;
                if (p == -1) {
                    int len = r - l;
                    if (len <= size) { // 全部塗りつぶされる
                        mp[r] = id;
                        size -= len;
                    }
                    else { // 区間が分割される
                        mp.erase(r);
                        mp[l + size] = id;
                        mp[r] = -1;
                        size = 0;
                    }
                }
                l = r;
            }
            if (size > 0) {
                mp[l + size] = id;
            }
        }
        else if (c == 'D') {
            int id; cin >> id;
            for (auto& [_, p] : mp) if (p == id) p = -1;
        }
        else if (c == 'R') {
            int p; cin >> p;
            bool flg = false;
            for (auto& [r, id] : mp) if (p < r) {
                cout << id << endl;
                flg = true;
                break;
            }
            if (!flg) {
                cout << -1 << endl;
            }
        }
        else {
            assert(!"input false");
        }
        // output(mp);
    }

    return true;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    while (solve()) {
        cout << endl;
    }
}
accuracy: 1
time_ms: 290
memory_kb: 3720
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;

#define foa(s, v) for (auto &s : v)
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define rep(i, n) for (int i = 0; i < int(n); i++)

template <class T>
bool chmin(T &a, T b)
{
    return a > b ? a = b, 1 : 0;
}
template <class T>
bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; }

int n;
void solve()
{
    const ll rightmax = 1e9 + 7;
    set<pair<ll, ll>> remain;
    remain.emplace(0LL, rightmax);

    map<int, vector<pair<ll, ll>>> alc;

    set<tuple<ll, ll, int>> filled;

    auto write = [&](int idx, ll len) -> void
    {
        auto it = remain.begin();
        vector<pair<ll, ll>> res;
        while (len > 0)
        {
            ll itlen = it->second - it->first;
            if (len >= itlen)
            {
                len -= itlen;
                res.push_back(*it);
                it = remain.erase(it);
            }
            else
            {
                // len < itlen
                ll l, r;
                tie(l, r) = *it;
                res.emplace_back(l, l + len);
                l += len;
                remain.erase(it);
                remain.emplace(l, r);
                len = 0;
            }
        }

        for (auto [l, r] : res)
        {
            filled.emplace(l, r, idx);
        }

        alc[idx] = res;
    };

    auto del = [&](int idx) -> void
    {
        for (auto p : alc[idx])
        {
            tuple<ll, ll, int> tp(p.first, p.second, idx);
            filled.erase(tp);
            remain.insert(p);
        }
        alc.erase(idx);
    };

    auto read = [&](ll pos) -> int
    {
        tuple<ll, ll, int> tp(pos + 1, -1, -1);
        auto it = filled.lower_bound(tp);
        if (it == filled.begin())
            return -1;
        it--;
        ll l, r;
        int idx;
        tie(l, r, idx) = *it;
        return l <= pos && pos < r ? idx : -1;
    };

    rep(i, n)
    {
        char c;
        cin >> c;
        switch (c)
        {
        case 'W':
        {
            int idx;
            ll len;
            cin >> idx >> len;
            write(idx, len);
            break;
        }

        case 'R':
        {
            ll pos;
            cin >> pos;
            cout << read(pos) << endl;
            break;
        }

        case 'D':
        {
            int idx;
            cin >> idx;
            del(idx);
            break;
        }

        default:
            assert(false);
            break;
        }
    }
    cout << endl;
    return;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    while (cin >> n && n)
        solve();
}
accuracy: 1
time_ms: 430
memory_kb: 4100
final_rank: 11

code_snippet: #include<bits/stdc++.h>
#define int ll
#define rep(i, N) for(int i = 0; i < (int)(N); ++i)
#define rep1(i, N) for(int i = 1; i <= (int)(N); ++i)
#define per(i, N) for(int i = (N)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bit(n, k) ((n) >> (k))
#define pcnt(n) (__builtin_popcount(n))
#define TakAo(ans) ans ? cout << "Takahashi\n" : cout << "Aoki\n"
#define YesNo(ans) ans ? cout << "Yes\n" : cout << "No\n"
#define endl '\n'
#define fi first
#define se second
#define mkpr make_pair
#define mktpl make_tuple
#define eb emplace_back

using namespace std;
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using point = struct{ ll x, y; };
using pointld = struct{ ld x, y; };
template<class T> using max_heap = priority_queue<T>;
template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vec<vec<T>>;
template<class T> using vvvec = vec<vvec<T>>;
template<class T> using vvvvec = vvec<vvec<T>>;

constexpr ld EPS = 1e-10;
constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr ll MOD = (1) ? 998244353 : 1e9+7;
constexpr int NIL = -1;
constexpr int pm[2] = {1, -1};
constexpr int dy[4] = {0, 1, 0, -1};
constexpr int dx[4] = {1, 0, -1, 0};

ll cel(ll a, ll b){ return (a + b - 1) / b;}
ll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : abs(a);}
template<class T> T powi(T x, ll exp){
    return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : x / x;
}
ll modpow(ll x, ll exp, ll mod){
    x %= mod;
    return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;
}
template<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}
template<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}

using Pair = pair<ll, ll>;
using Tpl = tuple<char, int, int>;

void Main(){
    while(1){
        int N; cin >> N;
        if(N == 0) break;

        vec<Tpl> cmd(N);
        vec<int> vals;
        rep(i, N){
            char c; cin >> c;
            int A = NIL, B = NIL;
            if(c == 'W'){
                cin >> A >> B;
            }
            else cin >> A;

            if(c != 'R'){
                vals.eb(A);
            }
            cmd[i] = mktpl(c, A, B);
        }

        ll id = 0;
        sort(all(vals));
        vals.erase(unique(all(vals)), vals.end());
        vec<int> ids(vals.size());

        rep(i, N){
            if(get<0>(cmd[i]) != 'R'){
                int num = lower_bound(all(vals), get<1>(cmd[i])) - vals.begin();
                ids[num] = get<1>(cmd[i]);
                cmd[i] = mktpl(get<0>(cmd[i]),
                               num,
                               get<2>(cmd[i]));
            }
        }

        vec<Pair> sect; 
        sect.eb(mkpr(0, NIL));
        rep(i, N){
            Tpl T = cmd[i];
            char c = get<0>(T);
            int sz = sect.size();

            if(c == 'W'){
                int I = get<1>(T), S = get<2>(T);
                for(int j = 0; S > 0 && j < sz; j++){
                    if(j == sz - 1){
                        sect[j].se = I;
                        sect.eb(mkpr(sect[j].fi + S, NIL));
                        break;
                    }
                    if(sect[j].se != NIL) continue;

                    int sub = sect[j + 1].fi - sect[j].fi;
                    if(S < sub){
                        sect[j].se = I;
                        sect.insert(sect.begin() + j + 1, mkpr(sect[j].fi + S, NIL));
                        break;
                    }
                    else{
                        sect[j].se = I;
                        S -= sub;
                    }
                }
            }
            if(c == 'D'){
                int I = get<1>(T);
                rep(j, sz){
                    if(sect[j].se == I) sect[j].se = NIL;
                }
            }
            if(c == 'R'){
                int P = get<1>(T);
                auto it = upper_bound(all(sect), mkpr(P, INF));
                if(it == sect.begin()){
                    cout << -1 << endl;
                    continue;
                }
                
                it--;
                //cout << (*it).se << endl;
                int num = (*it).se;
                if(0 <= num && num < (int)ids.size()){
                    cout << ids[num] << endl;
                }
                else{
                    cout << num << endl;
                }
            }
        }

        rep(j, 0){
            cout << sect[j].fi << " " << sect[j].se << endl;
        }
        cout << endl;
    }
}
 
signed main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    Main();
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3624
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#include <complex>
#include <random>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vvvvvll = vector<vvvvll>;
using vvvvvvll = vector<vvvvvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vvvvb = vector<vvvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vvvvd = vector<vvvd>;
using vvvvvd = vector<vvvvd>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;
#define tN(t) (t==1?XN:YN)
#define tS(t) (t==1?XS:YS)
#define tA(t) (t==1?XA:YA)

template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}
ll sqrtz(ll N) {
    ll L = 0;
    ll R = sqrt(N) + 10000;
    while (abs(R - L) > 1) {
        ll mid = (R + L) / 2;
        if (mid * mid <= N)L = mid;
        else R = mid;
    }
    return L;

}

vector<ll> fact, factinv, inv;
ll mod = 998244353;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact.at(0) = fact.at(1) = 1;
    factinv.at(0) = factinv.at(1) = 1;
    inv.at(1) = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact.at(i) = (fact.at(i - 1) * i) % mod;
        inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
        factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
    }

}
ll nCk(ll n, ll k) {
    if (n < k) return 0;
    return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}
ll modPow(ll a, ll n, ll p) {
    ll res = 1;
    ll k = a;
    while (n > 0) {
        if (n % 2 == 1) {
            res *= k;
            if (res > p)res %= p;
        }
        k = k * k;
        if (k > p)k %= p;
        n /= 2;
    }
    return res;
}

ll inva(ll N) {
    ll a = N, b = mod, c = 1, d = 0;
    while (b > 0) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        c -= t * d;
        swap(c, d);

    }
    c %= mod;
    if (c < 0)c += mod;
    return c;

}
string S;
string num;

ll has(ll l, ll r) {
    if ('a' <= S[l] && S[l] <= 'd') {
        return ll(num[S[l] - 'a'] - '0');
    }
    else if (S[l] == '[') {
        return has(l + 1, r - 1);
    }
    else {
        ll a, b;
        ll res = 0;
        if ('a' <= S[l + 1] && S[l + 1] <= 'd') {
            a = ll(num[S[l + 1] - 'a'] - '0');
            res = l + 1;
        }
        else {
            ll k = 0;
            for (ll i = l + 1; i < r; i++) {
                if (S[i] == '[')k++;
                else if (S[i] == ']') {
                    k--;
                    if (k == 0) {
                        a = has(l + 1, i + 1);
                        res = i;
                        break;
                    }
                }
            }
        }

        if ('a' <= S[res + 1] && S[res + 1] <= 'd') {
            b = ll(num[S[res + 1] - 'a'] - '0');
            res = l + 1;
        }
        else {
            ll k = 0;
            for (ll i = res + 1; i < r; i++) {
                if (S[i] == '[')k++;
                else if (S[i] == ']') {
                    k--;
                    if (k == 0) {
                        b = has(res + 1, i + 1);
                        res = i;
                        break;
                    }
                }
            }
        }

        if (S[l] == '+')return (a | b);
        else if (S[l] == '*')return (a & b);
        else if (S[l] == '^')return (a ^ b);

    }
    return 0;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    while (1) {
        ll N;
        cin >> N;
        if (N == 0)return 0;
        set<pair<pair<ll, ll>, ll>> S;
        S.insert({ {-1,-1},-1 });
        S.insert({ {1e18,1e18},-1 });
        rep(i, N) {
            char C;
            cin >> C;
            if (C == 'W') {
                ll A, B;
                cin >> A >> B;
                auto q = S.begin();
                auto p = *q;
                vector<pair<pair<ll, ll>, ll>> INSE;
                while (B > 0) {
                    q++;
                    auto np = *q;
                    if (p.first.second + 1 < np.first.first) {
                        ll siz = min(B, np.first.first - p.first.second - 1);
                        INSE.push_back({ {p.first.second + 1,p.first.second + siz},A });
                        B -= siz;
                    }
                    p = *q;
                }
                for (auto v : INSE)S.insert(v);
            }
            else if (C == 'D') {
                vector<pair<pair<ll, ll>, ll>> INSE;
                ll A; cin >> A;
                for (auto v : S) {
                    if (v.second == A)INSE.push_back(v);
                }
                for (auto v : INSE) {
                    S.erase(v);
                }
            }
            else {
                ll A;
                cin >> A;
                bool OK = 0;
                for (auto v : S) {
                    if (v.first.first <= A && v.first.second >= A) {
                        cout << v.second << endl;
                        OK = 1;
                        break;
                    }
                }
                if (!OK)cout << -1 << endl;
            }
        }
        cout << endl;
    }
}
accuracy: 1
time_ms: 450
memory_kb: 3668
final_rank: 10

code_snippet: #include<bits/stdc++.h>
#define int ll
#define rep(i, N) for(int i = 0; i < (int)(N); ++i)
#define rep1(i, N) for(int i = 1; i <= (int)(N); ++i)
#define per(i, N) for(int i = (N)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bit(n, k) ((n) >> (k))
#define pcnt(n) (__builtin_popcount(n))
#define TakAo(ans) ans ? cout << "Takahashi\n" : cout << "Aoki\n"
#define YesNo(ans) ans ? cout << "Yes\n" : cout << "No\n"
#define endl '\n'
#define fi first
#define se second
#define mkpr make_pair
#define mktpl make_tuple
#define eb emplace_back

using namespace std;
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using point = struct{ ll x, y; };
using pointld = struct{ ld x, y; };
template<class T> using max_heap = priority_queue<T>;
template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vec<vec<T>>;
template<class T> using vvvec = vec<vvec<T>>;
template<class T> using vvvvec = vvec<vvec<T>>;

constexpr ld EPS = 1e-10;
constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr ll MOD = (1) ? 998244353 : 1e9+7;
constexpr int NIL = -1;
constexpr int pm[2] = {1, -1};
constexpr int dy[4] = {0, 1, 0, -1};
constexpr int dx[4] = {1, 0, -1, 0};

ll cel(ll a, ll b){ return (a + b - 1) / b;}
ll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : abs(a);}
template<class T> T powi(T x, ll exp){
    return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : x / x;
}
ll modpow(ll x, ll exp, ll mod){
    x %= mod;
    return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;
}
template<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}
template<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}

using Pair = pair<ll, ll>;
using Tpl = tuple<char, int, int>;

void Main(){
    while(1){
        int N; cin >> N;
        if(N == 0) break;

        vec<Tpl> cmd(N);
        vec<int> vals;
        rep(i, N){
            char c; cin >> c;
            int A = NIL, B = NIL;
            if(c == 'W'){
                cin >> A >> B;
            }
            else cin >> A;

            if(c != 'R'){
                vals.eb(A);
            }
            cmd[i] = mktpl(c, A, B);
        }

        ll id = 0;
        sort(all(vals));
        vals.erase(unique(all(vals)), vals.end());
        vec<int> ids(vals.size());

        rep(i, N){
            if(get<0>(cmd[i]) != 'R'){
                int num = lower_bound(all(vals), get<1>(cmd[i])) - vals.begin();
                ids[num] = get<1>(cmd[i]);
                cmd[i] = mktpl(get<0>(cmd[i]),
                               num,
                               get<2>(cmd[i]));
            }
        }

        vec<Pair> sect; 
        sect.eb(mkpr(0, NIL));
        rep(i, N){
            Tpl T = cmd[i];
            char c = get<0>(T);
            int sz = sect.size();

            if(c == 'W'){
                int I = get<1>(T), S = get<2>(T);
                for(int j = 0; S > 0 && j < sz; j++){
                    if(j == sz - 1){
                        sect[j].se = I;
                        sect.eb(mkpr(sect[j].fi + S, NIL));
                        break;
                    }
                    if(sect[j].se != NIL) continue;

                    int sub = sect[j + 1].fi - sect[j].fi;
                    if(S < sub){
                        sect[j].se = I;
                        sect.insert(sect.begin() + j + 1, mkpr(sect[j].fi + S, NIL));
                        break;
                    }
                    else{
                        sect[j].se = I;
                        S -= sub;
                    }
                }
            }
            if(c == 'D'){
                int I = get<1>(T);
                rep(j, sz){
                    if(sect[j].se == I) sect[j].se = NIL;
                }
            }
            if(c == 'R'){
                int P = get<1>(T);
                auto it = upper_bound(all(sect), mkpr(P, INF));
                if(it == sect.begin()){
                    cout << -1 << endl;
                    continue;
                }
                
                it--;
                //cout << (*it).se << endl;
                int num = (*it).se;
                if(0 <= num && num < (int)ids.size()){
                    cout << ids[num] << endl;
                }
                else{
                    cout << num << endl;
                }
            }
        }

        rep(j, 0){
            cout << sect[j].fi << " " << sect[j].se << endl;
        }
        cout << endl;
    }
}
 
signed main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    Main();
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3756
final_rank: 3

code_snippet: #include<bits/stdc++.h>
#include <streambuf>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;

    void print();
    template <class t> void print(const vector<t> &);
    template <class t, class u> void print(const pair<t, u> &);
    template <class t> void print(const t&);
    template <class Head, class... Tail> void print(const Head&, const Tail&...);

    template <class t> void println(const vector<vector<t>>&);
    template <class t> void println(const vector<t>&);
    template <class t> void println(const t&);
    template <class Head, class... Tail> void println(const Head&, const Tail&...);
    void println();
    void newline();

    void print(){
        return;
    }

    template <class t>
    void print(const vector<t>&x){
        for(const t&i:x)print(i);
    }
    template <class t, class u>
    void print(const pair<t,u>&p){
        print(p.first);
        print(p.second);
    }
    template <class t>
    void print(const t&x){
        if(was_output)cout<<" ";
        cout<<x;
        was_output = true;
    }
    template <class Head, class... Tail>
    void print(const Head&head,const Tail&...tail){
        print(head);
        print(tail...);
    }

    template<class t>
    void println(const vector<vector<t>>&x){
        for(vector<t> i:x)println(i);
    }
    template<class t>
    void println(const vector<t>&x){
        for(const t&i:x)print(i);
        println();
    }
    template <class t>
    void println(const t&x){
        print(x);
        println();
    }
    void println(){
        if(was_output){
            cout << endl;
            was_output = false;
        }
    }
    template <class Head, class... Tail>
    void println(const Head&head,const Tail&...tail){
        print(head);
        println(tail...);
    }

    void newline(){
        was_output = true;
        println();
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }

    class output_initializer{
    public:
        output_initializer(){
            ios::sync_with_stdio(false);
            cin.tie(nullptr);
            cout << setprecision(20);
        }
    };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();
}

using namespace templates;

class sector{
public:
    ll size;
    int num;
    sector *next;
    sector(int num,ll size):num(num),size(size),next(nullptr){}
    sector(ll size):num(-1),size(size),next(nullptr){}
    sector(int num,ll size,sector *next):num(num),size(size),next(next){}
};

int reader(sector *head,ll p){
    if(head==nullptr)return -1;
    if(p < head->size)return head->num;
    return reader(head->next,p-head->size);
}

sector* writer(sector *head,int num,ll size){
    if(size==0)return head;
    if(head->num < 0){
        if(head->size < size){
            head->num = num;
            head->next = writer(head->next,num,size-head->size);
        }else if(head->size > size){
            sector *next = new sector(-1,head->size - size, head->next);
            head->next = next;
            head->size = size;
            head->num = num;
        }else{
            head->num = num;
        }
        return head;
    }
    head->next = writer(head->next,num,size);
    return head;
}

sector* deleter(sector *head,int num){
    if(head==nullptr)return nullptr;
    if(head->num == num){
        head->num = -1;
        while(head->next != nullptr and head->next->num == -1){
            sector *next = head->next;
            head->size += next->size;
            head->next = next->next;
            delete next;
        }
        head->next = deleter(head->next,num);
        return head;
    }
    head->next = deleter(head->next,num);
    return head;
}

void delete_all(sector *head){
    if(head->next != nullptr)delete_all(head->next);
    delete head;
}

vector<int> func(int n){
    sector* head = new sector(1e18);
    vector<int> res;
    rep(_,n){
        char c = in<char>();
        if(c=='W'){
            int num = in();
            int size = in();
            head = writer(head,num,size);
        }
        if(c=='R'){
            int p = in();
            res.emplace_back(reader(head,p));
        }
        if(c=='D'){
            int kind = in();
            head = deleter(head,kind);
        }
    }
    delete_all(head);
    return res;
}

int main(){
    while(true){
        int n=in();
        if(n==0)break;
        foreach(i,func(n)){
            println(i);
        }
        newline();
    }
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3496
final_rank: 5

code_snippet: #include <stdio.h>
#include <map>
#include <unordered_map>
#include <vector>
using namespace std;

map<int, pair<int, int>> space; //st, ed, file
unordered_map<int, vector<int>> file;

int main(void) {
    int n, a, b, s;
    char o;
    pair<int, pair<int, int>> node;
    //freopen("E:\\PS\\Contest\\Japanese Alumni Group\\2009D\\D\\D1", "r", stdin);
    //freopen("E:\\PS\\Contest\\Japanese Alumni Group\\2009D\\D\\D1_t.out", "w", stdout);
    while (1) {
        scanf("%d", &n);
        if (n == 0) break;
        for (int i = 0; i < n; i++) {
            scanf(" %c", &o);
            if (o == 'W') {
                scanf("%d %d", &a, &b);
                s = 0;
                while (b > 0) {
                    if (space.lower_bound(s) == space.end()) {
                        space.insert({ s, {s + b - 1, a} });
                        if (file.find(a) != file.end()) file[a].push_back(s);
                        else file.insert({ a, {s} });
                        break;
                    }
                    node = *space.lower_bound(s);
                    if (node.first > s) {
                        if (node.first - s > b) {
                            space.insert({ s, {s + b - 1, a} });
                            b = 0;
                        }
                        else {
                            space.insert({ s, {node.first - 1, a} });
                            b -= node.first - s;
                        }
                        if (file.find(a) != file.end()) file[a].push_back(s);
                        else file.insert({ a, {s} });
                    }
                    s = node.second.first + 1;
                }
            }
            if (o == 'D') {
                scanf("%d", &a);
                for (int i : file[a]) {
                    space.erase(i);
                }
                file.erase(a);
            }
            if (o == 'R') {
                scanf("%d", &a);
                if (space.size() == 0) {
                    printf("-1\n");
                    continue;
                }
                if (space.upper_bound(a) != space.begin() &&
                    a <= (space.upper_bound(a).operator--())->second.first) {
                    printf("%d\n", (space.upper_bound(a).operator--())->second.second);
                }
                else printf("-1\n");
            }
        }
        printf("\n");
        space.clear();
        file.clear();
    }
    return 0;
}
accuracy: 1
time_ms: 1000
memory_kb: 3408
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

int M = 1000000005;

void solve(int N) {
    set<pair<pair<int, int>, int>> S;
    S.insert({{0, M}, -1});  // {{start, siz}, id}
    rep(i, N) {
        char type;
        cin >> type;
        if (type == 'W') {
            int id, siz;
            cin >> id >> siz;
            int cs = 0;
            auto it = S.begin();
            vector<pair<pair<int, int>, int>> v;
            while (cs < siz) {
                if ((*it).second == -1) {
                    cs += (*it).first.second;
                    v.push_back(*it);
                }
                it++;
            }
            for (auto vi : v) S.erase(vi);
            cs = 0;
            for (auto vi : v) {
                int rem = siz - cs;
                if (vi.first.second <= rem) {
                    S.insert({vi.first, id});
                } else {
                    S.insert({{vi.first.first, rem}, id});
                    S.insert({{vi.first.first + rem, vi.first.second - rem}, -1});
                }
                cs += vi.first.second;
            }

        } else if (type == 'D') {
            int id;
            cin >> id;
            vector<pair<pair<int, int>, int>> v;
            for (auto &si : S) {
                if (si.second == id) {
                    v.push_back(si);
                }
            }
            for (auto &vi : v) {
                S.erase(vi);
                S.insert({vi.first, -1});
            }
        } else {
            // R query
            int loc;
            cin >> loc;
            for (auto &si : S) {
                if (si.first.first <= loc && loc < si.first.first + si.first.second) {
                    cout << si.second << '\n';
                    break;
                }
            }
        }
    }
    cout << '\n';
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    while (cin >> N, N) solve(N);
    return 0;
}
accuracy: 1
time_ms: 720
memory_kb: 3848
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
int n;

struct Sec{
    int l;
    int r;
    int i;
};

void solve(){
    char com;
    int i, s, p;
    vector<Sec> seg;
    
    while(n--){
        cin >> com;
        if(com == 'W'){
            cin >> i >> s;
            if(seg.empty()){
                seg.push_back({0, s, i});
            }
            else{
                vector<Sec> seg2;
                int rr = 0;
                for(auto sec: seg){
                    if(rr < sec.l){
                        int d = sec.l - rr;
                        if(d <= s){
                            seg2.push_back({rr, rr + d, i});
                            s -= d;
                        }
                        else if(s > 0){
                            seg2.push_back({rr, rr + s, i});
                            s = 0;
                        }
                    }
                    rr = sec.r;
                    seg2.push_back(sec);
                }
                if(s > 0){
                    seg2.push_back({rr, rr + s, i});
                }
                seg = seg2;
            }
        }
        else if(com == 'D'){
            cin >> i;
            vector<Sec> seg2;
            for(auto sec: seg){
                if (sec.i != i){
                    seg2.push_back(sec);
                }
            }
            seg = seg2;
        }
        else{
            cin >> p;
            bool ng = true;
            for(auto sec: seg){
                if(sec.l <= p && p < sec.r){
                    cout << sec.i << "\n";
                    ng = false;
                    break;
                }
            }
            if(ng) cout << "-1\n";
        }
    }
    cout << "\n";
}

int main(void){
    while(1){
        cin >> n;
        if(n == 0) break;
        solve();
    }
    
}
accuracy: 1
time_ms: 70
memory_kb: 3376
final_rank: 4

code_snippet: #include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>
#include <limits>
#include <cassert>
#include <bitset>
#include <chrono>
#include <random>
#include <memory>
 
/* macro */
 
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (i64 i = (i64)(a); i< (i64)(n); i++)
#define RRep(i, a, n) for (i64 i = ((i64)(n-i64(1))); i>=(i64)(a); i--)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define Bit(n) (1LL<<(n))
 
/* macro end */
 
/* template */
 
namespace ebi {
 
#ifdef LOCAL
#define debug(...)                                                    \
    std::cerr << "LINE: " << __LINE__ << "  [" << #__VA_ARGS__ << "]:", \
        debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
 
void debug_out() { std::cerr << std::endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
    std::cerr << " " << h;
    if (sizeof...(t) > 0) std::cout << " :";
    debug_out(t...);
}
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
    return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
    for (std::size_t i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    return os;
}
 
using size_t = std::size_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using ld = long double;
 
template<class T> void fill(std::vector<T> &v) { for(T &e: v) std::cin >> e; }
 
template <class T>
inline bool chmin(T &a, T b) {
    if (a > b){
        a = b;
        return true;
    }
    return false;
}
 
template <class T>
inline bool chmax(T &a, T b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
 
template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    else return gcd(b, a%b);
}
 
template <class T>
T lcm(T a, T b) {
    a /= gcd(a, b);
    return a*b;
}
 
template<class T>
T Pow(T x, i64 n) {
    T res = 1;
    while(n>0){
        if(n&1) res = res *x;
        x = x*x;
        n>>=1;
    }
    return res;
}
 
template<class T>
T scan() {
    T val;
    std::cin >> val;
    return val;
}
 
constexpr i64 LNF = std::numeric_limits<i64>::max()/4;
 
constexpr int INF = std::numeric_limits<int>::max()/2;
 
const std::vector<int> dy = {1,0,-1,0,1,1,-1,-1};
const std::vector<int> dx = {0,1,0,-1,1,-1,1,-1};
 
}
 
/* template end */

namespace ebi {

template<class T>
struct section_set {
private:
    std::set<std::pair<T,T>> set;
public:
    section_set() {
        set.insert({std::numeric_limits<T>::min(), std::numeric_limits<T>::min()});
        set.insert({std::numeric_limits<T>::max(), std::numeric_limits<T>::max()});
    }

    // [l, r)を追加
    void insert(T l, T r) {
        auto itr = std::prev(set.lower_bound({l, std::numeric_limits<T>::min()}));
        if(l <= itr->second) {
            assert(itr->first <= l);
            l = itr->first;
            r = std::max(r, itr->second);
            set.erase(itr);
        }
        itr = set.lower_bound({l, -1});
        while(itr->first <= r) {
            assert(l <= itr->first);
            r = std::max(r, itr->second);
            itr = set.erase(itr);
        }
        set.insert({l, r});
        return;
    }

    // 集合内の[l, r)に含まれている要素を削除
    void erase(T l, T r) {
        auto itr = std::prev(set.lower_bound({l, std::numeric_limits<T>::min()}));
        if(l < itr->second) {
            assert(itr->first < l);
            set.insert({itr->first ,l});
            if(r < itr->second) {
                set.insert({r, itr->second});
            }
            set.erase(itr);
        }
        itr = set.lower_bound({l, std::numeric_limits<T>::min()});
        while(itr->first < r) {
            if(itr->second <= r) {
                itr = set.erase(itr);
            }
            else {
                set.insert({r, itr->second});
                set.erase(itr);
                break;
            }
        }
        return;
    }

    bool find(T x) const {
        auto itr = std::prev(set.upper_bound({x, std::numeric_limits<T>::max()}));
        if(x < itr->second) {
            assert(itr->first <= x);
            return true;
        }
        else {
            return false;
        }
    }

    bool find(T l, T r) const {
        auto itr = std::prev(set.upper_bound({l, std::numeric_limits<T>::max()}));
        if(r <= itr->second) {
            assert(itr->first <= l);
            return true;
        }
        else {
            return false;
        }
    }

    std::pair<T,T> lower_bound(T l) const {
        return *set.lower_bound({l, -1});
    }

    void Debug_out() const {
        for(auto [l, r]: set) {
            debug(l, r);
        }
    }
};

}

namespace ebi {

void main_() {
    int n;
    while(std::cin >> n, !(n == 0)) {
        section_set<i64> used, noused;
        noused.insert(0, 1e9+7);
        std::map<i64, std::vector<std::pair<i64,i64>>> map;
        std::map<i64, i64> vmap;
        vmap[std::numeric_limits<i64>::max()] = -1;
        while(n--) {
            char c;
            i64 idx;
            std::cin >> c >> idx;
            if(c == 'W') {
                i64 s;
                std::cin >> s;
                while(s > 0) {
                    auto [l, r] = noused.lower_bound(0);
                    i64 w = std::min(r-l, s);
                    used.insert(l, l+w);
                    map[idx].emplace_back(l, l+w);
                    vmap[l] = idx;
                    noused.erase(l, r);
                    if(w != r-l) {
                        noused.insert(l+w, r);
                    }
                    s -= w;
                }
            }
            else if(c == 'D') {
                for(auto [l, r]: map[idx]) {
                    // debug(l, r);
                    assert(used.find(l, r));
                    used.erase(l, r);
                    vmap.erase(l);
                    noused.insert(l, r);
                }
                map.erase(idx);
            }
            else {
                if(!used.find(idx)) {
                    std::cout << "-1\n";
                    continue;
                }
                auto itr = std::prev(vmap.upper_bound(idx));
                std::cout << itr->second << '\n';
            }
            /*
            debug("used:");
            used.Debug_out();
            debug("unused");
            noused.Debug_out();
            */
        }
        std::cout << '\n';
    }
}

}
 
int main() {
    std::cout << std::fixed << std::setprecision(15);
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t = 1;
    // std::cin >> t;
    while(t--) {
        ebi::main_();
    }
    return 0;
}
accuracy: 1
time_ms: 900
memory_kb: 3840
final_rank: 14

code_snippet: //#define _GLIBCXX_DEBUG

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T> using PQ = priority_queue<T>;
template<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;
template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);};  
template<typename T>void debug(const T &v,ll h,ll w,string sv=" "){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};
template<typename T>void debug(const T &v,ll n,string sv=" "){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};
template<typename T>void debug(const vector<T>&v){debug(v,v.size());}
template<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}
template<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop_front();}cout<<endl;}
template<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
template<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
template<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<" ";cout<<endl;}
template<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<"["<<z.first<<"]="<<z.second<<",";cout<<endl;}
template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;}
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;}
template<typename T>void rearrange(vector<int>&ord, vector<T>&v){
  auto tmp = v;
  for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];
}
template<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){
  rearrange(ord, head);
  rearrange(ord, tail...);
}
template<typename T> vector<int> ascend(const vector<T>&v){
  vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
  sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]<v[j];});
  return ord;
}
template<typename T> vector<int> descend(const vector<T>&v){
  vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
  sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]>v[j];});
  return ord;
}
ll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}
ll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}
ll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}
ll modulo(ll n,ll d){return (n%d+d)%d;};
template<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}
template<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}
template<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};
template<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int popcount(ll x){return __builtin_popcountll(x);};
int poplow(ll x){return __builtin_ctzll(x);};
int pophigh(ll x){return 63 - __builtin_clzll(x);};
template<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};
template<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};
template<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};
template<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};
ll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}
ll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}
ll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}
template< typename T = int >
struct edge {
  int to;
  T cost;
  int id;
  edge():id(-1){};
  edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}
  operator int() const { return to; }
};

template<typename T>
using Graph = vector<vector<edge<T>>>;
template<typename T>
Graph<T>revgraph(const Graph<T> &g){
  Graph<T>ret(g.size());
  for(int i=0;i<g.size();i++){
    for(auto e:g[i]){
      int to = e.to;
      e.to = i;
      ret[to].push_back(e);
    }
  }
  return ret;
}
template<typename T>
Graph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){
  Graph<T> ret(n);
  for(int es = 0; es < m; es++){
    int u,v;
    T w=1;
    cin>>u>>v;u-=indexed,v-=indexed;
    if(weighted)cin>>w;
    ret[u].emplace_back(v,w,es);
    if(!directed)ret[v].emplace_back(u,w,es);
  }
  return ret;
}
template<typename T>
Graph<T> readParent(int n,int indexed=1,bool directed=true){
  Graph<T>ret(n);
  for(int i=1;i<n;i++){
    int p;cin>>p;
    p-=indexed;
    ret[p].emplace_back(i);
    if(!directed)ret[i].emplace_back(p);
  }
  return ret;
}

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll res=0,buf=0;
  bool judge = true;
	while(1){
		ll n;cin>>n;
		if(n==0)break;
		using T=tuple<ll,ll,ll>;
		vector<T>v;
		v.EB(0,0,-1);
		while(n--){
			char c;cin>>c;
			if(c=='W'){
				ll l,s;cin>>l>>s;
				vector<T>nv;
				rep(i,0,v.size()){
					nv.PB(v[i]);
					if(s>0&&i+1<v.size()){
						ll lr=get<1>(v[i]);
						ll rl=get<0>(v[i+1]);
						if(lr<rl){
							nv.EB(lr,min(rl,lr+s),l);
							s=max(0LL,s-(rl-lr));
						}
					}
				}
				ll rr=get<1>(v.back());
				//cout<<"rr" spa rr<<endl;
				if(s>0)nv.EB(rr,rr+s,l);
				v.swap(nv);
			}
			if(c=='D'){
				ll l;cin>>l;
				vector<T>nv;
				for(auto z:v){
					if(get<2>(z)!=l)nv.PB(z);
				}
				v.swap(nv);
			}
			if(c=='R'){
				ll p;cin>>p;
				bool sw=false;
				for(auto [l,r,k]:v){
					if(l<=p&&p<r){
						sw=true;
						cout<<k<<endl;
						break;
					}
				}
				if(!sw)cout<<-1<<endl;
			}
			/*for(auto [l,r,k]:v){
				cout<<n spa l spa r spa k<<endl;
			}*/
		}
		cout<<endl;
	}
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3420
final_rank: 6

code_snippet: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vc> vvc;
typedef vector<vs> vvs;
typedef vector<vll> vvll;
typedef pair<int, int> P;
typedef map<int, int> mii;
typedef set<int> si;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define fin(ans) cout << (ans) << '\n'
#define STLL(s) strtoll(s.c_str(), NULL, 10)
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define fi first
#define se second
// #include <atcoder/all>
// using namespace atcoder;
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T, class U>
inline bool chmax(T& a, U b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T, class U>
inline bool chmin(T& a, U b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T, class U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
    cout << "(" << p.first << ", " << p.second << ")";
    return os;
}
template <class T>
inline void dump(T& v) {
    irep(i, v) { cout << (*i) << ((i == --v.end()) ? '\n' : ' '); }
}
template <class T, class U>
inline void dump(map<T, U>& v) {
    if (v.size() > 100) {
        cout << "ARRAY IS TOO LARGE!!!" << endl;
    } else {
        irep(i, v) { cout << i->first << " " << i->second << '\n'; }
    }
}
template <class T, class U>
inline void dump(pair<T, U>& p) {
    cout << p.first << " " << p.second << '\n';
}
inline void yn(const bool b) { b ? fin("yes") : fin("no"); }
inline void Yn(const bool b) { b ? fin("Yes") : fin("No"); }
inline void YN(const bool b) { b ? fin("YES") : fin("NO"); }
void Case(int i) { printf("Case #%d: ", i); }
const int INF = INT_MAX;
constexpr ll LLINF = 1LL << 61;
constexpr ld EPS = 1e-11;
#define MODTYPE 0
#if MODTYPE == 0
constexpr ll MOD = 1000000007;
#else
constexpr ll MOD = 998244353;
#endif
/* --------------------   ここまでテンプレ   -------------------- */

struct Range {
    int begin;
    int end;
};
bool operator<(const Range& x, const Range& y) {
    if (x.begin != y.begin) {
        return x.begin < y.begin;
    } else {
        return x.end < y.end;
    }
}

map<int, int> id;   // 入力値をidにして小さくする
map<int, int> rid;  // 入力値をidにして小さくする
vector<vector<Range>> rangeData(10010);  // 各idのデータの区間
set<Range> emptyData;
int cnt = 0;

void del(int l) {
    l = id[l];
    for (int i = 0; i < rangeData[l].size(); i++) {
        Range r = rangeData[l][i];
        emptyData.insert(r);
    }
    rangeData[l].clear();
}

void write(int l, int sector) {
    id[l] = cnt;
    rid[cnt] = l;
    l = id[l];
    cnt++;
    vector<Range> eraseList;
    for (auto range : emptyData) {
        if (range.end - range.begin + 1 <= sector) {
            rangeData[l].push_back(range);
            eraseList.pb(range);
            sector -= range.end - range.begin + 1;
        } else {
            eraseList.pb(range);
            Range r1 = {range.begin, range.begin + sector - 1};
            Range r2 = {range.begin + sector, range.end};
            rangeData[l].push_back(r1);
            emptyData.insert(r2);
            sector = 0;
        }
        if (sector == 0) {
            break;
        }
    }
    for (Range range : eraseList) {
        emptyData.erase(range);
    }
}

int read(int p) {
    for (int i = 0; i < rangeData.size(); i++) {
        for (int j = 0; j < rangeData[i].size(); j++) {
            Range r = rangeData[i][j];
            if (r.begin <= p && p <= r.end) {
                return rid[i];
            }
        }
    }
    return -1;
}

int main() {
    while (1) {
        int n;
        cin >> n;
        if (n == 0) break;
        id.clear();
        rid.clear();
        emptyData.clear();
        rangeData = vector<vector<Range>>(10010);
        cnt = 0;
        emptyData.insert(Range{0, 1000000010});
        rep(i, n) {
            char c;
            cin >> c;
            if (c == 'W') {
                int l, s;
                cin >> l >> s;
                write(l, s);
            } else if (c == 'R') {
                int p;
                cin >> p;
                cout << read(p) << endl;
            } else if (c == 'D') {
                int l;
                cin >> l;
                del(l);
            }
            // for (int i = 0; i < rangeData.size(); i++) {
            //     if (rangeData[i].size() > 0) {
            //         cout << i << " : ";
            //         for (int j = 0; j < rangeData[i].size(); j++) {
            //             cout << "(" << rangeData[i][j].begin << " "
            //                  << rangeData[i][j].end << ")";
            //         }
            //         cout << endl;
            //     }
            // }
        }
        cout << endl;
    }
}
accuracy: 1
time_ms: 330
memory_kb: 24536
final_rank: 17