#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
int n, m;
map<string, int> mp;
map<string, bool> T;
string tar(int x) {
  if (x == -1) return "";
  string answ = "";
  answ += char('a' + x);
  return answ;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    mp[s]++;
  }
  for (int i = 1; i <= m; i++) {
    string s;
    cin >> s;
    string ss[5];
    int k = 0, ind = 0;
    ss[0] = ss[1] = ss[2] = ss[3] = "";
    for (int j = 0; j < (int)s.size(); j++) {
      if (s[j] == '?') {
        ind++;
        k++;
      } else
        ss[ind] += s[j];
    }
    int pat = 0;
    if (k == 0) {
      pat += mp[ss[0]];
    }
    if (k == 1) {
      for (int j = -1; j <= 4; j++) {
        string answ = ss[0] + tar(j) + ss[1];
        if (!T[answ]) {
          pat += mp[answ];
          T[answ] = true;
        }
      }
    }
    if (k == 2) {
      for (int j = -1; j <= 4; j++) {
        for (int h = -1; h <= 4; h++) {
          string answ = ss[0] + tar(j) + ss[1] + tar(h) + ss[2];
          if (!T[answ]) {
            pat += mp[answ];
            T[answ] = true;
          }
        }
      }
    }
    if (k == 3) {
      for (int j = -1; j <= 4; j++) {
        for (int h = -1; h <= 4; h++) {
          for (int t = -1; t <= 4; t++) {
            string answ =
                ss[0] + tar(j) + ss[1] + tar(h) + ss[2] + tar(t) + ss[3];
            if (!T[answ]) {
              pat += mp[answ];
              T[answ] = true;
            }
          }
        }
      }
    }
    T.clear();
    printf("%d\n", pat);
  }
  return 0;
}
