// #pragma GCC optimize("O3,unroll-loops,fast-math")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int Nmax=2e5+5,inf=1e9,MOD=1e9+7;
string s,t;
int n,i;
bool val[26];
bool orr();
bool term() {
if (t[i]=='(') {
i++;
bool rez=orr();
i++;
return rez;
}
if (t[i]=='1') {
i++;
return 1;
}
if (t[i]=='0') {
i++;
return 0;
}
return val[t[i++]-'A'];
}
bool nott() {
if (t[i]=='!') {
i++;
return !nott();
}
return term();
}
bool andd() {
bool rez=nott();
while (i<n && t[i]=='&') {
i++;
bool temp=nott();
rez&=temp;
}
return rez;
}
bool orr() {
bool rez=andd();
while (i<n && t[i]=='|') {
i++;
bool temp=andd();
rez|=temp;
}
return rez;
}
int main() {
ifstream cin("bool.in");
ofstream cout("bool.out");
getline(cin,s);
for (i=0; i<s.size();) {
if (s.substr(i,4)=="TRUE") {
t+="1";
i+=4;
}
else if (s.substr(i,5)=="FALSE") {
t+="0";
i+=5;
}
else if (s.substr(i,3)=="AND") {
t+="&";
i+=3;
}
else if (s.substr(i,2)=="OR") {
t+="|";
i+=2;
}
else if (s.substr(i,3)=="NOT") {
t+="!";
i+=3;
}
else {
if (s[i]!=' ' && s[i]!='\r') t+=s[i];
i++;
}
}
n=t.size();
int q;
string queries;
cin>>q>>queries;
for (auto &ch:queries) {
val[ch-'A']^=1;
i=0;
cout<<orr();
}
return 0;
}