//
// main.cpp
// Bool
//
// Created by Andrada Minca on 23.09.2025.
//
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("bool.in");
ofstream cout("bool.out");
int ind=0;
// char s[1005];
string s;
vector<bool> state(27,false);
bool expresie();
bool factor();
bool termen();
bool numar();
bool altcv();
bool expresie()
{
bool ans=termen();
while(ind + 1 < s.size() && s.substr(ind, 2) == "OR")
{
ind+=2;
ans|=termen();
}
return ans;
}
bool termen()
{
bool ans=factor();
while(ind + 2 < s.size() && s.substr(ind, 3) == "AND")
{
ind+=3;
ans&=factor();
}
return ans;
}
bool factor()
{
// bool ans=altcv();
bool ans = false;
while(ind + 2 < s.size() && s.substr(ind, 3) == "NOT")
{
ind+=3;
ans ^= 1;
}
return ans ^ altcv();
}
bool altcv()
{
if(s[ind]=='(')
{
ind++;
bool ans=expresie();
ind++;
return ans;
}
return numar();
}
bool numar()
{
if (ind + 3 < s.size() && s.substr(ind, 4) == "TRUE") {
ind += 4;
return true;
} else if (ind + 4 < s.size() && s.substr(ind, 5) == "FALSE") {
ind += 5;
return false;
} else {
ind++;
return state[s[ind - 1]-'A'];
}
}
int main()
{
int n;
getline(cin, s);
string t = "";
for (auto c : s) {
if (c != ' ') {
t += c;
}
}
s = t;
cin>>n;
for(int i=0;i<n;i++)
{
char x;
cin>>x;
state[x-'A']=!state[x-'A'];
cout<<expresie();
ind=0;
}
return 0;
}