#include <fstream>
using namespace std;
ifstream cin("bool.in");
ofstream cout("bool.out");
int ind = 0, n,v[100005];
char s[100005], c[100005];
int OR();
int AND();
int NOT();
int paranteza_expresie();
int valoare();
int OR()
{
while(s[ind] == ' ')
{
ind++;
}
int ans = AND();
while(s[ind] == 'O' && s[ind+1] == 'R')
{
ind+=2;
ans=ans | AND();
}
return ans;
}
int AND()
{
while(s[ind] == ' ')
{
ind++;
}
int ans2 = NOT();
while(s[ind] == 'A' && s[ind+1] == 'N')
{
ind+=3;
ans2 = ans2 & NOT();
}
return ans2;
}
int NOT()
{
while(s[ind] == ' ')
{
ind++;
}
int ans3 = paranteza_expresie();
while(s[ind] == 'N' && s[ind+1] == 'O')
{
ind+=3;
ans3 = ans3 & !paranteza_expresie();
}
return ans3;
}
int paranteza_expresie()
{
while(s[ind] == ' ')
{
ind++;
}
int ans4 = 0;
while(s[ind] == ' ')
{
ind++;
}
if(s[ind] == '(')
{
ind++;
ans4 = OR();
ind++;
}
else ans4 = valoare();
return ans4;
}
int valoare()
{
int ans5 = 0;
while(s[ind] == ' ')
{
ind++;
}
if(s[ind] != ' ')
{
ans5 = v[int(s[ind])];
ind++;
}
return ans5;
}
int main()
{
cin.getline(s,100005);
cin >> n; cin.get();
cin.getline(c,1005);
for(int i = 0; i < n; i++)
{
ind = 0;
if(v[int(c[i])] == 1)
{
v[int(c[i])] = 0;
}
else v[int(c[i])] = 1;
cout<<OR();
}
return 0;
}