#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int oo = 2e9;
struct node {
  int cnt, nxt[5];
  node() {
    cnt = 0;
    memset(nxt, -1, sizeof nxt);
  }
};
node trie[50 * N];
int cur = 1;
void insert(string &s) {
  int id = 1;
  for (auto c : s) {
    if (trie[id].nxt[c - 'a'] == -1) {
      trie[id].nxt[c - 'a'] = ++cur;
    }
    id = trie[id].nxt[c - 'a'];
  }
  trie[id].cnt++;
}
set<int> taken;
int get(string &s, int idx, int id) {
  if (idx == s.size()) {
    if (!taken.count(id)) {
      taken.emplace(id);
      return trie[id].cnt;
    }
    return 0;
  }
  int ret = 0;
  if (s[idx] != '?') {
    if (trie[id].nxt[s[idx] - 'a'] != -1)
      ret += get(s, idx + 1, trie[id].nxt[s[idx] - 'a']);
  } else {
    ret += get(s, idx + 1, id);
    for (int i = 0; i < 5; i++) {
      if (trie[id].nxt[i] != -1) ret += get(s, idx + 1, trie[id].nxt[i]);
    }
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    insert(s);
  }
  for (int i = 0; i < q; i++) {
    string s;
    cin >> s;
    cout << get(s, 0, 1) << '\n';
    taken.clear();
  }
}
