RT
//by olkieler
#include <bits/stdc++.h>
#define linf LLONG_MAX
#define iinf INT_MAX
#define ios ios::sync_with_stdio(0);cin.tie(0);
#define N 3000005
#define M 105
#define mod 1000000007
#define pint pair<int, int>
#define mp make_pair
#define fi first
#define se second;
using namespace std;
inline int r(){int x;cin >> x;return x;}
inline void w(int x){cout << x << '\n';}
inline void W(int x){cout << x << ' ';}
int tot;
int rk[125];
int num[N][65];
int ans[N];
inline void insert(string st)
{
int now = 0;
for (int i = 0; i < st.size(); i ++)
{
if (!num[now][rk[st[i]]])
{
num[now][rk[st[i]]] = ++ tot;
}
now = num[now][rk[st[i]]];
ans[now] ++;
}
}
inline int query(string st)
{
int now = 0;
for (int i = 0; i < st.size(); i ++)
{
now = num[now][rk[st[i]]];
}
return ans[now];
}
int main()
{
ios;
// freopen("P8306_1.in", "r", stdin);
// freopen("my.out", "w", stdout);
int t = r();
for (int i = 0; i < 26; i ++)
{
rk['a' + i] = i + 1;
rk['A' + i] = i + 27;
}
for (int i = 0; i <= 9; i ++)
{
rk['0' + i] = i + 53;
}
for (int asdf = 1; asdf <= t; asdf ++)
{
for (int i = 0; i <= tot; i ++)
{
ans[i] = 0;
for (int j = 0; j < 65; j ++)
{
num[i][j] = 0;
}
}
tot = 0;
int n = r(), q = r();
for (int i = 1; i <= n; i ++)
{
string st;
cin >> st;
insert(st);
}
for (int i = 1; i <= q; i ++)
{
string st;
cin >> st;
w(query(st));
}
}
return 0;
}