#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f, N = 1000011;
const LL mod = 1e9 + 7;
char a[N];
int son[N][2], ck, flag[N], c[N], n, q, x[N];
string s;
int guang_sou(int u, int g) {
a[u] ^= g;
if (u <= n) return a[u];
int x = guang_sou(son[u][0], g ^ flag[son[u][0]]);
int y = guang_sou(son[u][1], g ^ flag[son[u][1]]);
if (a[u] == 2) {
if (x == 0) c[son[u][1]] = 1;
if (y == 0) c[son[u][0]] = 1;
return x & y;
} else {
if (x == 1) c[son[u][1]] = 1;
if (y == 1) c[son[u][0]] = 1;
return x | y;
}
}
void guang_sou_2(int u) {
if (u <= n) return ;
c[son[u][0]] |= c[u];
c[son[u][1]] |= c[u];
guang_sou_2(son[u][0]);
guang_sou_2(son[u][1]);
}
//下方read()
char *p1, *p2, buf[100000];
#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++)
inline int read() {
int x = 0, f = 1;
char ch = nc();
while (ch < 48 || ch > 57) {
if (ch == '-') f = -1;
ch = nc();
}
while (ch >= 48 && ch <= 57) x = x * 10 + ch - 48, ch = nc();
return x * f;
}
//read() 停止x
void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
return;
}
int main() {
getline(cin, s);
n = read();
ck = n;
stack<int>b;
stack<int>w;
for (int i = 1; i <= n; i++) {
x[i] = read();
}
for (int i = 1; i <= s[i]; i++, i++) {
if (s[i] == 'x') {
int x = 0;
i++;
while (s[i] != ' ') {
x = x * 10 - '0';
i++;
}
i--;
b.push(x);
}
if (s[i] == '&') {
int h = b.top();
b.pop();
int u = b.top();
b.pop();
b.push(++ck);
son[i][0] = h;
a[ck] = 2;
son[i][1] = u;
} else if (s[i] == '|') {
int h = b.top();
b.pop();
int u = b.top();
b.pop();
b.push(++ck);
son[i][0] = h;
a[ck] = 3;
son[i][1] = u;
} else if (s[i] == '!') {
flag[b.top()] ^= 1;
}
}
int ans = guang_sou(ck, flag[ck]);
guang_sou_2(ck);
q = read();
while (q--) {
int x;
scanf("%d", &x);
if (c[x] == 1) {
write(ans);
cout<<"\n";
}else{
write(!ans);
cout<<"\n";
// write("\n")
}
}
return 0;
}
//flag[b.top()] ^= 1;