#include <cstdio>
#include <cstring>
using namespace std;
char q[1100];
int v[1000];
int indi;
char *p;
void sparg( char s[], char c, int n ){
p = strstr(q,s);
while( p != NULL ){
*p = c;
strcpy(p+1,p+n);
p = strstr(p,s);
}
}
int f1( );
int f2( );
int main()
{
freopen("bool.in","r",stdin);
freopen("bool.out","w",stdout);
int n, i, j, s, t, d, k;
char lit;
fgets(q,1005,stdin);
q[strlen(q)-1] = '\0';
sparg("OR\0",'|',2);
sparg("AND\0",'&',3);
sparg("NOT\0",'!',3);
sparg("TRUE\0",'1',4);
sparg("FALSE\0",'0',5);
for( i = 0; i < strlen(q); ++i ){
if( q[i] == ' ' ) strcpy(q+i,q+i+1);
}
scanf("%d%*c",&n);
for( i = 1; i <= n; ++i ){
scanf("%c",&lit);
if( v[lit] == 1 ) v[lit] = 0;
else v[lit] = 1;
indi = 0;
printf("%d",f1());
}
return 0;
}
int f1(){
int r = f2();
while( q[indi] == '|' || q[indi] == '&' ){
if( q[indi] == '|' ){
indi++;
r = ( r || f2() );
}
else{
indi++;
r = ( r && f2() );
}
}
return r;
}
int f2(){
int t = 0;
if( q[indi] == '(' ){
indi++;
t = f1();
indi++;
return t;
}
if( q[indi] == '!' ){
indi++;
t = f1();
indi++;
return !t;
}
while( q[indi] >= '0' && q[indi] <= '9' ){
t = q[indi] - '0';
indi++;
}
while( q[indi] >= 'A' && q[indi] <= 'Z' ){
t = v[q[indi]];
indi++;
}
return t;
}