#include <bits/stdc++.h>
using namespace std;
const int MAXL = 55;
const int MAXN = 4e6;
int n, m;
struct Trie {
  int c[MAXN][7], val[MAXN], qes[5], qs, cnt = 0, ans;
  char st[MAXL];
  bool bo[MAXN];
  void clear() { memset(bo, false, sizeof(bo)); }
  void ins() {
    int len = strlen(st), now = 0;
    for (int i = 0; i < len; ++i) {
      int v = st[i] - 'a';
      if (!c[now][v]) c[now][v] = ++cnt;
      now = c[now][v];
    }
    val[now]++;
  }
  void qqq() {
    int len = strlen(st), now = 0;
    for (int i = 0; i < len; ++i) {
      int v = st[i] - 'a';
      if (v >= 5) continue;
      if (!c[now][v]) return;
      now = c[now][v];
    }
    if (!bo[now]) ans += val[now];
    bo[now] = true;
  }
  void dfs(int now) {
    if (now == qs) {
      qqq();
      return;
    }
    for (int i = 0; i < 6; ++i) {
      st[qes[now]] = 'a' + i;
      dfs(now + 1);
      st[qes[now]] = '?';
    }
  }
  void query() {
    qs = 0;
    ans = 0;
    int len = strlen(st);
    for (int i = 0; i < len; ++i)
      if (st[i] == '?') qes[qs++] = i;
    dfs(0);
  }
};
Trie Tr;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%s", Tr.st);
    Tr.ins();
  }
  for (int i = 0; i < m; ++i) {
    Tr.clear();
    scanf("%s", Tr.st);
    Tr.query();
    printf("%d\n", Tr.ans);
  }
  return 0;
}
