#include <bits/stdc++.h>
using namespace std;
int n, m;
char s[110];
int l;
const int maxn = 3100000;
int ch[maxn][5], sz[maxn];
int pos = 1, root = 1;
void insert(char s[], int l) {
  int now = root;
  for (int i = 0; i < l; i++)
    if (ch[now][s[i] - 'a'])
      now = ch[now][s[i] - 'a'];
    else
      now = ch[now][s[i] - 'a'] = ++pos;
  sz[now]++;
}
char t[110000];
int nn, np;
int nu[4] = {1, 6, 36, 216};
int tmp[11000], cn;
int ask(char s[], int l) {
  int now = root;
  for (int i = 0; i < l; i++) now = ch[now][s[i] - 'a'];
  return now;
}
int pp[maxn];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    l = strlen(s);
    insert(s, l);
  }
  for (int i = 1; i <= m; i++) {
    scanf("%s", s);
    l = strlen(s);
    np = cn = 0;
    for (int j = 0; j < l; j++)
      if (s[j] == '?') np++;
    for (int j = 0; j < nu[np]; j++) {
      int now = j;
      nn = 0;
      for (int k = 0; k < l; k++)
        if (s[k] == '?') {
          if (now % 6 == 5)
            ;
          else
            t[nn++] = 'a' + (now % 6);
          now /= 6;
        } else
          t[nn++] = s[k];
      t[nn] = 0;
      tmp[++cn] = ask(t, nn);
    }
    int ans = 0;
    for (int i = 1; i <= cn; i++)
      if (!pp[tmp[i]]) ans += sz[tmp[i]], pp[tmp[i]] = 1;
    for (int i = 1; i <= cn; i++) pp[tmp[i]] = 0;
    printf("%d\n", ans);
  }
  return 0;
}
