#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e6 + 5;
int book[maxn];
char str[55];
char pat[55];
int k;
struct acho {
  int nex[maxn][7], cnt[maxn], fail[maxn];
  int L, root;
  int newnode() {
    for (int i = 0; i < 7; i++) nex[L][i] = -1;
    cnt[L++] = 0;
    return L - 1;
  }
  void init() {
    L = 0;
    root = newnode();
  }
  void insert(char *a) {
    int now = root;
    for (int i = 0; a[i]; i++) {
      if (nex[now][a[i] - 'a'] == -1) {
        nex[now][a[i] - 'a'] = newnode();
      }
      now = nex[now][a[i] - 'a'];
    }
    cnt[now]++;
    return;
  }
  queue<int> que;
  void build() {
    while (que.empty() == 0) que.pop();
    int now = root;
    for (int i = 0; i < 7; i++) {
      if (nex[now][i] == -1)
        nex[now][i] = root;
      else {
        fail[nex[now][i]] = root;
        que.push(nex[now][i]);
      }
    }
    while (que.empty() == 0) {
      now = que.front();
      que.pop();
      for (int i = 0; i < 7; i++) {
        if (nex[now][i] == -1) {
          nex[now][i] = nex[fail[now]][i];
        } else {
          fail[nex[now][i]] = nex[fail[now]][i];
          que.push(nex[now][i]);
        }
      }
    }
    return;
  }
  int match(char *a) {
    int res = 0;
    k = 0;
    int now = root;
    for (int i = 0; a[i]; i++) {
      if (nex[now][a[i] - 'a'] == -1) break;
      now = nex[now][a[i] - 'a'];
      if (cnt[now] > 0 && !a[i + 1] && book[now] == 0) {
        res += cnt[now];
        book[now] = 1;
      }
    }
    return res;
    ;
  }
} acho;
char sur[55];
int ans, len;
void dfs(int x, int y) {
  if (x == len) {
    sur[y] = '\0';
    ans += acho.match(sur);
    return;
  }
  if (pat[x] == '?') {
    for (int i = 0; i < 5; i++) {
      sur[y] = 'a' + i;
      dfs(x + 1, y + 1);
    }
    dfs(x + 1, y);
  } else {
    sur[y] = pat[x];
    dfs(x + 1, y + 1);
  }
}
int main() {
  int n, m, i, j;
  cin >> n >> m;
  acho.init();
  for (i = 0; i < n; i++) {
    scanf("%s", str);
    acho.insert(str);
  }
  int num, pos[5], k = 0;
  for (i = 0; i < m; i++) {
    scanf("%s", pat);
    len = strlen(pat);
    ans = 0;
    for (j = 0; pat[j]; j++) {
      if (pat[j] == '?') {
        pos[k++] = j;
      }
    }
    for (int i = 0; i < acho.L; i++) book[i] = 0;
    dfs(0, 0);
    printf("%d\n", ans);
  }
}
