#include <bits/stdc++.h>
using namespace std;
map<string, int> words;
map<string, bool> found;
int ans;
void If_Ans(string s) {
  if (words[s] && !found[s]) {
    ans += words[s];
    found[s] = true;
  }
  return;
}
void make_string(string s, string base, int i, int max_i) {
  if (i >= max_i) {
    If_Ans(s);
    return;
  }
  if (base[i] != '?') {
    make_string(s + base[i], base, i + 1, max_i);
  } else {
    make_string(s + 'a', base, i + 1, max_i);
    make_string(s + 'b', base, i + 1, max_i);
    make_string(s + 'c', base, i + 1, max_i);
    make_string(s + 'd', base, i + 1, max_i);
    make_string(s + 'e', base, i + 1, max_i);
    make_string(s, base, i + 1, max_i);
  }
}
int main() {
  int jin, jenny;
  cin >> jin >> jenny;
  for (int i = 0; i < jin; i++) {
    string s;
    cin >> s;
    words[s]++;
  }
  for (int i = 0; i < jenny; i++) {
    found.clear();
    ans = 0;
    string s, c;
    cin >> s;
    make_string(c, s, 0, s.size());
    cout << ans << endl;
  }
}
