#include <fstream>
#include <map>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
int n;
string keywords[1005];
string input;
map<string, bool> variables;
int i;
bool isVariable(string word){
return word != "AND" && word != "OR" && word != "NOT" && word != "(" && word != ")" && word != ")" && word != "TRUE" && word != "FALSE";
}
bool expresie();
bool termen();
bool factor();
bool expresie(){
bool r;
if(keywords[i] == "NOT"){
i++;
r = !termen();
}
else r = termen();
while(keywords[i] == "OR"){
i++;
if(keywords[i] == "NOT"){
i++;
r = r || !termen();
}
else r = r || termen();
}
return r;
}
bool termen(){
bool r;
if(keywords[i] == "NOT"){
i++;
r = !factor();
}
else r = factor();
while(keywords[i] == "AND"){
i++;
if(keywords[i] == "NOT"){
i++;
r = r && !factor();
}
else r = r && factor();
}
return r;
}
bool factor(){
bool isswapped = false;
bool r;
if(keywords[i] == "NOT"){isswapped = true; i++;}
if(keywords[i] == "("){
i++;
if(isswapped) r = !expresie();
else r = expresie();
i++;
}
else{
r = variables[keywords[i]];
i++;
}
return r;
}
int main()
{
variables["TRUE"] = 1;
variables["FALSE"] = 0;
getline(fin, input);
int words = 1;
for(i = 0; i < input.size(); i++){
if(input[i] == ' ') {
if(keywords[words].size() == 0) continue;
if(isVariable(keywords[words])) variables[keywords[words]] = 0;
words++;
}
keywords[words].append(1, input[i]);
if(input[i] == '(' || input[i] == ')') words++;
}
fin >> n;
for(int j = 1; j <= n; j++){
i = 1;
char s;
fin >> s;
string variable(1, s);
variables[variable] = !variables[variable];
fout << expresie();
}
}