P8306
#include <bits/stdc++.h>
using namespace std;
int tire[3000010][66],tot,t[3000010];
inline int get(char c){
if(c>='A'&&c<='Z'){
return c-'A';
}else if(c>='a'&&c<='z'){
return c-'a'+26;
}else if(c>='0'&&c<='9'){
return c-'0'+52;
}
}
inline void insert(string s){
int rt=0;
for(int i=0;s[i];i++){
int c=get(s[i]);
if(!tire[rt][c]){
tire[rt][c]=++tot;
}
rt=tire[rt][c];
t[rt]++;
}
return;
}
inline int find(string s){
int rt=0;
for(int i=0;s[i];i++){
int c=get(s[i]);
if(!tire[rt][c]){
return 0;
}
rt=tire[rt][c];
}
return t[rt];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int xh;
cin>>xh;
for(int i=0;i<xh;i++){
for(int j=0;j<=tot;j++){
memset(tire[j],0,sizeof(tire[j]));
}
memset(t,0,sizeof(t));
tot=0;
int x,h;
string z;
cin>>x>>h;
for(int j=0;j<x;j++){
cin>>z;
insert(z);
z.clear();
}
for(int j=0;j<h;j++){
cin>>z;
cout<<find(z)<<"\n";
z.clear();
}
}
return 0;
}