#include <bits/stdc++.h>
using namespace std;
unordered_map<string, int> mp;
unordered_map<string, int> vis;
int n, m;
vector<int> whe;
string s;
int num;
void dfs(int x) {
  if (x == whe.size()) {
    if (vis.count(s)) return;
    if (mp.count(s)) num += mp[s];
    vis[s] = 1;
    return;
  }
  for (int i = 0; i < 5; i++) {
    s[whe[x]] = i + 'a';
    dfs(x + 1);
  }
  s.erase(whe[x] + s.begin());
  for (int i = 0; i < whe.size(); i++) whe[i]--;
  dfs(x + 1);
  for (int i = 0; i < whe.size(); i++) whe[i]++;
  s.insert(whe[x], "?");
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    mp[s]++;
  }
  for (int i = 1; i <= m; i++) {
    cin >> s;
    whe.clear();
    vis.clear();
    for (int j = 0; j < s.length(); j++) {
      if (s[j] == '?') whe.push_back(j);
    }
    num = 0;
    dfs(0);
    cout << num << endl;
  }
  return 0;
}
