#include <bits/stdc++.h>
using namespace std;
unsigned long long pw[67];
void pre() {
  pw[0] = 1;
  for (int i = 1; i < 64; i++) pw[i] = 2 * pw[i - 1];
}
int main() {
  ios_base::sync_with_stdio(false);
  pre();
  int n, m, j, k, l, f, i;
  string s, t;
  map<string, int> mp;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> s;
    mp[s]++;
  }
  for (i = 0; i < m; i++) {
    int ans = 0;
    cin >> s;
    map<string, int> bar;
    vector<int> v;
    int cnt = 0;
    for (f = 0; f < s.size(); f++) {
      if (s[f] == '?') {
        cnt++;
        v.push_back(f);
      }
    }
    if (cnt == 0) {
      ans = (ans + mp[s]);
      cout << ans << endl;
      continue;
    }
    if (cnt == 1) {
      for (j = 0; j < 6; j++) {
        t = s;
        if (j == 0) {
          t.erase(v[0], 1);
        } else {
          char ch = 'a' + j - 1;
          t[v[0]] = ch;
        }
        ans = (ans + mp[t]);
      }
    } else if (cnt == 2) {
      for (j = 0; j < 6; j++) {
        for (k = 0; k < 6; k++) {
          t = s;
          int foo = 0;
          if (j != 0) {
            char ch = 'a' + j - 1;
            t[v[0]] = ch;
          }
          if (k != 0) {
            char ch = 'a' + k - 1;
            t[v[1]] = ch;
          }
          if (j == 0) {
            t.erase(v[0], 1);
            foo++;
          }
          if (k == 0) {
            if (foo == 1)
              t.erase(v[1] - 1, 1);
            else
              t.erase(v[1], 1);
            foo++;
          }
          if (bar[t] == 0) {
            ans = (ans + mp[t]);
          }
          bar[t]++;
        }
      }
    } else if (cnt == 3) {
      for (j = 0; j < 6; j++) {
        for (k = 0; k < 6; k++) {
          for (l = 0; l < 6; l++) {
            t = s;
            int foo = 0;
            if (j != 0) {
              char ch = 'a' + j - 1;
              t[v[0]] = ch;
            }
            if (k != 0) {
              char ch = 'a' + k - 1;
              t[v[1]] = ch;
            }
            if (l != 0) {
              char ch = 'a' + l - 1;
              t[v[2]] = ch;
            }
            if (j == 0) {
              foo++;
              t.erase(v[0], 1);
            }
            if (k == 0) {
              if (foo == 1)
                t.erase(v[1] - 1, 1);
              else
                t.erase(v[1], 1);
              foo++;
            }
            if (l == 0) {
              if (foo == 2)
                t.erase(v[2] - 2, 1);
              else if (foo == 1)
                t.erase(v[2] - 1, 1);
              else
                t.erase(v[2], 1);
              foo++;
            }
            if (bar[t] == 0) {
              ans = (ans + mp[t]);
            }
            bar[t]++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
