#include <bits/stdc++.h>
using namespace std;
map<string, long long> mp;
set<string> ss;
string poss = "abcde", s;
long long ans;
void dfs(long long it, string cur = "") {
  if (it == s.length()) {
    if (mp.find(cur) != mp.end()) ss.insert(cur);
    return;
  }
  if (s[it] == '?') {
    dfs(it + 1, cur);
    cur += " ";
    for (long long i = 0; i < 5; i++) {
      cur[cur.length() - 1] = poss[i];
      dfs(it + 1, cur);
    }
  } else {
    cur += " ";
    cur[cur.length() - 1] = s[it];
    dfs(it + 1, cur);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  for (long long x = 0; x < n; x++) {
    cin >> s;
    if (mp.find(s) == mp.end())
      mp[s] = 1;
    else
      mp[s]++;
  }
  for (long long x = 0; x < m; x++) {
    cin >> s;
    ans = 0;
    ss.clear();
    dfs(0);
    for (auto it = ss.begin(); it != ss.end(); it++) {
      ans += mp[*it];
    }
    cout << ans << '\n';
  }
  return 0;
}
