#include <bits/stdc++.h>
using namespace std;
int ch_64[256];
char ch_256[64];
inline void ch_init()
{
for(int i=0;i<256;i++)
ch_64[i]=-1;
for(int i='a';i<='z';i++)
ch_64[i]=i-'a';
for(int i='A';i<='Z';i++)
ch_64[i]=i-'A'+26;
for(int i='0';i<='9';i++)
ch_64[i]=i-'0'+52;
ch_64['_']=62;
ch_64['.']=63;
for(int i=0;i<256;i++)
if(ch_64[i]!=-1)
ch_256[ch_64[i]]=i;
}
class TreeNode{
public:
bool isword;
int dfs_res;
TreeNode *nxt[64];
TreeNode()
{
isword=false;
dfs_res=-1;
for(int i=0;i<64;i++)
nxt[i]=NULL;
}
};
class Trie{
public:
void add(string str)
{
TreeNode *now=root;
for(int i=0;i<(int)str.size();i++)
{
if(now->nxt[ch_64[str[i]]]==NULL)
now->nxt[ch_64[str[i]]]=new TreeNode;
now=now->nxt[ch_64[str[i]]];
}
now->isword=true;
}
int qt(string str)
{
TreeNode *now=root;
for(int i=0;i<(int)str.size();i++)
{
if(now->nxt[ch_64[str[i]]]==NULL)
return 0;
now=now->nxt[ch_64[str[i]]];
}
return dfs(now);
}
private:
TreeNode *root=new TreeNode;
int dfs(TreeNode *rt)
{
if(rt==NULL)
return 0;
if(rt->dfs_res!=-1)
return rt->dfs_res;
int ret=0;
for(int i=0;i<64;i++)
if(rt->nxt[i]!=NULL)
ret+=dfs(rt->nxt[i]);
ret+=rt->isword;
return rt->dfs_res=ret;
}
};
int T,n,q;
string s;
int main()
{
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
ch_init();
cin>>T;
while(T--)
{
Trie trie;
cin>>n>>q;
while(n--)
{
cin>>s;
trie.add(s);
}
while(q--)
{
cin>>s;
cout<<trie.qt(s)<<'\n';
}
}
return 0;
}