#include <bits/stdc++.h>
using namespace std;
int base[4] = {141, 163};
const unsigned long long MOD1 = 1e9 + 7, MOD2 = 1e9 + 9;
map<pair<unsigned long long, unsigned long long>, int> mp;
set<pair<unsigned long long, unsigned long long> > st;
char s[100100];
int ans = 0;
void dfs(int k, unsigned long long h1, unsigned long long h2, int len) {
  if (k == len) {
    if (!st.count(make_pair(h1, h2))) {
      ans += mp[make_pair(h1, h2)];
      st.insert(make_pair(h1, h2));
    }
    return;
  }
  if (s[k] == '?') {
    for (int i = 0; i <= 'e' - 'a'; i++) {
      dfs(k + 1, (h1 * 233 + i + 1) % MOD1, (h2 * 666 + i + 1) % MOD2, len);
    }
    dfs(k + 1, h1, h2, len);
  } else {
    dfs(k + 1, (h1 * 233 + s[k] - 'a' + 1) % MOD1,
        (h2 * 666 + s[k] - 'a' + 1) % MOD2, len);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    int len = strlen(s);
    unsigned long long h = 0, g = 0;
    for (int j = 0; j < len; j++) {
      h = (h * 233 + s[j] - 'a' + 1) % MOD1;
      g = (g * 666 + s[j] - 'a' + 1) % MOD2;
    }
    mp[make_pair(h, g)]++;
  }
  for (int i = 1; i <= m; i++) {
    st.clear();
    scanf("%s", s);
    ans = 0;
    dfs(0, 0, 0, strlen(s));
    printf("%d\n", ans);
  }
}
