const ult:char=chr(ord('Z')+2);
var s:ansistring;
a:array['A'..chr(ord('Z')+2)] of longint;
q,x,n,i:longint;
f,g:text;
ch:char;
function sau:longint; forward;
function si:longint; forward;
function non:longint; forward;
function termen:longint; forward;
function sau:longint;
var x,y:longint;
begin
x:=si;
while (i<=q) and (s[i]='|') do
begin
inc(i);
y:=si;
x:=x or y;
end;
sau:=x;
end;
function si:longint;
var x,y:longint;
begin
x:=non;
while (i<=q) and (s[i]='&') do
begin
inc(i);
y:=non;
x:=x and y;
end;
si:=x;
end;
function non:longint;
var x:longint;
begin
if s[i]='~' then
begin
inc(i);
x:=termen;
x:=x xor 1;
end
else
x:=termen;
non:=x;
end;
function termen:longint;
var x:longint;
begin
if s[i]='(' then begin
inc(i);
x:=sau;
inc(i);
end
else begin
x:=a[s[i]];
inc(i);
end;
termen:=x;
end;
begin
assign(f,'bool.in'); reset(f);
assign(g,'bool.out'); rewrite(g);
n:=0; a[ult]:=0; a[chr(ord('Z')+1)]:=1;
readln(f,s);
x:=pos(' ',s);
while x<>0 do
begin
delete(s,x,1);
x:=pos(' ',s);
end;
x:=pos('TRUE',s);
while x<>0 do
begin
delete(s,x,4);
insert(chr(ord('Z')+1),s,x);
x:=pos('TRUE',s);
end;
x:=pos('FALSE',s);
while x<>0 do
begin
delete(s,x,5);
insert(ult,s,x);
x:=pos('FALSE',s);
end;
x:=pos('NOT',s);
while x<>0 do
begin
delete(s,x,3);
insert('~',s,x);
x:=pos('NOT',s);
end;
x:=pos('AND',s);
while x<>0 do
begin
delete(s,x,3);
insert('&',s,x);
x:=pos('AND',s);
end;
x:=pos('OR',s);
while x<>0 do
begin
delete(s,x,2);
insert('|',s,x);
x:=pos('OR',s);
end;
x:=pos('~~',s);
while x<>0 do
begin
delete(s,x,2);
x:=pos('~~',s);
end;
readln(f,n);
q:=length(s);
for x:=1 to n do begin
read(f,ch);
a[ch]:=a[ch] xor 1;
i:=1;
write(g,sau);
end;
writeln(g);
close(f); close(g);
end.