#include <fstream>
using namespace std;
ifstream cin("bool.in");
ofstream cout("bool.out");
const int MAX = 1001;
char s[MAX], a[MAX], *p;
int var[26], n;
int expresie();
int termen();
int factor();
int main()
{
cin.getline(s, MAX);
//cout<<s<<endl;
int poz = 0, bune = 0;
for(poz = 0; s[poz]; poz++){
if(s[poz]=='N' and s[poz+1]=='O'){
s[bune++] = '!';
poz += 2;
continue;
}
if(s[poz]=='A' and s[poz+1]=='N'){
s[bune++] = '&';
poz += 2;
continue;
}
if(s[poz]=='O' and s[poz+1]=='R'){
s[bune++] = '|';
poz += 1;
continue;
}
if(s[poz]=='T' and s[poz+1]=='R'){
s[bune++] = '1';
poz += 3;
continue;
}
if(s[poz]=='F' and s[poz+1]=='A'){
s[bune++] = '0';
poz += 4;
continue;
}
if(s[poz]==' ') continue;
s[bune++] = s[poz];
}
s[bune] = '\0';
//cout<<s<<endl;
cin>>n;
cin.get(); cin.get(a, MAX);
for(int i=0; a[i]; i++){
var[ a[i]-'A' ] = !var[ a[i]-'A' ];
p = s;
cout<<expresie();
}
return 0;
}
int expresie()
{
int ans = termen();
while(*p=='|')
{
p++;
int rez = termen();
ans = ans or rez;
}
return ans;
}
int termen()
{
int ans = factor();
while(*p=='&')
{
p++;
int rez = factor();
ans = ans and rez;
}
return ans;
}
int factor()
{
int ans;
if(*p=='('){
p++;
ans = expresie();
p++;
return ans;
}
if(*p=='!'){
p++;
ans = expresie();
return !ans;
}
if(*p=='0' or *p=='1'){
p++;
return (*(p-1)-'0');
}
ans = var[*p-'A'];
p++;
return ans;
}