#include <bits/stdc++.h>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
char s[1005];
int fv[30];
char crt;
int poz;
int sisau();
int notop();
int termen()
{
int rez=0;
if(crt=='(')
{
poz++;
crt=s[poz];
rez=sisau();
poz+=2;
crt=s[poz];
}
else
if(crt=='T'&&s[poz+1]=='R')
{
poz+=5;
crt=s[poz];
rez=1;
}
else
if(crt=='F'&&s[poz+1]=='A')
{
poz+=6;
crt=s[poz];
rez=0;
}
else
{
poz+=2;
crt=s[poz];
rez=fv[s[poz-2]-'A'];
}
return rez;
}
int sisau()
{
int rez=notop();
if(crt=='A'&&s[poz+1]=='N')
{
poz+=4;
crt=s[poz];
rez=rez¬op();
}
else
if(crt=='O'&&s[poz+1]=='R')
{
poz+=3;
crt=s[poz];
rez=(rez|notop());
}
return rez;
}
int notop()
{
int ok=0,rez;
if(crt=='N'&&s[poz+1]=='O')
{
poz+=4;
crt=s[poz];
ok=1;
}
rez=termen();
if(ok==0)
return rez;
else
return !rez;
}
int main()
{
char c;
int n,i;
fin.getline(s,1000);
//fin.get();
fin>>n;
fin.get();
for(i=1;i<=n;i++)
{
fin>>c;
if(fv[c-'A']==0)
fv[c-'A']=1;
else
fv[c-'A']=0;
poz=0;
crt=s[poz];
fout<<sisau();
}
return 0;
}