#include <cstdio>
#include <fstream>
#include <string>
#define MAXVAL 26
#define MAXLEN 1000
#define NOT 2
#define AND 3
#define OR 4
std::string str;
char e[MAXLEN];
int sIndex;
bool val[MAXVAL];
bool orr();
bool fac() {
bool n;
if (isalpha(e[sIndex])) // daca e variabila
n = val[e[sIndex++] - 'A'];
else if (e[sIndex] <= 1) { // daca e constanta
n = e[sIndex++];
} else { // daca e paranteza
sIndex++;
n = orr();
sIndex++; // sar peste paranteza inchisa
}
return n;
}
bool nott() {
bool n;
if (e[sIndex] != NOT)
n = fac();
else {
sIndex++;
n = fac();
n = !n;
}
return n;
}
bool andd() {
bool n;
n = nott();
while (e[sIndex] == AND) {
sIndex++;
n = nott() && n;
}
return n;
}
bool orr() {
bool n;
n = andd();
while (e[sIndex] == OR) {
sIndex++;
n = andd() || n;
}
return n;
}
int main() {
FILE *fout;
std::ifstream fin("bool.in");
fout = fopen("bool.out", "w");
std::string querry;
std::getline(fin, str);
fin.ignore(100, '\n'); // scap de linia a doua, ca e inutila
std::getline(fin, querry);
sIndex = 0;
// transforma stringul in expresie
for (int i = 0; i < str.size();) {
if (str[i] == ' ')
i++;
else if (str[i] == 'T' && str[i + 1] == 'R') { // true
e[sIndex++] = 1;
i += 4; // sar peste restul trueului si spatiul urator
} else if (str[i] == 'F' && str[i + 1] == 'A') { // false
e[sIndex++] = 0;
i += 5; // lafel
} else if (str[i] == 'N' && str[i + 1] == 'O') { // NOT
e[sIndex++] = NOT;
i += 3;
} else if (str[i] == 'A' && str[i + 1] == 'N') { // AND
e[sIndex++] = AND;
i += 3;
} else if (str[i] == 'O' && str[i + 1] == 'R') {
e[sIndex++] = OR;
i += 2;
} else { // paranteza sau variabila
e[sIndex++] = str[i];
i += 1;
}
}
e[sIndex] = '\n';
sIndex = 0;
/*for (; e[sIndex] != '\n'; sIndex++) {
if (e[sIndex] == AND)
printf("&");
else if (e[sIndex] == OR)
printf("|");
else if (e[sIndex] == NOT)
printf("!");
else if (e[sIndex] <= 1)
printf("%d", e[sIndex]);
else
printf("%c", e[sIndex]);
}
printf("\n");*/
for (int i = 0; i < querry.size(); i++) {
val[querry[i] - 'A'] = !val[querry[i] - 'A'];
sIndex = 0;
fprintf(fout, "%d", orr());
}
sIndex = 0;
return 0;
}