#include <bits/stdc++.h>
using namespace std;
namespace q_wr {
inline int read() {
char c = getchar();
int x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
inline void write(int x) {
if (x < 0) {
x = ~(x - 1);
putchar('-');
}
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
}
using namespace q_wr;
const int maxn = 1e5 + 500;
struct segtree {
int ls, rs;
char val;
} tr[maxn * 32];
int n, vs, rt[maxn], cnt, len[maxn];
int modify (int s, int l, int r, int p, char c) {
int o = ++cnt;
tr[o] = tr[p];
if (l == r) {
tr[o].val = c;
return o;
}
int mid = (l + r) >> 1;
if (s <= mid) tr[o].ls = modify(s, l, mid, tr[p].ls, c);
else tr[o].rs = modify(s, mid + 1, r, tr[p].rs, c);
return o;
}
void query (int s, int l, int r, int p) {
if (l == r) {
cout << tr[p].val;
puts("");
return;
}
int mid = (l + r) >> 1;
if (s <= mid) query(s, l, mid, tr[p].ls);
else query(s, mid + 1, r, tr[p].rs);
}
int main () {
n = read();
for (int i = 1; i <= n; i++) {
char opt;
cin >> opt;
if (opt == 'T') {
char c;
cin >> c;
len[++vs] = len[vs - 1] + 1;
// cout << len[vs] << "\n";
rt[vs] = modify(len[vs], 1, n, rt[vs - 1], c);
}
if (opt == 'U') {
int x = read();
len[++vs] = len[vs - x - 1];
rt[vs] = rt[vs - x - 1];
}
if (opt == 'Q') {
int x = read();
query(x, 1, n, rt[vs]);
}
}
}
WA on test #1~10