#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10005;
int n, m, now, ans;
map<string, int> mp;
set<string> vis;
string x, ss[5];
char s[55], f[5];
char listc[] = "abcde ";
int mp_find(const string &now) {
  if (vis.find(now) != vis.end()) return 0;
  vis.insert(now);
  auto it = mp.find(now);
  if (it != mp.end()) {
    return it->second;
  }
  return 0;
}
void dfs(int deep) {
  if (deep == now + 1) {
    string que = ss[0];
    for (int i = 1; i <= now; ++i) {
      if (f[i] != ' ') {
        que += f[i];
      }
      que += ss[i];
    }
    ans += mp_find(que);
    return;
  }
  for (int i = 0; i <= 5; ++i) {
    f[deep] = listc[i];
    dfs(deep + 1);
  }
}
int main() {
  ios::sync_with_stdio(true);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    mp[x]++;
  }
  while (m--) {
    vis.clear();
    cin >> s;
    int l = strlen(s);
    for (int i = 0; i <= 4; ++i) {
      ss[i] = "";
    }
    now = 0;
    ans = 0;
    for (int i = 0; i < l; ++i) {
      if (s[i] == '?') {
        ++now;
      } else {
        ss[now] += s[i];
      }
    }
    dfs(1);
    cout << ans << endl;
  }
  return 0;
}
