#include <bits/stdc++.h>
using namespace std;
int n, m;
map<pair<int, int>, pair<int, int> > A;
unsigned long long hashingbase10(string s);
unsigned long long hashingbase23(string s);
int george(string x, int lvl, vector<int> poss, int cur);
int main() {
  cin >> n >> m;
  cin.ignore();
  for (int i = 0; i < n; i++) {
    string x;
    getline(cin, x);
    unsigned long long t = hashingbase10(x), h = hashingbase23(x);
    if (A.count({t, h}))
      A[{t, h}].second++;
    else
      A.insert({{t, h}, {-1, 1}});
  }
  for (int i = 0; i < m; i++) {
    string x;
    getline(cin, x);
    vector<int> poss;
    for (int j = 0; j < x.size(); j++) {
      if (x[j] == '?') poss.push_back(j);
    }
    cout << george(x, 0, poss, i) << "\n";
  }
}
unsigned long long hashingbase10(string s) {
  unsigned long long ret = 0, jojo = 1;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != '!') {
      ret += s[i] * jojo;
      ret %= (unsigned long long)(1e12) + (unsigned long long)(1e9) + 7ull;
      jojo *= 10;
    }
  }
  return ret;
}
unsigned long long hashingbase23(string s) {
  unsigned long long ret = 0, jojo = 1;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != '!') {
      ret += s[i] * jojo;
      ret %= (unsigned long long)(1e13) + (unsigned long long)(1e8) + 9ull;
      jojo *= 23;
    }
  }
  return ret;
}
int george(string x, int lvl, vector<int> poss, int cur) {
  int ans = 0;
  if (lvl == poss.size()) {
    unsigned long long jojo = hashingbase10(x), j0j0 = hashingbase23(x);
    if (A.count({jojo, j0j0}) && A[{jojo, j0j0}].first != cur) {
      A[{jojo, j0j0}].first = cur;
      ans = A[{jojo, j0j0}].second;
    }
  } else {
    for (int i = 0; i < 6; i++) {
      if (i) {
        x[poss[lvl]] = 'a' + i - 1;
      } else {
        x[poss[lvl]] = '!';
      }
      ans += george(x, lvl + 1, poss, cur);
    }
  }
  return ans;
}
