#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
int ans = 0;
int indx[3];
void calc(int c, int i, string &s, map<string, bool> &vis) {
  if (i == c) {
    if (!vis[s]) {
      ans += mp[s];
      vis[s] = true;
    }
    return;
  }
  for (char ch = 'a'; ch <= 'f'; ch++) {
    s[indx[i]] = ch;
    if (ch == 'f') {
      s.erase(indx[i], 1);
      for (int j = i + 1; j < c; j++) indx[j]--;
    }
    calc(c, i + 1, s, vis);
    if (ch == 'f') {
      s.insert(indx[i], "?");
      for (int j = i + 1; j < c; j++) indx[j]++;
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    mp[s]++;
  }
  for (int i = 0; i < m; i++) {
    ans = 0;
    map<string, bool> vis;
    string s;
    cin >> s;
    int c = 0;
    for (int j = 0; j < s.size(); j++)
      if (s[j] == '?') {
        indx[c] = j;
        c++;
      }
    calc(c, 0, s, vis);
    cout << ans << endl;
  }
  return 0;
}
