#include <bits/stdc++.h>
using namespace std;
unordered_map<string, int> Map, Map2;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, mm;
  cin >> n >> mm;
  string s;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    Map[s]++;
  }
  string a[5] = {};
  int m[6] = {};
  int res, vt;
  bool br = 0;
  string t[4];
  for (int i = 1; i <= mm; i++) {
    cin >> s;
    t[1] = "";
    t[2] = "";
    t[3] = "";
    a[1] = "";
    a[2] = "";
    a[3] = "";
    a[4] = "";
    m[1] = 0;
    m[2] = 0;
    m[3] = 0;
    br = 0;
    int co = 1;
    for (auto i : s) {
      if (i == '?')
        co++;
      else
        a[co].push_back(i);
    }
    co--;
    if (!co) {
      cout << Map[s] << "\n";
      continue;
    }
    res = 0;
    vt = co;
    for (;;) {
      string de = a[1] + t[1] + a[2] + t[2] + a[3] + t[3] + a[4];
      m[vt]++;
      while (m[vt] == 7) {
        m[vt] = 0;
        vt--;
        if (vt == 0) {
          br = 1;
          break;
        }
        m[vt]++;
      }
      vt = co;
      for (int j = 1; j <= 3; j++)
        if (m[j])
          t[j] = char(m[j] + 'a' - 1);
        else
          t[j] = "";
      if (br) break;
      if (Map2[de] == i) continue;
      Map2[de] = i;
      res += Map[de];
    }
    cout << res << "\n";
  }
}
