#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAX = 1e5 + 10;
long long base = uniform_int_distribution<int>(30, 1e7)(rng);
long long mod[2] = {592123193, 737707171};
int n, m;
string s, s1;
pair<long long, long long> hash_string(string s) {
  long long basepow[2] = {1ll, 1ll};
  pair<long long, long long> val = {0, 0};
  for (auto &c : s) {
    val.first = (val.first + (c - 'a' + 1) * basepow[0]) % mod[0];
    val.second = (val.second + (c - 'a' + 1) * basepow[1]) % mod[1];
    basepow[0] = (basepow[0] * base) % mod[0];
    basepow[1] = (basepow[1] * base) % mod[1];
  }
  return val;
}
map<pair<long long, long long>, int> mp, vis;
int ans = 0;
int cur;
void solve(int idx) {
  if (idx == s.size()) {
    pair<long long, long long> p = hash_string(s1);
    if (vis[p] != cur) {
      ans += mp[p];
      vis[p] = cur;
    }
    return;
  }
  if (s[idx] != '?') {
    s1.push_back(s[idx]);
    solve(idx + 1);
    s1.pop_back();
  } else {
    solve(idx + 1);
    for (char c = 'a'; c <= 'e'; ++c) {
      s1.push_back(c);
      solve(idx + 1);
      s1.pop_back();
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    mp[hash_string(s)]++;
  }
  for (int i = 1; i <= m; ++i) {
    cin >> s;
    s1 = "";
    ans = 0;
    cur = i;
    solve(0);
    cout << ans << "\n";
  }
  return 0;
}
