#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define Lmax 1001
#define NRLEV 3
char *p;
char* op[] = {"|", "&", "!", ""};
int val[26];
int expr(int) ;
int eval(int, char, int) ;
void f(char*) ;
int main()
{
ifstream fin("bool.in");
ofstream fout("bool.out");
int n;
char c;
char s[Lmax];
fin.getline(s, Lmax);
f(s);
for(int i = 0; i < 26; ++i) val[i] = 0;
for(fin >> n; n; --n)
{
fin >> c;
val[c - 'A'] = 1 - val[c - 'A'];
p = s;
fout << expr(0);
}
fout << '\n' << strchr("re", '\0');
return 0;
}
int expr(int level)
{
int x, y;
char oper;
if(level == NRLEV)
if(*p == '(')
++p, x = expr(0), ++p;
else if(*p == '1') x = 1, ++p;
else if(*p == '0') x = 0, ++p;
else x = val[*p - 'A'], ++p;
else for(x = expr(level + 1); p && strchr(op[level], *p); x = y)
{
oper = *p;
p++;
y = expr(level + 1);
y = eval(x, oper, y);
}
return x;
}
int eval(int x, char oper, int y)
{
switch(oper)
{
case '|': return x | y;
case '&': return x & y;
case '!': return !y;
}
}
void f(char* s)
{
char *p1, *p2;
for(p1 = p2 = s; *p2; )
if(*p2 == '(' || *p2 == ')') *p1 = *p2, ++p1, ++p2;
else if(*p2 == 'A')
{
if(*(p2 + 1) == 'N') *p1 = '&', ++p1, p2 += 3; // *p2 = AND (operator)
else *p1 = 'A', ++p1, ++p2;
}
else if(*p2 == 'O')
{
if(*(p2 + 1) == 'R') *p1 = '|', ++p1, p2 += 2; // *p2 = OR (operator)
else *p1 = 'O', ++p1, ++p2;
}
else if(*p2 == 'N')
{
if(*(p2 + 1) == 'O') *p1 = '1', ++p1, *p1 = '!', ++p1, p2 += 3; // *p2 = NOT (operator)
else *p1 = 'N', ++p1, ++p2;
}
else if(*p2 == 'T')
{
if(*(p2 + 1) == 'R') *p1 = '1', ++p1, p2 += 4; // *p2 = TRUE
else *p1 = 'T', ++p1, ++p2;
}
else if(*p2 == 'F')
{
if(*(p2 + 1) == 'A') *p1 = '0', ++p1, p2 += 5; // *p2 = FALSE
else *p1 = 'F', ++p1, ++p2;
}
else if('A' <= *p2 && *p2 <= 'Z') *p1 = *p2, ++p1, ++p2;
else ++p2; // *p2 = spatiu
*p1 = '\n';
}