#include <bits/stdc++.h>
using namespace std;
char s[55];
int n, m;
multiset<string> Set1;
set<string> Set2;
int work(int now, string st) {
  while (s[now] && s[now] != '?') st += s[now++];
  if (!s[now]) {
    if (Set2.count(st)) return 0;
    Set2.insert(st);
    return Set1.count(st);
  }
  int res = work(now + 1, st);
  for (char i = 'a'; i <= 'e'; i++) res += work(now + 1, st + i);
  return res;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    Set1.insert(string(s));
  }
  for (int i = 0; i < m; i++) {
    scanf("%s", s);
    Set2.clear();
    int ans = work(0, "");
    printf("%d\n", ans);
  }
  return 0;
}
