#include<bits/stdc++.h>
using namespace std;
stack <int> t;
int n,q,len;
int a[100001],r[100001];
string s;
int main()
{
int i,j,o;
getline(cin,s);
len=s.size();
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cin>>q;
for(i=1;i<=q;i++)
{
cin>>r[i];
}
int e=0;
for(i=0;i<len;i++)
{
if(s[i]=='x')
{
int x;
x=int(s[i+1]-'0');
s[i]=char(a[x]+'0');
s[i+1]=' ';
}
}
for(int k=1;k<=q;k++)
{
e=0;
for(i=0;i<len;i++)
{
if(s[i]>='0' && s[i]<='9')
{
e++;
}
if(e==r[k])
{
o=i;
if(s[i]=='1')
{
s[i]='0';
}
else if(s[i]=='0')
{
s[i]='1';
}
break;
}
}
for(i=0;i<len;i++)
{
if(s[i]>='0' && s[i]<='9')
{
int x;
x=int(s[i]-'0');
t.push(x);
}
else if(s[i]=='&')
{
int x,y;
x=t.top();
t.pop();
y=t.top();
t.pop();
if(x==1 && y==1)
{
t.push(1);
}
else
{
t.push(0);
}
}
else if(s[i]=='|')
{
int x,y;
x=t.top();
t.pop();
y=t.top();
t.pop();
if(x==1 || y==1)
{
t.push(1);
}
else
{
t.push(0);
}
}
else if(s[i]=='!')
{
int x;
x=t.top();
t.pop();
if(x==1)
{
t.push(0);
}
else
{
t.push(1);
}
}
}
int x;
x=t.top();
t.pop();
cout<<x<<endl;
if(s[o]=='1')
{
s[o]='0';
}
else if(s[o]=='0')
{
s[o]='1';
}
}
}