#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
struct Suffix_Tree {
char op;
bool sum;
int l, r;
} tree[N];
int head[N];
string st;
int n, a[N];
stack <int> s;
stack <int> g;
bool b[N];
int p = 0;
bool ans;
void build () {
int v = 0;
for (int i = 0; i < st.size (); i ++ ) {
if (st[i] == 'x') {
while (st[i + 1] >= '0' && st[i + 1] <= '9') {
i ++ ;
v = v * 10 + st[i] - '0';
}
s.push (a[v]);
g.push (v);
p ++ ;
head[v] = p;
tree[p].sum = a[v];
v = 0;
}
if (st[i] == '&') {
int x = s.top (); s.pop ();
int y = s.top (); s.pop ();
int x_laber = g.top (); g.pop ();
int y_laber = g.top (); g.pop ();
++ p;
tree[p].op = '&';
tree[p].sum = x & y;
tree[p].l = x_laber; tree[p].r = y_laber;
s.push (x & y);
g.push (p);
}
if (st[i] == '|') {
int x = s.top (); s.pop ();
int y = s.top (); s.pop ();
int x_laber = g.top (); g.pop ();
int y_laber = g.top (); g.pop ();
++ p;
tree[p].op = '|';
tree[p].sum = x | y;
tree[p].l = x_laber; tree[p].r = y_laber;
s.push (x | y);
g.push (p);
}
if (st[i] == '!') {
int asdf = s.top (); s.pop ();
int u = g.top (); g.pop ();
asdf = !asdf;
tree[u].sum = asdf;
s.push (asdf);
g.push (u);
}
}
}
bool dfs (int fa, int x) {
int l = tree[x].l, r = tree[x].r;
if (fa != 0) {
int bro;
if (tree[fa].l == x) bro = tree[fa].r;
else bro = tree[fa].l;
if (b[fa] == 1) {
if (tree[x].sum == 1 && tree[bro].sum == 0 && tree[fa].op == '&') b[x] = 0;
if (tree[x].sum == 1 && tree[bro].sum == 1 && tree[fa].op == '&') b[x] = 1;
if (tree[x].sum == 0 && tree[bro].sum == 0 && tree[fa].op == '&') b[x] = 0;
if (tree[x].sum == 0 && tree[bro].sum == 1 && tree[fa].op == '&') b[x] = 1;
if (tree[x].sum == 1 && tree[bro].sum == 0 && tree[fa].op == '|') b[x] = 1;
if (tree[x].sum == 1 && tree[bro].sum == 1 && tree[fa].op == '|') b[x] = 0;
if (tree[x].sum == 0 && tree[bro].sum == 0 && tree[fa].op == '|') b[x] = 1;
if (tree[x].sum == 0 && tree[bro].sum == 1 && tree[fa].op == '|') b[x] = 0;
}
else b[x] = 0;
}
if(l != 0 && r != 0 && l != fa && r != fa) {
dfs (x, l);
dfs (x, r);
}
}
int main () {
getline (cin, st);
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
build ();
int T;
cin >> T;
ans = tree[p].sum;
b[p] = 1;
dfs (0, p);
while (T -- ) {
int w;
cin >> w;
if (b[head[w]]) cout << !ans << endl;
else cout << ans << endl;
}
return 0;
}