#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  char ch;
  while ((ch = getchar()), (ch < '0' || ch > '9'))
    ;
  x = ch - '0';
  while ((ch = getchar()), (ch >= '0' && ch <= '9')) x = x * 10 + (ch - '0');
}
inline void readstr(char *str) {
  char ch;
  while ((ch = getchar()), ((ch < 'a' || ch > 'z') && (ch != '?')))
    ;
  *(str++) = ch;
  while ((ch = getchar()), ((ch >= 'a' && ch <= 'z') || (ch == '?')))
    *(str++) = ch;
  *(str++) = 0;
}
const int MAXNODE = 100010 * 50;
namespace trie {
int ch[MAXNODE][5], size[MAXNODE], pool = 2;
int vis[MAXNODE], vid = 1;
inline void ins(char *str) {
  char c;
  int u = 1;
  while (c = *(str++)) {
    c -= 'a';
    if (!ch[u][c]) ch[u][c] = pool++;
    u = ch[u][c];
  }
  size[u]++;
}
char pat[60];
int ans;
inline void find(int idx, int u) {
  char c = pat[idx];
  if (c == 0) {
    if (vis[u] != vid) ans += size[u], vis[u] = vid;
  } else if (c == '?') {
    find(idx + 1, u);
    for (int i = 0; i < 5; i++)
      if (ch[u][i]) find(idx + 1, ch[u][i]);
  } else {
    c -= 'a';
    if (ch[u][c]) find(idx + 1, ch[u][c]);
  }
}
inline int work() {
  vid++;
  ans = 0;
  find(0, 1);
  return ans;
}
}  // namespace trie
int N, M;
char str[60];
int main() {
  int i;
  read(N);
  read(M);
  for (i = 1; i <= N; i++) readstr(str), trie::ins(str);
  for (i = 1; i <= M; i++) readstr(trie::pat), printf("%d\n", trie::work());
}
