class UnionFind: def __init__(self, n): self.n = n self.par = [-1] * n self.group_ = n self.R = [i for i in range(n)] def find(self, x): if self.par[x] < 0: return x lst = [] while self.par[x] >= 0: lst.append(x) x = self.par[x] for y in lst: self.par[y] = x return x def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return False if self.par[x] > self.par[y]: x, y = y, x self.par[x] += self.par[y] self.par[y] = x self.group_ -= 1 self.R[x] = max(self.R[x], self.R[y]) return True def size(self, x): return -self.par[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) @property def group(self): return self.group_ class BIT: def __init__(self, n): self.n = n self.data = [0] * (n + 1) if n == 0: self.n0 = 0 else: self.n0 = 1 << (n.bit_length() - 1) def sum_(self, i): s = 0 while i > 0: s += self.data[i] i -= i & -i return s def sum(self, l, r=-1): if r == -1: return self.sum_(l) else: return self.sum_(r) - self.sum_(l) def get(self, i): return self.sum(i, i + 1) def add(self, i, x): i += 1 while i <= self.n: self.data[i] += x i += i & -i def lower_bound(self, x): if x <= 0: return 0 i = 0 k = self.n0 while k > 0: if i + k <= self.n and self.data[i + k] < x: x -= self.data[i + k] i += k k //= 2 return i + 1 def solve(): n = int(input()) X = list(map(eval, input().split())) Y = input().split() S = list(map(int, input().split())) bit = BIT(n) for i in range(n): bit.add(i, 1) for i, s in enumerate(S): p = bit.lower_bound(s) bit.add(p - 1, -1) S[i] = p - 1 UF = UnionFind(n) for s in S: y = Y[s] u = UF.find(s) v = UF.find(UF.R[u] + 1) UF.unite(u, v) p = UF.find(u) if y == "and": nex = X[u] & X[v] elif y == "or": nex = X[u] | X[v] elif y == "xor": nex = X[u] ^ X[v] else: if X[u]: nex = X[v] else: nex = True X[p] = nex print(X[UF.find(0)]) for _ in range(int(input())): solve()