#include <bits/stdc++.h>
using namespace std;
int n, m;
map<string, int> f;
char inp[100];
vector<string> prefix;
set<string> ss;
string sub(int l, int r) {
  string res = "";
  for (int i = l; i <= r; i++) {
    res += inp[i];
  }
  return res;
}
int solve(vector<int> v) {
  int len = strlen(inp);
  int res = 0;
  ss.clear();
  if (v.size() == 3) {
    for (int i = 0; i < 6; i++) {
      for (int j = 0; j < 6; j++) {
        for (int k = 0; k < 6; k++) {
          string s = sub(0, v[0] - 1) + prefix[i] + sub(v[0] + 1, v[1] - 1) +
                     prefix[j] + sub(v[1] + 1, v[2] - 1) + prefix[k] +
                     sub(v[2] + 1, len - 1);
          if (ss.find(s) != ss.end()) continue;
          ss.insert(s);
          res += f[s];
        }
      }
    }
  } else if (v.size() == 2) {
    for (int i = 0; i < 6; i++) {
      for (int j = 0; j < 6; j++) {
        string s = sub(0, v[0] - 1) + prefix[i] + sub(v[0] + 1, v[1] - 1) +
                   prefix[j] + sub(v[1] + 1, len - 1);
        if (ss.find(s) != ss.end()) continue;
        ss.insert(s);
        res += f[s];
      }
    }
  } else if (v.size() == 1) {
    for (int i = 0; i < 6; i++) {
      string s = sub(0, v[0] - 1) + prefix[i] + sub(v[0] + 1, len - 1);
      if (ss.find(s) != ss.end()) continue;
      ss.insert(s);
      res += f[s];
    }
  } else if (v.size() == 0) {
    string s = inp;
    res += f[s];
  } else
    inp[len + 123213] = '1';
  return res;
}
int main() {
  prefix.push_back("a");
  prefix.push_back("b");
  prefix.push_back("c");
  prefix.push_back("d");
  prefix.push_back("e");
  prefix.push_back("");
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s", &inp);
    f[inp]++;
  }
  for (int i = 0; i < m; i++) {
    scanf("%s", &inp);
    int len = strlen(inp);
    int res = 0;
    vector<int> v;
    for (int j = 0; j < len; j++) {
      if (inp[j] == '?') v.push_back(j);
    }
    res = solve(v);
    printf("%d\n", res);
  }
  return 0;
}
