#include<bits/stdc++.h>
using namespace std;
const int len=1e6;
int n,m,a[2*len+10],s[2*len+10],cnt=0,q;
int f[2*len+10],x,cnt2,ch[2*len+10][2],d[2*len+10],bro[2*len+10],root;
string c;
bool p[2*len+10];
void build(int x)
{
if(d[x]==len+1)
{
build(ch[x][0]);
build(ch[x][1]);
a[x]=a[ch[x][0]]|a[ch[x][1]];
}
if(d[x]==len+2)
{
build(ch[x][0]);
build(ch[x][1]);
a[x]=a[ch[x][0]]&a[ch[x][1]];
}
if(d[x]==len+3)
{
build(ch[x][0]);
a[x]=!a[ch[x][0]];
}
}
void dfs(int x)
{
if(d[f[x]]==len+2)
{
if(a[bro[x]]==0) p[x]=true;
if(a[bro[x]]==1)
{
p[x]=false;
dfs(ch[x][0]);
dfs(ch[x][1]);
}
}
if(d[f[x]]==len+1)
{
if(a[bro[x]]==0)
{
p[x]=false;
dfs(ch[x][0]);
dfs(ch[x][1]);
}
if(a[bro[x]]==1) p[x]=true;
}
if(d[f[x]]==len+3)
{
p[x]=false;
dfs(ch[x][0]);
}
}
int main()
{
freopen("expr.in","r",stdin);
freopen("expr.out","w",stdout);
getline(cin,c);
memset(p,true,sizeof(p));
cin>>n;
cnt2=len;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=0;i<c.size();i++)
{
if(c[i]>='0'&&c[i]<='9') x=x*10+c[i]-'0';
if(c[i]==' '&&c[i-1]>='0'&&c[i-1]<='9') s[++cnt]=x,x=0;
if(c[i]=='|')
{
f[s[cnt]]=++cnt2;
ch[cnt2][0]=s[cnt];
cnt--;
f[s[cnt]]=cnt2;
ch[cnt2][1]=s[cnt];
bro[ch[cnt2][1]]=ch[cnt2][0];
bro[ch[cnt2][0]]=ch[cnt2][1];
cnt--;
d[cnt2]=len+1;
s[++cnt]=cnt2;
root=cnt2;
}
if(c[i]=='&')
{
f[s[cnt]]=++cnt2;
ch[cnt2][0]=s[cnt];
cnt--;
f[s[cnt]]=cnt2;
ch[cnt2][1]=s[cnt];
bro[ch[cnt2][1]]=ch[cnt2][0];
bro[ch[cnt2][0]]=ch[cnt2][1];
cnt--;
d[cnt2]=len+2;
s[++cnt]=cnt2;
root=cnt2;
}
if(c[i]=='!')
{
f[s[cnt]]=++cnt2;
ch[cnt2][0]=s[cnt];
cnt--;
d[cnt2]=len+3;
s[++cnt]=cnt2;
}
}
build(root);
// cout<<a[root]<<endl;
dfs(ch[root][0]);dfs(ch[root][1]);
cin>>q;
while(q--)
{
scanf("%d",&x);
if(p[x]) cout<<a[root]<<endl;
else cout<<!a[root]<<endl;
}
}