#include<bits/stdc++.h>
using namespace std;
string s;
int n,root,q,x;
map<int,int> a;
map<int,int> b;
map<int,int> f;
map<int,char> c;
void init(){
stack<int> p;
for(int i=0;i<s.size();i++){
if(s[i]=='x'){
i++;
int x=0;
while(s[i]>='0'&&s[i]<='9'){
x=x*10+s[i]-'0';
i++;
}
p.push(x);
}
if(s[i]=='&'){
int x=p.top();
p.pop();
int y=p.top();
p.pop();
a[++n]=a[x]&a[y];
b[n]=a[x]+a[y];
f[x]=f[y]=n;
c[n]='&';
p.push(n);
}
if(s[i]=='|'){
int x=p.top();
p.pop();
int y=p.top();
p.pop();
a[++n]=a[x]|a[y];
b[n]=a[x]+a[y];
f[x]=f[y]=n;
c[n]='|';
p.push(n);
}
if(s[i]=='!'){
int x=p.top();
a[x]=!a[x];
}
}
root=p.top();
}
int check(int u){
if(u==root){
return a[root];
}
if(c[f[u]]=='&'){
if(b[f[u]]==2||(b[f[u]]==1&&a[u]==0)){
int x=a[f[u]];
a[f[u]]=1;
if(check(f[u])){
a[f[u]]=x;
return 1;
}else{
a[f[u]]=x;
return 0;
}
}else{
int x=a[f[u]];
a[f[u]]=0;
if(check(f[u])){
a[f[u]]=x;
return 1;
}else{
a[f[u]]=x;
return 0;
}
}
}
if(c[f[u]]=='|'){
if(b[f[u]]>=1||(b[f[u]]==0&&a[u]==0)){
int x=a[f[u]];
a[f[u]]=1;
if(check(f[u])){
a[f[u]]=x;
return 1;
}else{
a[f[u]]=x;
return 0;
}
}else{
int x=a[f[u]];
a[f[u]]=0;
if(check(f[u])){
a[f[u]]=x;
return 1;
}else{
a[f[u]]=x;
return 0;
}
}
}
}
inline void solve(){
getline(cin,s);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
init();
cin>>q;
while(q--){
cin>>x;
if(x==root){
a[root]=!a[root];
}
cout<<check(x)<<'\n';
if(x==root){
a[root]=!a[root];
}
}
}
int main(){
int t=1;
for(int i=1;i<=t;i++){
solve();
}
return 0;
}