#include <bits/stdc++.h>
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
using namespace std;
const int N = 1e6 + 10;
map<string, int> mp, H;
int pos[5];
int n, m, ans;
string s;
int main(int argc, char* argv[]) {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> s;
    mp[s] += 1;
  }
  int now = 0;
  for (int i = 1; i <= m; ++i) {
    ans = 0;
    H.clear();
    cin >> s;
    string tmp = "", tt = "";
    int cnt = 0;
    for (int j = 0; j < s.length(); ++j) {
      if (s[j] == '?') pos[++cnt] = j;
    }
    tmp = s;
    if (cnt == 0) {
      ans += mp[tmp];
    } else if (cnt == 1) {
      for (int k = 'a'; k <= 'e' + 1; ++k) {
        s = tmp;
        if (k <= 'e') s[pos[1]] = k;
        tt = "";
        for (int j = 0; j < s.length(); ++j)
          if (s[j] != '?') tt += s[j];
        if (!H[tt]) ans += mp[tt], H[tt] = 1;
      }
    } else if (cnt == 2) {
      for (int k = 'a'; k <= 'e' + 1; ++k)
        for (int k1 = 'a'; k1 <= 'e' + 1; ++k1) {
          s = tmp;
          if (k <= 'e') s[pos[1]] = k;
          if (k1 <= 'e') s[pos[2]] = k1;
          tt = "";
          for (int j = 0; j < s.length(); ++j)
            if (s[j] != '?') tt += s[j];
          if (!H[tt]) ans += mp[tt], H[tt] = 1;
        }
    } else if (cnt == 3) {
      for (int k = 'a'; k <= 'e' + 1; ++k)
        for (int k1 = 'a'; k1 <= 'e' + 1; ++k1)
          for (int k2 = 'a'; k2 <= 'e' + 1; ++k2) {
            s = tmp;
            if (k <= 'e') s[pos[1]] = k;
            if (k1 <= 'e') s[pos[2]] = k1;
            if (k2 <= 'e') s[pos[3]] = k2;
            tt = "";
            for (int j = 0; j < s.length(); ++j)
              if (s[j] != '?') tt += s[j];
            if (!H[tt]) ans += mp[tt], H[tt] = 1;
          }
    }
    cout << ans << endl;
  }
  return 0;
}
