#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 10, MAX = 2e5 + 1e4, MOD = 1e9 + 7, MAXL = 25;
void OUT(long double o, int x) {
  cout << fixed << setprecision(x) << o;
  return;
}
string ss[40] = {"", "a", "b", "c", "d", "e"};
map<string, int> mp, mp2;
vector<int> v;
vector<string> vs;
string s;
long long ans = 0;
void bt(string k, long long l) {
  if (l == v.size()) {
    string p = "";
    for (int i = 0; i < vs.size() - 1; i++)
      p += vs[i] + ss[int(k[i]) - int('0')];
    p += vs[vs.size() - 1];
    if (!mp2[p]) {
      ans += mp[p];
      mp2[p] = 1;
    }
  } else {
    for (int i = 0; i < 6; i++) bt(k + char(int('0') + i), l + 1);
  }
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string q;
    cin >> q;
    mp[q]++;
  }
  for (int i = 0; i < m; i++) {
    ans = 0;
    cin >> s;
    string g = "";
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '?') {
        v.push_back(j);
        vs.push_back(g);
        g = "";
      } else
        g += s[j];
    }
    vs.push_back(g);
    bt("", 0);
    v.clear();
    vs.clear();
    mp2.clear();
    cout << ans << "\n";
  }
  return 0;
}
