#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    mp[s]++;
  }
  char ch[] = {'a', 'b', 'c', 'd', 'e', '*'};
  for (int i = 0; i < m; i++) {
    map<string, bool> vis;
    string s;
    cin >> s;
    int c = 0;
    int indx[3];
    for (int j = 0; j < s.size(); j++)
      if (s[j] == '?') {
        indx[c] = j;
        c++;
      }
    if (c == 0) {
      cout << mp[s] << endl;
      continue;
    }
    int ans = 0;
    if (c == 1) {
      for (char ch = 'a'; ch <= 'e'; ch++) {
        s[indx[0]] = ch;
        ans += mp[s];
      }
      s.erase(indx[0], 1);
      ans += mp[s];
    }
    if (c == 2) {
      string ss = s;
      for (char ch1 = 'a'; ch1 <= 'f'; ch1++) {
        ss[indx[0]] = ch1;
        if (ch1 == 'f') {
          ss.erase(indx[0], 1);
          indx[1]--;
        }
        for (char ch2 = 'a'; ch2 <= 'f'; ch2++) {
          ss[indx[1]] = ch2;
          if (ch2 == 'f') ss.erase(indx[1], 1);
          if (!vis[ss]) {
            ans += mp[ss];
            vis[ss] = true;
          }
          if (ch2 == 'f') {
            ss.insert(indx[1], "?");
          }
        }
        if (ch1 == 'f') {
          ss.insert(indx[0], "?");
          indx[1]++;
        }
      }
    }
    if (c == 3) {
      string ss = s;
      for (char ch1 = 'a'; ch1 <= 'f'; ch1++) {
        ss[indx[0]] = ch1;
        if (ch1 == 'f') {
          ss.erase(indx[0], 1);
          indx[1]--;
          indx[2]--;
        }
        for (char ch2 = 'a'; ch2 <= 'f'; ch2++) {
          ss[indx[1]] = ch2;
          if (ch2 == 'f') {
            ss.erase(indx[1], 1);
            indx[2]--;
          }
          for (char ch3 = 'a'; ch3 <= 'f'; ch3++) {
            ss[indx[2]] = ch3;
            if (ch3 == 'f') ss.erase(indx[2], 1);
            if (!vis[ss]) {
              ans += mp[ss];
              vis[ss] = true;
            }
            if (ch3 == 'f') {
              ss.insert(indx[2], "?");
            }
          }
          if (ch2 == 'f') {
            ss.insert(indx[1], "?");
            indx[2]++;
          }
        }
        if (ch1 == 'f') {
          ss.insert(indx[0], "?");
          indx[1]++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
