7!=5040
所以这题直接求质数比打一千万的表都快
这提高诉我们阶乘其实不算大&看(算)清数据规模
Program cc;
var
n,t,len,i,j,ans:longint;
s:string;
b:array[0..9] of longint;
procedure dfs(p,l:longint);
var
i:longint;
begin
if l=len then
begin
if p=1 then exit;
for i:=2 to trunc(sqrt(p)) do if (p mod i=0) then exit;
inc(ans);
exit;
end;
for i:=0 to 9 do
if b[i]>0 then
begin
if (i=0) and (l=0) then continue;
dec(b[i]);
dfs(p*10+i,l+1);
inc(b[i]);
end;
end;
begin
readln(t);
while (t>0) do
begin
readln(s);
len:=length(s);
fillchar(b,sizeof(b),0);
for i:=1 to len do inc(b[ord(s[i])-48]);
ans:=0;
for i:=1 to len do
begin
dfs(0,0);
dec(len);
end;
writeln(ans);
dec(t);
end;
end.