#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
int n, m, ans, pos;
string f[4];
set<string> st;
void bf(int cnt, string s) {
  s += f[cnt];
  if (cnt == pos) {
    st.insert(s);
    return;
  }
  bf(cnt + 1, s);
  for (int i = 0; i < 5; ++i) bf(cnt + 1, s + char('a' + i));
  return;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    ++mp[s];
  }
  for (int i = 0; i < m; ++i) {
    string s;
    ans = 0, pos = 0;
    st.clear();
    cin >> s;
    for (int j = 0; j < 4; ++j) f[j].clear();
    int l = s.size();
    for (int j = 0; j < l; ++j)
      if (s[j] == '?')
        ++pos;
      else
        f[pos] += s[j];
    bf(0, "");
    for (auto k : st) ans += mp[k];
    cout << ans << '\n';
  }
  return 0;
}
