#include <bits/stdc++.h>
using namespace std;
set<string> dee;
map<string, int> mmp;
void dfs(string s) {
  int i = 0;
  while (s[i] != '?' && i < s.size()) i++;
  if (i == s.size()) {
    dee.insert(s);
    return;
  }
  for (char ch = 'a'; ch <= 'e'; ch++) {
    s[i] = ch;
    dfs(s);
  }
  dfs(s.substr(0, i) + s.substr(i + 1));
}
string s;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  mmp.clear();
  int n, m, h;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s;
    mmp[s]++;
  }
  for (int i = 0; i < m; i++) {
    h = 0;
    dee.clear();
    cin >> s;
    dfs(s);
    for (string ss : dee) {
      if (mmp.count(ss) > 0) h += mmp[ss];
    }
    printf("%d\n", h);
  }
  return 0;
}
