#include<iostream>
#include<cstring>
using namespace std;
#define N 3000005
int tree[N][65];
int n,t,q,cnt;
int res[N];
string s;
int map(int x){
if(x<'A') return x-'0';
else if(x<'a') return x-'A'+10;
else return x-'a'+36;
}
void insert(string x){
int pos=0,len=x.size(),key;
for(int i=0;i<len;i++){
key=map(x[i]);
if(tree[pos][key]==0)
tree[pos][key]=++cnt;
pos=tree[pos][key];
res[pos]++;
}
}
int find(string x){
int pos=0,len=x.size(),key;
for(int i=0;i<len;i++){
key=map(x[i]);
if(tree[pos][key]==0) return 0;
pos=tree[pos][key];
}
return res[pos];
}
int main(){
ios::sync_with_stdio(0);
cin>>t;
while(t--){
cnt=n=q=0;
cin>>n>>q;
memset(tree,0,sizeof(tree));
memset(res,0,sizeof(res));
while(n--){
cin>>s;
insert(s);
}
while(q--){
cin>>s;
cout<<find(s)<<endl;
}
}
return 0;
}