#include <cstdio>
#include<cstring>
using namespace std;
FILE *f=fopen("bool.in", "r");
FILE *g=fopen("bool.out", "w");
char s[1001], sch[1001], sch2[1001];
int v[31], poz;
void schimb(){
int n=strlen(s);
char *p;
p=strchr(s, ' ');
while(p!=NULL){
strcpy(p, p+1);
p=strchr(p+1, ' ');
}
p=strstr(s, "TRUE");
while(p!=NULL){
s[p-s+3]='1';
strcpy(p, p+3);
p=strstr(p, "TRUE");
}
p=strstr(s, "FALSE");
while(p!=NULL){
s[p-s+4]='0';
strcpy(p, p+4);
p=strstr(p, "FALSE");
}
p=strstr(s, "AND");
while(p!=NULL){
s[p-s+2]='+';
strcpy(p, p+2);
p=strstr(p, "AND");
}
p=strstr(s, "OR");
while(p!=NULL){
s[p-s+1]='/';
strcpy(p, p+1);
p=strstr(p, "OR");
}
p=strstr(s, "NOT");
while(p!=NULL){
s[p-s+2]='!';
strcpy(p, p+2);
p=strstr(p, "NOT");
}
p=strchr(s, '(');
while(p!=NULL){
if(s[p-s+2]==')'){
strcpy(p, p+1);
strcpy(p+1, p+2);
p=strchr(s, '(');
}
else
p=strchr(p+1, '(');
}
p=strstr(s, "!!");
while(p!=NULL){
strcpy(p, p+2);
p=strstr(p, "!!");
}
}
void schimb3(char c){
char *p;
p=strchr(sch, c);
while(p!=NULL){
s[p-sch]=(s[p-sch]-'0'+1)%2+'0';
p=strchr(p+1, c);
}
}
void schimb2(){
char *p;
int x=-'A'+1;
for(char c='A'; c<='Z'; ++c){
p=strchr(s, c);
while(p!=NULL){
s[p-s]='0';
p=strchr(s, c);
}
}
}
void scot_par(){
char *p;
p=strstr(s, "(0)");
while(p!=NULL){
strcpy(p, p+1);
strcpy(p+1, p+2);
p=strstr(s, "(0)");
}
p=strstr(s, "(1)");
while(p!=NULL){
strcpy(p, p+1);
strcpy(p+1, p+2);
p=strstr(s, "(1)");
}
}
void scot_and(){
char *p=strstr(s, "0+0");
while(p!=NULL){
strcpy(p+1, p+3);
p=strstr(s, "0+0");
}
p=strstr(s, "0+1");
while(p!=NULL){
strcpy(p+1, p+3);
p=strstr(s, "0+1");
}
p=strstr(s, "1+0");
while(p!=NULL){
strcpy(p, p+2);
p=strstr(s, "1+0");
}
p=strstr(s, "1+1");
while(p!=NULL){
strcpy(p, p+2);
p=strstr(s, "1+1");
}
}
void scot_or(){
char *p=strstr(s, "0/0");
while(p!=NULL){
strcpy(p+1, p+3);
p=strstr(s, "0/0");
}
p=strstr(s, "0/1");
while(p!=NULL){
strcpy(p, p+2);
p=strstr(s, "0/1");
}
p=strstr(s, "1/0");
while(p!=NULL){
strcpy(p+1, p+3);
p=strstr(s, "1/0");
}
p=strstr(s, "1/1");
while(p!=NULL){
strcpy(p, p+2);
p=strstr(s, "1/1");
}
}
void scot_not(){
char *p;
p=strstr(s, "!0");
while(p!=NULL){
s[p-s]='1';
strcpy(p+1, p+2);
p=strstr(s, "!0");
}
p=strstr(s, "!1");
while(p!=NULL){
s[p-s]='0';
strcpy(p+1, p+2);
p=strstr(s, "!1");
}
}
int fct(){
char *p=strchr(s, s[0]);
while(strlen(s)!=1){
scot_par();
scot_and();
scot_or();
scot_not();
}
return s[0]-'0';
}
int main()
{
char c;
int n, x=0;
fgets(s, 1001, f);
s[strlen(s)-1]=NULL;
fscanf(f, "%d%*c", &n);
schimb();
strcpy(sch, s);
schimb2();
//fprintf(g, "%s\n", s);
for(int i=1; i<=n; ++i){
fscanf(f, "%c", &c);
schimb3(c);
strcpy(sch2, s);
fprintf(g, "%d", fct());
//fprintf(g, " %s %s\n", s, sch2);
strcpy(s, sch2);
poz=0;
}
return 0;
}