var a:array[1..5] of longint;
n,i:longint;st:string;
function rst(x:longint):longint;
var f:array[-2..105,1..3] of longint;
num,t,j,k,l,m:longint; abb:boolean;
begin
fillchar(f,sizeof(f),0);
k:=0;t:=0;num:=0;
for j:=1 to length(st) do
begin
abb:=false;
case st[j] of
'+':begin inc(t);f[t,1]:=num;num:=0;f[t,2]:=k+1;f[t,3]:=1;end;
'-':begin inc(t);f[t,1]:=num;num:=0;f[t,2]:=k+1;f[t,3]:=-1;end;
'*':begin inc(t);f[t,1]:=num;num:=0;f[t,2]:=k+2;end;
'^':begin inc(t);f[t,1]:=num;num:=0;f[t,2]:=k+3;end;
'(':k:=k+3;
')':k:=k-3;
' ':;
'a':num:=x
else begin num:=(num*10+ord(st[j])-ord('0')) mod 10007; abb:=true;end;
end;
if (abb=false) then
while (f[t,2]<=f[t-1,2]) and (t>1) do
begin
if (f[t-1,2] mod 3=1) then
begin
dec(t);f[t,1]:=(f[t,1]+f[t,3]*f[t+1,1]) mod 10007;
f[t,2]:=f[t+1,2];f[t,3]:=f[t+1,3];
end
else if (f[t-1,2] mod 3=2) then
begin
dec(t);f[t,1]:=(f[t,1]*f[t+1,1]) mod 10007;
f[t,2]:=f[t+1,2];f[t,3]:=f[t+1,3];
end
else begin
dec(t);m:=f[t,1];
for l:=2 to f[t+1,1] do f[t,1]:=(f[t,1]*m) mod 10007;
f[t,2]:=f[t+1,2];f[t,3]:=f[t+1,3];
end;
end;
end;
inc(t);f[t,1]:=num; f[t,2]:=0;
while (f[t,2]<=f[t-1,2]) and (t>1) do
begin
if (f[t-1,2] mod 3=1) then
begin
dec(t);f[t,1]:=(f[t,1]+f[t,3]*f[t+1,1]) mod 10007;
f[t,2]:=f[t+1,2];f[t,3]:=f[t+1,3];
end
else if (f[t-1,2] mod 3=2) then
begin
dec(t);f[t,1]:=(f[t,1]*f[t+1,1]) mod 10007;
f[t,2]:=f[t+1,2];f[t,3]:=f[t+1,3];
end
else begin
dec(t);m:=f[t,1];
for l:=2 to f[t+1,1] do f[t,1]:=(f[t,1]*m) mod 10007;
f[t,2]:=f[t+1,2];f[t,3]:=f[t+1,3];
end;
end;
for j:=t downto 2 do
begin
if (f[t-1,2] mod 3=1) then
begin
dec(t);f[t,1]:=(f[t,1]+f[t,3]*f[t+1,1]) mod 10007;
end
else if (f[t-1,2] mod 3=2) then
begin
dec(t);f[t,1]:=(f[t,1]*f[t+1,1]) mod 10007;
end
else begin
dec(t);m:=f[t,1];
for l:=2 to f[t+1,1] do f[t,1]:=(f[t,1]*m) mod 10007;
end;
end;
exit(f[1,1]);
end;
function okay:boolean;
var j:longint;
begin
for j:=1 to 5 do begin
if rst(j)<>a[j] then exit(false);
end;
exit(true);
end;
begin
assign(input,'equal.in');
assign(output,'equal.ans');
reset(input);
rewrite(output);
readln(st);
for i:=1 to 5 do a[i]:=rst(i);
readln(n);
for i:=1 to n do
begin
readln(st);
if okay then write(chr(ord('A')+i-1));
end;
writeln;
close(input);
close(output);
end.