#include <fstream>
using namespace std;
const int NMAX = 1001;
ifstream fin("bool.in");
ofstream fout("bool.out");
bool alf[30], expresie(), termen(), factor();
char *p;
bool expresie()
{
bool crt = termen();
while(*p == 'O') p += 3, crt = crt || termen();
return crt;
}
bool termen()
{
bool crt = factor();
while(*p == 'A') p += 4, crt = crt && factor();
return crt;
}
bool factor()
{
bool crt, neg = 0;
if(*p == 'N' && *(p + 1) == 'O') p += 4, neg = 1;
if(*p == '(') ++p, crt = expresie(), ++p;
else if(*p == 'T' && *(p + 1) == 'R') crt = 1, p += 4;
else if(*p == 'F' && *(p + 1) == 'A') crt = 0, p += 5;
else crt = alf[*p - 'A'], ++p;
if(*p == ' ') ++p;
else if(*(p + 1) == ' ') p += 2;
return (neg ? !crt : crt);
}
int main()
{
char s[NMAX];
fin.getline(s, NMAX);
int n;
char c;
fin >> n;
while(n--)
{
fin >> c;
alf[c - 'A'] = !alf[c - 'A'];
p = s;
fout << expresie();
}
return 0;
}