1 #include
2 #include
3 #include
4 using namespace std;
5 int n,len,ans;
6 long long x,t;
7 char s[15];
8 int main()
9 {
10 scanf("%d",&n);
11 for(int i=1;i<=n;i++)
12 {
13 ans=0;
14 scanf("%s%lld",s,&x);
15 len=strlen(s);
16 sort(s,s+len);
17 sscanf(s,"%lld",&t);
18 if(!(t%x))ans++;
19 while(next_permutation(s,s+len))
20 {
21 sscanf(s,"%lld",&t);
22 if(!(t%x))ans++;
23 }
24 printf("%d\n",ans);
25 }
26 return 0;
27 }