#include <fstream>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
bool variabile[26];
const int marime = 1e3 + 5, cuv_marime[] = {5, 4, 3, 3, 2};
const char cuv[][6] = {"FALSE", "TRUE", "NOT", "AND", "OR"};
char S[marime], *p, modificari[marime];
int dictionar(int start, int end) {
for ( int i = start; i < end; i++ ) {
char *p1 = p;
int j = 0;
for ( j; j < cuv_marime[i]; p1++, j++ )
if ( *p1 != cuv[i][j] )
break;
if ( j >= cuv_marime[i] )
return i;
}
return -1;
}
void skip_spatii() {
while ( *p == ' ' )
p++;
}
bool expr();
bool termen() {
skip_spatii();
bool x = 0, no = 0;
if ( dictionar(2, 3) == 2 ) {
no = 1;
p += cuv_marime[2];
skip_spatii();
}
if ( *p == '(' ) {
p++;
x = expr();
skip_spatii();
p++;
} else {
int logic = dictionar(0, 2);
if ( logic != -1 ) {
p += cuv_marime[logic];
x = ( logic == 1 );
} else if ( *p >= 'A' && *p <= 'Z' ) {
x = variabile[*p-'A'];
p++;
}
}
if ( no )
x = !x;
return x;
}
bool expr() {
bool x = termen();
while ( true ) {
skip_spatii();
if ( *p == '\0' || *p == ')' )
break;
int cuv_index = dictionar(3, 5);
if ( cuv_index != -1 ) {
p += cuv_marime[cuv_index];
if ( cuv_index == 3 )
x = x && termen();
else
x = x || termen();
} else
break;
}
return x;
}
int main() {
int N;
fin.getline(S, marime);
fin >> N;
fin >> modificari;
for ( int i = 0; i < N; i++ ) {
variabile[modificari[i]-'A'] = !variabile[modificari[i]-'A'];
p = S;
fout << expr();
}
return 0;
}