#include <fstream>
#include <string>
#include <cstring>
using namespace std;
ifstream cin("bool.in");
ofstream cout("bool.out");
const int nmax = 1 << 7;
int state;
char str[1 << 10];
int n;
int L;
char x[nmax];
inline bool isUpper(char x) {
return ('A' <= x && x <= 'Z');
}
inline int getCh(char x) {
return static_cast<int>(x - 'A');
}
bool evaluate(int&);
bool getExpression(int &i) {
bool ret = false;
if (str[i] == 'N' && str[i + 1] == 'O') {
i += 4;
ret = !getExpression(i);
}
else
if (str[i] == '(') {
i++;
ret = evaluate(i);
i++;
}
else
if (str[i] == 'F' && str[i + 1] == 'A') {
i += 5;
ret = false;
} else
if (str[i] == 'T' && str[i + 1] == 'R') {
i += 4;
ret = true;
} else
if (isUpper(str[i])) {
ret = (state & (1 << getCh(str[i]))) > 0;
i++;
}
return ret;
}
bool evaluateAnd(int &pos) {
bool ret = false;
for (ret = getExpression(pos);str[pos + 1] == 'A' && str[pos + 2] == 'N' && str[pos + 3] == 'D';) {
pos += 5;
ret &= getExpression(pos);
}
return ret;
}
bool evaluate(int &pos) {
bool ret = false;
for (ret = evaluateAnd(pos);str[pos + 1] == 'O' && str[pos + 2] == 'R';) {
pos += 4;
ret |= evaluateAnd(pos);
}
return ret;
}
int main()
{
cin.getline(str,1 << 10);
cin >> n;
cin.get();
cin.getline(x,nmax);
for (int i = 0;i < n;i++) {
state ^= (1 << getCh(x[i]));
int pos = 0;
cout << evaluate(pos);
}
return 0;
}