#include<bits/stdc++.h>
using namespace std;
int n,q,T;
int main(){
ios::sync_with_stdio(0);
cin>>T;
while(T--){
unordered_map<string,int>mp;
string st;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>st;
int len=st.size();
string ins="";
for(int j=0;j<len;j++){
ins+=st[j];
mp[ins]++;
}
}
for(int i=1;i<=q;i++){
cin>>st;
cout<<mp[st]<<endl;
}
}
return 0;
}