#include <bits/stdc++.h>
using namespace std;
#define ll int
unordered_map<ll,ll>vis;
unordered_map<ll,ll>trie[26];
ll ans[100005];
ll sz;
void in(char s[])
{
ll st=0,dep=0,mi=0;
for (ll i=0;i<strlen(s);i++)
{
dep+=1;
ll cur=s[i]-97;
if(!vis[trie[cur][st]])
{
trie[cur][st]=++sz;
}
vis[trie[cur][st]]+=1;
if(i==0)
mi=vis[trie[cur][st]];
else
{
mi=min(mi,vis[trie[cur][st]]);
}
ans[dep]=max(ans[dep],mi);
st=trie[cur][st];
}
}
char s[100005];
int main()
{
ll i,j,k,n,m,t;
scanf("%d",&t);
while(t--)
{
ll q;
sz=0;
for(i=0;i<=100000;i++)
{
ans[i]=0;
}
for(i=0;i<26;i++) trie[i].clear();
vis.clear();
scanf("%d %d",&n,&q);
while(n--)
{
scanf("%s",s);
in(s);
}
while(q--)
{
scanf("%d",&j);
printf("%d\n",ans[j]);
}
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgaW50CnVub3JkZXJlZF9tYXA8bGwsbGw+dmlzOwp1bm9yZGVyZWRfbWFwPGxsLGxsPnRyaWVbMjZdOwpsbCBhbnNbMTAwMDA1XTsKbGwgc3o7CnZvaWQgaW4oY2hhciBzW10pCnsKICAgIGxsIHN0PTAsZGVwPTAsbWk9MDsKICAgIGZvciAobGwgaT0wO2k8c3RybGVuKHMpO2krKykKICAgIHsKICAgICAgICBkZXArPTE7CiAgICAgICAgbGwgY3VyPXNbaV0tOTc7CiAgICAgICAgaWYoIXZpc1t0cmllW2N1cl1bc3RdXSkKICAgICAgICB7CiAgICAgICAgICAgIHRyaWVbY3VyXVtzdF09KytzejsKICAgICAgICB9CgogICAgICAgIHZpc1t0cmllW2N1cl1bc3RdXSs9MTsKICAgICAgICBpZihpPT0wKQogICAgICAgICAgICBtaT12aXNbdHJpZVtjdXJdW3N0XV07CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgIG1pPW1pbihtaSx2aXNbdHJpZVtjdXJdW3N0XV0pOwogICAgICAgIH0KICAgICAgICBhbnNbZGVwXT1tYXgoYW5zW2RlcF0sbWkpOwogICAgICAgIHN0PXRyaWVbY3VyXVtzdF07CiAgICB9Cn0KCgpjaGFyIHNbMTAwMDA1XTsKaW50IG1haW4oKQp7CiAgICBsbCBpLGosayxuLG0sdDsKICAgIHNjYW5mKCIlZCIsJnQpOwogICAgd2hpbGUodC0tKQogICAgewogICAgICAgIGxsIHE7CiAgICAgICAgc3o9MDsKICAgICAgICBmb3IoaT0wO2k8PTEwMDAwMDtpKyspCiAgICAgICAgewogICAgICAgICAgICBhbnNbaV09MDsKICAgICAgICB9CiAgICAgICAgZm9yKGk9MDtpPDI2O2krKykgdHJpZVtpXS5jbGVhcigpOwogICAgICAgIHZpcy5jbGVhcigpOwogICAgICAgIHNjYW5mKCIlZCAlZCIsJm4sJnEpOwogICAgICAgIHdoaWxlKG4tLSkKICAgICAgICB7CiAgICAgICAgICAgIHNjYW5mKCIlcyIscyk7CiAgICAgICAgICAgIGluKHMpOwogICAgICAgIH0KICAgICAgICB3aGlsZShxLS0pCiAgICAgICAgewogICAgICAgICAgICBzY2FuZigiJWQiLCZqKTsKICAgICAgICAgICAgcHJpbnRmKCIlZFxuIixhbnNbal0pOwogICAgICAgIH0KCiAgICB9Cgp9Cg==