#include<bits/stdc++.h>
#define int long long
using namespace std;
char ch[1000005];
int cnt,in;
char tree[4000005];
int sum[4000005];
bool flag;
int num;
int tag[100005];
int tot;
int n,q;
int a[100005];
int w;
void build(int k)
{
if(ch[tot] == ' ')tot--;
if(isdigit(ch[tot]))
{
cnt = 0;
num = 0;
while(ch[tot]!='x')
{
num += ch[tot] - 48 * pow(10,cnt);
cnt ++;
tot --;
}
tot--;
tag[num] = k;
}
else
{
tree[k] = ch[tot];
tot--;
build(k * 2 + 1);
if(tree[k] != '!')
build(k * 2);
}
if(tot == 0)return;
}
void getsum(int k)
{
if(tree[k] == '0')
{
sum[k] = 0;
return;
}
if(tree[k] == '1')
{
sum[k] = 1;
return;
}
getsum(k * 2 + 1);
if(tree[k] != '!')getsum(k * 2);
if(tree[k] == '&')
{
if(sum[k * 2] == 1 && sum[k * 2 + 1] == 1)sum[k] = 1;
else sum[k] = 0;
return;
}
if(tree[k] == '|')
{
if(sum[k * 2] == 0 && sum[k * 2 + 1] == 0)sum[k] = 0;
else sum[k] = 1;
return;
}
if(tree[k] == '!')
{
if(sum[k * 2 + 1] == 0)sum[k] = 1;
else sum[k] = 0;
return;
}
}
void question(int k,int last)
{
if(k == 0)return;
if(k * 2 != last)
{
if(tree[k] == '&')
{
if(sum[k * 2] == 1 && w == 1)w = 1;
else w = 0;
question(k/2,k);
return;
}
if(tree[k] == '|')
{
if(sum[k * 2] == 0 && w == 0)w = 0;
else w = 1;
question(k/2,k);
return;
}
}
if(tree[k] == '!')
{
if(w == 1)w = 0;
else w = 1;
question(k/2,k);
return;
}
if(tree[k] == '&')
{
if(w == 1 && sum[k * 2 + 1] == 1)w = 1;
else w = 0;
question(k/2,k);
return;
}
if(tree[k] == '|')
{
if(w == 0 && sum[k * 2 + 1] == 0)w = 0;
else w = 1;
question(k/2,k);
return;
}
}
void print(int k)
{
if(tree[k] == '0' || tree[k] == '1')
cout<<tree[k];
else
{
if(tree[k] != '!')print(k * 2);
cout<<tree[k];
print(k * 2 + 1);
}
}
signed main()
{
cin.getline(ch,1000005);
tot = strlen(ch) - 1;
build(1);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
tree[tag[i]] = a[i] + 48;
}
getsum(1);
cin >> q;
while(q--)
{
cin >> in;
if(a[in] == 1)w = 0;
else w = 1;
question(tag[in]/2,tag[in]);
cout<<w<<endl;
}
return 0;
}