#include <bits/stdc++.h>
using namespace std;
int const N = 100000;
int n, m;
string s[N];
map<string, int> fr;
set<char> pos[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
    ++fr[s[i]];
    for (int j = 0; j < s[i].size(); ++j) {
      pos[j].insert(s[i][j]);
    }
  }
  while (m--) {
    string pat;
    cin >> pat;
    int an = 0;
    vector<int> posi;
    for (int i = 0; i < pat.size(); ++i) {
      if (pat[i] == '?') posi.push_back(i);
    }
    if (!posi.size()) {
      printf("%d\n", fr[pat]);
      continue;
    }
    vector<string> all;
    if (posi.size()) {
      for (auto &it : pos[posi.back()]) {
        pat[posi.back()] = it;
        all.push_back(pat);
      }
      string patt = "";
      for (int j = 0; j < posi.back(); ++j) patt += pat[j];
      for (int j = posi.back() + 1; j < pat.size(); ++j) patt += pat[j];
      all.push_back(patt);
      posi.pop_back();
    }
    vector<string> all0;
    if (posi.size()) {
      for (auto &it : pos[posi.back()]) {
        for (auto j : all) {
          j[posi.back()] = it;
          all0.push_back(j);
          string patt = "";
          for (int i = 0; i < posi.back(); ++i) patt += j[i];
          for (int i = posi.back() + 1; i < j.size(); ++i) patt += j[i];
          all0.push_back(patt);
        }
      }
      posi.pop_back();
    }
    vector<string> all1;
    if (posi.size()) {
      for (auto &it : pos[posi.back()]) {
        for (auto j : all0) {
          j[posi.back()] = it;
          all1.push_back(j);
          string patt = "";
          for (int i = 0; i < posi.back(); ++i) patt += j[i];
          for (int i = posi.back() + 1; i < j.size(); ++i) patt += j[i];
          all1.push_back(patt);
        }
      }
      posi.pop_back();
    }
    if (all1.size()) {
      sort(all1.begin(), all1.end());
      all1.resize(unique(all1.begin(), all1.end()) - all1.begin());
      for (auto j : all1) an += fr[j];
    }
    if (all0.size() && !all1.size()) {
      sort(all0.begin(), all0.end());
      all0.resize(unique(all0.begin(), all0.end()) - all0.begin());
      for (auto j : all0) an += fr[j];
    }
    if (all.size() && !all1.size() && !all0.size()) {
      sort(all.begin(), all.end());
      all.resize(unique(all.begin(), all.end()) - all.begin());
      for (auto j : all) an += fr[j];
    }
    printf("%d\n", an);
  }
}
