#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");
int n,i;
char s[1001],p;
vector<bool> v;
bool orr();
bool andd();
bool variabila();
bool expresie()
{
bool rez=orr();
while(s[i]=='O'&&s[i+1]=='R')
{
i+=3;
rez=rez|orr();
}
return rez;
}
bool orr()
{
bool rez=andd();
while(s[i]=='A'||s[i+1]=='N')
{
i+=4;
rez=rez&andd();
}
return rez;
}
bool andd()
{
bool rez;
if(s[i]=='(')
{
i++;
rez=expresie();
if(s[i]==')')i++;
if(s[i]==' ')i++;
}
else if(s[i]=='N'&&s[i+1]=='O')
{
i+=4;
rez=!expresie();
}
else
{
rez=variabila();
i++;
if(s[i]==')')i++;
if(s[i]==' ')i++;
}
return rez;
}
bool variabila()
{
return v[(int)s[i]];
}
int main()
{
f.getline(s,1000);
f>>n;
v.resize(100,0);
for(int j=1;j<=n;j++)
{
f>>p;
v[p]=v[p]^1;
i=0;
g<<expresie();
}
//cout<<s<<'\n'<<n<<'\n'<<(int)'A';
return 0;
}