#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int M = 5005;
int n, m, trie[N * 50][6], num[N * 50], size;
char s[55];
bool vis[N * 50] = {0};
void init() {
  size = 0;
  num[0] = 0;
  for (int i = 0; i < 5; i++) trie[0][i] = 0;
}
void insert(char *t) {
  int u = 0, v;
  int len = strlen(t);
  for (int i = 0; i < len; i++) {
    v = t[i] - 'a';
    if (!trie[u][v]) {
      trie[u][v] = ++size;
      num[size] = 0;
      for (int j = 0; j < 5; j++) trie[size][j] = 0;
    }
    u = trie[u][v];
  }
  num[u]++;
}
void dfs(char *t, int u, int cur, int len, int &ans) {
  if (cur == len) {
    if (!vis[u]) ans += num[u], vis[u] = true;
    return;
  }
  int v = t[cur] - 'a';
  if (t[cur] == '?') {
    dfs(t, u, cur + 1, len, ans);
    for (int i = 0; i < 5; i++)
      if (trie[u][i]) dfs(t, trie[u][i], cur + 1, len, ans);
  } else if (trie[u][v])
    dfs(t, trie[u][v], cur + 1, len, ans);
}
int main() {
  scanf("%d%d", &n, &m);
  init();
  while (n--) {
    scanf("%s", s);
    insert(s);
  }
  while (m--) {
    scanf("%s", s);
    int ans = 0;
    for (int i = 0; i <= size; i++) vis[i] = false;
    dfs(s, 0, 0, strlen(s), ans);
    printf("%d\n", ans);
  }
  return 0;
}
