#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int N = 3000005;
char s[N];
int ch[N][65], cnt[N], idx;
int fun(char x) {
if (x >= 'A' && x <= 'Z') {
return x - 'A';
}
else if (x >= 'a' && x <= 'z') {
return x - 'a' + 26;
}
else {
return x - '0' + 52;
}
}
void insert(char* s) {
int p = 0;
for (int i = 0; s[i]; i++) {
int j = fun(s[i]);
// 如果没有编号,要对这个节点进行编号
if (!ch[p][j]) ch[p][j] = ++idx;
p = ch[p][j]; // 获取当前节点
cnt[p]++;
}
}
int query(char* s) {
int p = 0;
for (int i = 0; s[i]; i++) {
int j = fun(s[i]);
if (!ch[p][j]) return 0;
p = ch[p][j];
}
return cnt[p];
}
int main() {
int t;
cin >> t;
while (t--) {
//memset(cnt, 0, sizeof cnt);
//memset(ch, 0, sizeof ch);
for (int i = 0; i < idx; i++) {
for (int j = 0; j < 62; j++) ch[i][j] = 0;
cnt[i] = 0;
}
idx = 0;
int n, q;
cin >> n >> q;
//char s[N];
for (int i = 1; i <= n; i++) {
scanf("%s", s);
insert(s);
}
for (int j = 1; j <= q; j++) {
scanf("%s", s);
cout << query(s) << endl;
}
}
}