/*input
1
5 4
abccba
abddba
xa
abdcba
aneverknow
1
2
4
3
*/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("O3")
typedef long long ll;
typedef long double ld;
int kiek[101010];
int NXT[101010][26];
int ans[101010];
int main()
{
for (int t = 0; t < 101010; t++)
{
kiek[t] = ans[t] = 0;
for (int d = 0; d < 26; d++)
NXT[t][d] = -1;
}
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
int num = 1;
int n, q;
cin >> n >> q;
while (n--)
{
string s;
cin >> s;
int kur = 0;
for (int i = s.size() - 1; i >= 0; i--)
{
int k = s[i] - 'a';
if (NXT[kur][k] == -1)
NXT[kur][k] = num++;
kur = NXT[kur][k];
ans[s.size() - i] = max(ans[s.size() - i], ++kiek[kur]);
}
}
while (q--)
{
int x;
cin >> x;
cout << ans[x] << '\n';
}
for (int t = 0; t <= num; t++)
{
kiek[t] = ans[t] = 0;
for (int d = 0; d < 26; d++)
NXT[t][d] = -1;
}
}
}
LyppbnB1dAoxCjUgNAphYmNjYmEKYWJkZGJhCnhhCmFiZGNiYQphbmV2ZXJrbm93CjEKMgo0CjMKKi8KI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNwcmFnbWEgR0NDIG9wdGltaXplICgiTzMiKQp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBsb25nIGRvdWJsZSBsZDsKaW50IGtpZWtbMTAxMDEwXTsKaW50IE5YVFsxMDEwMTBdWzI2XTsKaW50IGFuc1sxMDEwMTBdOwppbnQgbWFpbigpCnsKCWZvciAoaW50IHQgPSAwOyB0IDwgMTAxMDEwOyB0KyspCgl7CgkJa2lla1t0XSA9IGFuc1t0XSA9IDA7CgkJZm9yIChpbnQgZCA9IDA7IGQgPCAyNjsgZCsrKQoJCQlOWFRbdF1bZF0gPSAtMTsKCX0KCWlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8o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