#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, modificare;
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] && ( *p1 == ' ' || *p1 == '(' || *p1 == ')' || *p1 == '\0' || *p1 == '\r' ) )
return i;
}
return -1;
}
void skip_spatii() {
while ( *p == ' ' || *p == '\r' )
p++;
}
bool expr();
bool baza() {
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 termen() {
bool x = baza();
while ( true ) {
skip_spatii();
if ( *p == '\0' || *p == ')' )
break;
if ( dictionar(3, 4) == 3 ) {
p += cuv_marime[3];
bool y = baza();
x = x && y;
} else
break;
}
return x;
}
bool expr() {
bool x = termen();
while ( true ) {
skip_spatii();
if ( *p == '\0' || *p == ')' )
break;
if ( dictionar(4, 5) == 4 ) {
p += cuv_marime[4];
bool y = termen();
x = x || y;
} else
break;
}
return x;
}
int main() {
ios_base::sync_with_stdio(false);
fin.tie(NULL);
int N;
fin.getline(S, marime);
int len = 0;
while ( S[len] != '\0' )
len++;
if ( len > 0 && S[len - 1] == '\r' )
S[len - 1] = '\0';
fin >> N;
for ( int i = 0; i < N; i++ ) {
fin >> modificare;
variabile[modificare-'A'] = !variabile[modificare-'A'];
p = S;
fout << expr();
}
return 0;
}