#2 #3 #4 三个点 WA 了,求调!
#include<bits/stdc++.h>
#include<cstring>
#include<string>
using namespace std;
int n,cnt=0,t,q;
int tree[3000005][70],value[3000005];
int change(char s){
if(s>='a'&&s<='z'){
return s-'a'+1;
}
if(s<='Z'&&s>='A'){
return s-'A'+27;
}
if(s>='0'&&s<='9'){
return s-'0'+57;
}
}
int find(string s){
int len=s.length(),dex;
int p=0;
for(int i=0;i<len;i++){
dex=change(s[i]);
if(tree[p][dex]==0){ //下标为 p 的节点的子节点中有该串
return 0;
}
p=tree[p][dex];
}
return value[p];
}
void add(string s){
int len=s.length(),dex;
int p=0;
for(int i=0;i<len;i++){
dex=change(s[i]);
if(tree[p][dex]==0){ //下标为 p 的节点的子节点中有该串
cnt++;
tree[p][dex]=cnt;
}
p=tree[p][dex];
value[p]++;
}
}
int main(){
cin>>t;
while(t--){
for(int i=1;i<=cnt;i++){
value[i]=0;
for(int j=0;j<=68;j++){
tree[i][j]=0;
}
}
cnt=0;
cin>>n>>q;
string s;
for(int i=1;i<=n;i++){
cin>>s;
//在 Trie 中查找 s
add(s);
}
for(int i=1;i<=q;i++){
cin>>s;
//在 Trie 中查找 s
printf("%d\n",find(s));
}
}
return 0;
}