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