#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 10000;
const int N = 29;
const int read()
{
char ch = getchar();
while (ch<'0' || ch>'9') ch = getchar();
int x = ch - '0';
while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
return x;
}
int T, n, x, sz;
int f[N * 10000][2];
char s[N];
int clear(int x)
{
f[x][0] = f[x][1] = 0; return x;
}
int main()
{
T = read();
while (T--)
{
n = read();
clear(sz = 0);
while (n--)
{
scanf("%s%d", s, &x);
if (s[0] == 'i')
{
int rt = 0;
per(i, N, 0)
{
int y = (x & (1 << i)) ? 1 : 0;
if (!f[rt][y]) f[rt][y] = clear(++sz);
rt = f[rt][y];
}
}
else
{
int rt = 0, ans = 0;
if (s[2] == 'i')
{
per(i, N, 0)
{
int y = (x & (1 << i)) ? 1 : 0;
if (f[rt][y]) rt = f[rt][y];
else rt = f[rt][y ^ 1], ans |= 1 << i;
}
}
else
{
per(i, N, 0)
{
int y = (x & (1 << i)) ? 1 : 0;
if (f[rt][y ^ 1]) rt = f[rt][y ^ 1], ans |= 1 << i;
else rt = f[rt][y];
}
}
printf("%d\n", ans);
}
}
}
return 0;
}