#include <iostream>
#include <fstream>
#define nl '\n'
using namespace std;
const int DMAX = 1201;
char S[DMAX], *p;
int V[26];
ifstream fin("bool.in");
ofstream fout("bool.out");
int expresie();
int termen();
int factor();
int expresie()
{
int r = termen();
while (*p == 'O' && *(p + 1) == 'R')
{
p += 3;
r |= termen();
}
return r;
}
int termen()
{
int r = factor();
while (*p == 'A' && *(p+1) == 'N' && *(p+2) == 'D')
{
p+=4;
r &= factor();
}
return r;
}
int factor()
{
int r;
if (*p == '(')
{
p++;
r = expresie();
p++;
}
else
{
if (*p == 'N' && *(p+1) == 'O' && *(p+2) == 'T')
{
p+=4;
r = !factor();
}
else
{
if (*p == 'T' && *(p+1) == 'R' && *(p+2) == 'U' && *(p+3) == 'E')
{
p+=5;
r = 1;
}
else
{
if (*p == 'F' && *(p+1) == 'A' && *(p+2) == 'L' && *(p+3) == 'S' && *(p+4) == 'E')
{
p+=6;
r = 0;
}
else
{
p+=2;
r = V[*p-'A'];
}
}
}
}
return r;
}
int main()
{
int N;
char c;
fin.getline(S, DMAX);
fin >> N;
fin.get();
while (N--)
{
fin.get(c);
V[c-'A'] ^= 1;
p = S;
fout << expresie();
}
return 0;
}