#include <fstream>
#include <vector>
#include <cstring>
#include <map>
using namespace std;
ifstream cin("bool.in");
ofstream cout("bool.out");
bool exprOr();
bool exprAnd();
bool exprNot();
bool factor();
char s[1005],c;
vector <char> t;
map <char,bool> val;
int i,ind,n;
bool exprOr()
{
bool evalor=exprAnd();
while (t[ind]=='|')
{
ind++;
evalor=evalor|exprAnd();
}
//cout<<"expror "<<evalor<<'\n';
return evalor;
}
bool exprAnd()
{
bool evaland=exprNot();
while (t[ind]=='&')
{
ind++;
evaland=evaland&exprNot();
}
//cout<<"exprand "<<evaland<<'\n';
return evaland;
}
bool exprNot()
{
bool evalnot=factor();
while (t[ind]=='^')
{
ind++;
evalnot=evalnot^factor();
}
//cout<<"exprnot "<<evalnot<<'\n';
return evalnot;
}
bool factor()
{
bool fct=0;
if (t[ind]=='(')
{
ind++;
fct=exprNot();
ind++;
}
else fct=val[t[ind]];
//cout<<"fct "<<fct<<'\n';
return fct;
}
int main()
{cin.getline(s,1005);
for (i=0; i<strlen(s); i++)
{
if (s[i]==' ') continue;
if (i<strlen(s)-4&&s[i]=='F'&&s[i+1]=='A'&&s[i+2]=='L'&&s[i+3]=='S'&&s[i+4]=='E')
{t.push_back('0'); i+=4;}
else if (i<strlen(s)-3&&s[i]=='T'&&s[i+1]=='R'&&s[i+2]=='U'&&s[i+3]=='E')
{t.push_back('1'); i+=3;}
else if (i<strlen(s)-2&&s[i]=='A'&&s[i+1]=='N'&&s[i+2]=='D')
{t.push_back('&'); i+=2;}
else if (i<strlen(s)-2&&s[i]=='N'&&s[i+1]=='O'&&s[i+2]=='T')
{t.push_back('1'); t.push_back('^'); i+=2;}
else if (i<strlen(s)-1&&s[i]=='O'&&s[i+1]=='R')
{t.push_back('|'); i++;}
else t.push_back(s[i]);
}
//for (i=0; i<t.size(); i++)
//cout<<t[i];
val['1']=true; val['0']=false;
cin>>n;
for (i=1; i<=n; i++)
{
cin>>c;
val[c]=!val[c];
ind=0;
cout<<exprOr();
}
return 0;
}