#include <bits/stdc++.h>
using namespace std;
int ans = 0;
string searchS;
map<string, int> mp;
set<string> st;
void getans(string s, int idx) {
  if (idx == searchS.size()) {
    if (!st.count(s)) {
      ans += mp[s];
      st.insert(s);
    }
    return;
  }
  if (searchS[idx] == '?') {
    for (int i = 0; i < 5; i++) getans(s + char(i + 'a'), idx + 1);
    getans(s, idx + 1);
  } else
    getans(s + searchS[idx], idx + 1);
}
void lets_hope_its_right() {
  int n, m;
  cin >> n >> m;
  while (n--) {
    string s;
    cin >> s;
    mp[s]++;
  }
  while (m--) {
    string s;
    cin >> s;
    ans = 0;
    searchS = s;
    st.clear();
    getans("", 0);
    cout << ans << '\n';
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tt = 1;
  while (tt--) lets_hope_its_right();
}
