#include <bits/stdc++.h>
using namespace std;
int n, m, x, a, b, ans;
bool tag[52];
char s[52];
map<pair<int, int>, int> S;
set<pair<int, int> > T;
inline void dfs(int no) {
  if (no == x + 1) {
    a = b = 0;
    for (int i = 1; i <= x; i++)
      if (!tag[i])
        a = (19260817ll * a + s[i] * 233) % 1000000007,
        b = (998244353ll * b + s[i] * 233) % 1000000009;
    if (!T.count(make_pair(a, b)))
      ans += S[make_pair(a, b)], T.insert(make_pair(a, b));
    return;
  }
  if (s[no] == '?') {
    for (int i = 0; i < 5; i++) s[no] = 'a' + i, dfs(no + 1), s[no] = '?';
    tag[no] = 1;
    dfs(no + 1);
    tag[no] = 0;
  } else
    dfs(no + 1);
}
int main() {
  cin >> n >> m;
  while (n--) {
    scanf("%s", s + 1), x = strlen(s + 1), a = b = 0;
    for (int i = 1; i <= x; i++)
      a = (19260817ll * a + s[i] * 233) % 1000000007,
      b = (998244353ll * b + s[i] * 233) % 1000000009;
    S[make_pair(a, b)]++;
  }
  while (m--) {
    scanf("%s", s + 1), x = strlen(s + 1);
    ans = 0, T.clear(), dfs(1);
    printf("%d\n", ans);
  }
  return 0;
}
