#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
typedef struct p
{
double a[27][27];
}matrix;
matrix mat_mul(matrix m,matrix n)
{
int i,j,k;
matrix b;
for(i=0;i<=25;i++)
{
for(j=0;j<=25;j++)
{
b.a[i][j]=0;
for(k=0;k<=25;k++)
b.a[i][j]+=m.a[i][k]*n.a[k][j];
}
}
return b;
}
matrix pow_mat(matrix p,int k)
{
matrix ans;
for(int i=0;i<=25;i++)
for(int j=0;j<=25;j++)
ans.a[i][j]=double(i==j);
while(k)
{
if(k%2==1)
ans=mat_mul(ans,p);
k/=2;
p=mat_mul(p,p);
}
return ans;
}
int main()
{
int t,n,i,j,k;
char s[4],m[4];
bool hash_string[26*26*26+26*26+26+2];
//freopen("input.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
matrix p;
memset(hash_string,0,sizeof(hash_string));
double ans=0;
scanf("%d %d\n",&n,&k);
scanf("%s",s);
for(i=0;i<=25;i++)
{
for(j=0;j<=25;j++)
{
scanf("%lf",&p.a[i][j]);
//printf("%.2f ",x);
}
//printf("\n");
}
matrix z=pow_mat(p,k);
/*for(i=0;i<=25;i++)
{
for(j=0;j<=25;j++)
{
printf("%.2f ",z.a[i][j]);
}
printf("\n");
}*/
for(i=0;i<n;i++)
{
scanf("%s",m);
double temp=1;
int val=0;
for(j=0;j<strlen(m);j++)
val+=(val*26)+(m[j]-'a');
if(!hash_string[val])
{
if(strlen(s)==strlen(m))
{
for(j=0;j<strlen(m);j++)
temp*=z.a[s[j]-'a'][m[j]-'a'];
ans+=temp;
}
hash_string[val]=true;
}
//cout<<temp<<endl;
}
printf("%.6lf\n",ans);
}
return 0;
}
I2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTxpb3N0cmVhbT4KI2luY2x1ZGU8Y3N0cmluZz4KI2luY2x1ZGU8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiBzdHJ1Y3QgcAp7CiAgICBkb3VibGUgYVsyN11bMjddOwp9bWF0cml4OwoKCm1hdHJpeCBtYXRfbXVsKG1hdHJpeCBtLG1hdHJpeCBuKQp7CgogICAgaW50IGksaixrOwogICAgbWF0cml4IGI7CiAgICBmb3IoaT0wO2k8PTI1O2krKykKICAgIHsKICAgICAgICBmb3Ioaj0wO2o8PTI1O2orKykKICAgICAgICB7CiAgICAgICAgICAgIGIuYVtpXVtqXT0wOwogICAgICAgICAgICBmb3Ioaz0wO2s8PTI1O2srKykKICAgICAgICAgICAgICAgIGIuYVtpXVtqXSs9bS5hW2ldW2tdKm4uYVtrXVtqXTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gYjsKCn0KCgoKbWF0cml4IHBvd19tYXQobWF0cml4IHAsaW50IGspCnsKICAgIG1hdHJpeCBhbnM7CiAgICBmb3IoaW50IGk9MDtpPD0yNTtpKyspCiAgICAgICAgZm9yKGludCBqPTA7ajw9MjU7aisrKQogICAgICAgICAgIGFucy5hW2ldW2pdPWRvdWJsZShpPT1qKTsKCiAgICB3aGlsZShrKQogICAgewogICAgICAgIGlmKGslMj09MSkKICAgICAgICAgICAgYW5zPW1hdF9tdWwoYW5zLHApOwogICAgICAgIGsvPTI7CiAgICAgICAgcD1tYXRfbXVsKHAscCk7CiAgICB9CgogIHJldHVybiBhbnM7Cgp9CgppbnQgbWFpbigpCnsKaW50IHQsbixpLGosazsKY2hhciBzWzRdLG1bNF07CmJvb2wgaGFzaF9zdHJpbmdbMjYqMjYqMjYrMjYqMjYrMjYrMl07Ci8vZnJlb3BlbigiaW5wdXQudHh0IiwiciIsc3RkaW4pOwpzY2FuZigiJWQiLCZ0KTsKd2hpbGUodC0tKQp7CiAgIG1hdHJpeCBwOwogICBtZW1zZXQoaGFzaF9zdHJpbmcsMCxzaXplb2YoaGFzaF9zdHJpbmcpKTsKICAgZG91YmxlIGFucz0wOwogICAgc2NhbmYoIiVkICVkXG4iLCZuLCZrKTsKICAgIHNjYW5mKCIlcyIscyk7CiAgICBmb3IoaT0wO2k8PTI1O2krKykKICAgIHsKICAgICAgICBmb3Ioaj0wO2o8PTI1O2orKykKICAgICAgICB7CiAgICAgICAgICAgIHNjYW5mKCIlbGYiLCZwLmFbaV1bal0pOwogICAgICAgICAgICAvL3ByaW50ZigiJS4yZiAiLHgpOwogICAgICAgIH0KICAgICAgICAvL3ByaW50ZigiXG4iKTsKICAgIH0KCiAgIG1hdHJpeCB6PXBvd19tYXQocCxrKTsKICAgLypmb3IoaT0wO2k8PTI1O2krKykKICAgIHsKICAgICAgICBmb3Ioaj0wO2o8PTI1O2orKykKICAgICAgICB7CiAgICAgICAgICAgIHByaW50ZigiJS4yZiAiLHouYVtpXVtqXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0qLwoKICAgZm9yKGk9MDtpPG47aSsrKQogICB7CiAgICAgICBzY2FuZigiJXMiLG0pOwogICAgICBkb3VibGUgdGVtcD0xOwogICAgICBpbnQgdmFsPTA7CiAgICAgIGZvcihqPTA7ajxzdHJsZW4obSk7aisrKQogICAgICAgIHZhbCs9KHZhbCoyNikrKG1bal0tJ2EnKTsKICAgICAgaWYoIWhhc2hfc3RyaW5nW3ZhbF0pCiAgICAgIHsKICAgICAgIGlmKHN0cmxlbihzKT09c3RybGVuKG0pKQogICAgICAgewogICAgICAgZm9yKGo9MDtqPHN0cmxlbihtKTtqKyspCiAgICAgICAgIHRlbXAqPXouYVtzW2pdLSdhJ11bbVtqXS0nYSddOwoKICAgICAgIGFucys9dGVtcDsKICAgICAgIH0KICAgICAgIGhhc2hfc3RyaW5nW3ZhbF09dHJ1ZTsKICAgICAgfQogICAgICAgLy9jb3V0PDx0ZW1wPDxlbmRsOwoKICAgfQoKcHJpbnRmKCIlLjZsZlxuIixhbnMpOwoKCn0KCnJldHVybiAwOwp9
MQoyIDEKYQowIDEgMCAwIDAgMCAwIDAgMCAwIDAgMCAwIDAgMCAwIDAgMCAwIDAgMCAwIDAgMCAwIDAKMCAwLjUgMCAwLjUgMCAwID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