#include <bits/stdc++.h>
using namespace std;
int tree[2500005][5], sum[2500005], clk, n, m, ans, use[2500005];
char s[55];
void Insert() {
  int len = strlen(s), id = 0;
  sum[id]++;
  for (int i = 0; i < len; i++) {
    if (tree[id][s[i] - 'a'] == 0) tree[id][s[i] - 'a'] = clk++;
    id = tree[id][s[i] - 'a'];
    sum[id]++;
  }
}
void query(int id, int now, int len, int ii) {
  if (now == len) {
    int cnt = 0;
    if (use[id] < ii)
      use[id] = ii;
    else
      return;
    for (int i = 0; i < 5; i++) {
      if (tree[id][i] != 0) cnt += sum[tree[id][i]];
    }
    ans += sum[id] - cnt;
    return;
  }
  if (s[now] == '?') {
    for (int i = 0; i < 5; i++) {
      if (tree[id][i] != 0) query(tree[id][i], now + 1, len, ii);
    }
    query(id, now + 1, len, ii);
  } else {
    if (tree[id][s[now] - 'a'] != 0)
      query(tree[id][s[now] - 'a'], now + 1, len, ii);
  }
}
int main() {
  clk = 1;
  memset(tree, 0, sizeof(tree));
  memset(sum, 0, sizeof(sum));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    Insert();
  }
  for (int i = 1; i <= m; i++) {
    scanf("%s", s);
    ans = 0;
    query(0, 0, strlen(s), i);
    printf("%d\n", ans);
  }
  return 0;
}
