def matmul(a,b,l,m,n):
c = []
for i in range(l):
dummy = []
for j in range(n):
ans = 0
for k in range(m):
ans = ans + a[i][k] * b[k][j]
dummy.append(ans)
c.append(dummy)
return c
def power_matrix(a,n):
if n == 0:
return 1
elif n == 1:
return a
elif n%2 == 0:
return power_matrix(matmul(a,a,26,26,26),n/2)
elif n%2 == 1:
return matmul(a,power_matrix(matmul(a,a,26,26,26),(n-1)/2))
a = []
for i in range(26):
k = []
for j in range(26):
k.append(0.0)
a.append(k)
t = input()
for dummy in range(t):
n, k = raw_input().split()
n = int(n)
k = int(k)
sum_of_Prob = 0
word = raw_input()
length = len(word)
word = list(word)
for i in range(length):
word[i] = ord(word[i]) - 97
for i in range(26):
num = raw_input().split()
for j in range(26):
a[i][j] = float(num[j])
a = power_matrix(a,k)
prob = 0
for j in range(n):
p1 = 1
str1 = raw_input()
if(len(str1) != length):
continue
else:
s = list(str1)
for i in range(len(s)):
s[i] = ord(s[i])-97
for i in range(length):
p1 = p1*a[word[i]][s[i]]
if p1 == 0:
break
prob = prob + p1
print prob
ZGVmIG1hdG11bChhLGIsbCxtLG4pOgoJYyA9IFtdCglmb3IgaSBpbiByYW5nZShsKToKCQlkdW1teSA9IFtdCgkJZm9yIGogaW4gcmFuZ2Uobik6CgkJCWFucyA9IDAKCQkJZm9yIGsgaW4gcmFuZ2UobSk6CgkJCQlhbnMgPSBhbnMgKyBhW2ldW2tdICogYltrXVtqXQoJCQlkdW1teS5hcHBlbmQoYW5zKQoJCWMuYXBwZW5kKGR1bW15KQoJcmV0dXJuIGMKCmRlZiBwb3dlcl9tYXRyaXgoYSxuKToKCWlmIG4gPT0gMDoKCQlyZXR1cm4gMQoJZWxpZiBuID09IDE6CgkJcmV0dXJuIGEKCWVsaWYgbiUyID09IDA6CgkJcmV0dXJuIHBvd2VyX21hdHJpeChtYXRtdWwoYSxhLDI2LDI2LDI2KSxuLzIpCgllbGlmIG4lMiA9PSAxOgoJCXJldHVybiBtYXRtdWwoYSxwb3dlcl9t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