#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int LEN = 1 << 14;
const int LOG = 16;
int dp[LEN * LOG];
const int N = 20'001;
const int INF = 1e9 + 47;
VI g[N];
int q[2 * N];
int ans[N];
int c[N];
int val[N];
int idx[N];
int cnt[N];
set<int> ind;
int sz = 1;
void recalc(int i, int j, int v)
{
FOR (k, 0, LEN)
{
dp[i * LEN + k] = min(dp[j * LEN + k], dp[j * LEN + (k ^ c[v])] + val[v]);
}
}
void dfsSZ(int v)
{
cnt[v] = 1;
for (auto to : g[v])
{
dfsSZ(to);
cnt[v] += cnt[to];
}
}
void dfsSolve(int v, int xr = 0)
{
ans[v] = dp[idx[v] * LEN + xr];
if (g[v].empty())
return;
sort(ALL(g[v]), [&](int i, int j)
{
return cnt[i] < cnt[j];
});
int id = idx[v];
FOR (i, 0, SZ(g[v]) - 1)
{
int to = g[v][i];
idx[to] = *ind.begin();
ind.erase(idx[to]);
recalc(idx[to], id, to);
dfsSolve(to, xr ^ c[to]);
ind.insert(idx[to]);
}
int to = g[v].back();
idx[to] = *ind.begin();
ind.erase(idx[to]);
recalc(idx[to], id, to);
ind.insert(id);
dfsSolve(to, xr ^ c[to]);
ind.insert(idx[to]);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cerr << sizeof(cnt) * 8 + sizeof(dp) << '\n';
int n;
cin >> n;
{
stack<int> s;
s.push(0);
FOR (i, 0, n)
{
string t;
cin >> t;
if (t == "ADD")
{
cin >> c[sz] >> val[sz];
g[s.top()].PB(sz);
s.push(sz);
sz++;
}
else
{
s.pop();
}
q[i] = s.top();
}
}
idx[0] = 0;
fill(dp[0] + 1, dp[0] + LEN, INF);
FOR (i, 1, LOG)
ind.insert(i);
dfsSZ(0);
dfsSolve(0);
FOR (i, 0, n)
{
cout << ans[q[i]] << '\n';
}
return 0;
}