#include <bits/stdc++.h>
using namespace std;
struct node {
  int nxt[5], eow;
  bool f;
  node() {
    eow = 0;
    f = false;
    memset(nxt, -1, sizeof(nxt));
  }
};
int cnt = 2;
node v[5000001];
void add(string s) {
  int curr = 1;
  for (int i = 0; i < (int)s.size(); i++) {
    if (v[curr].nxt[s[i] - 'a'] == -1) v[curr].nxt[s[i] - 'a'] = cnt++;
    curr = v[curr].nxt[s[i] - 'a'];
  }
  v[curr].eow++;
}
map<int, bool> done;
int get(int pos, string s, int curr) {
  if (pos == s.size()) {
    if (v[curr].eow && !done[curr]) {
      done[curr] = !done[curr];
      return v[curr].eow;
    }
    return 0;
  }
  int ret = 0;
  int c = s[pos] - 'a';
  if (s[pos] == '?') {
    ret += get(pos + 1, s, curr);
    for (int i = 0; i < 5; i++) {
      if (v[curr].nxt[i] != -1) {
        ret += get(pos + 1, s, v[curr].nxt[i]);
      }
    }
  } else {
    if (v[curr].nxt[c] == -1)
      return 0;
    else
      ret += get(pos + 1, s, v[curr].nxt[c]);
  }
  return ret;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    char tmp[55];
    scanf("%s", tmp);
    add(tmp);
  }
  while (m--) {
    char tmp[55];
    scanf("%s", tmp);
    done.clear();
    printf("%d\n", get(0, tmp, 1));
  }
}
