#include <bits/stdc++.h>
using namespace std;
struct node {
  int next[7];
  int end;
  node() {
    memset(next, 0, sizeof(next));
    end = 0;
  }
} f[3000000];
struct abc {
  int next[7];
  int end;
  abc() {
    memset(next, 0, sizeof(next));
    end = 0;
  }
} tf[3000000];
char a[55];
int root = 0, pop = 0, sum, top = 0;
void insert(char *x) {
  int last = root;
  for (int i = 0; i < strlen(x); i++) {
    if (!f[last].next[x[i] - 'a']) pop++, f[last].next[x[i] - 'a'] = pop;
    last = f[last].next[x[i] - 'a'];
  }
  f[last].end++;
}
int find(char *x) {
  int last = root;
  for (int i = 0; i < strlen(x); i++) {
    if (x[i] == 'f') continue;
    if (!f[last].next[x[i] - 'a']) return 0;
    last = f[last].next[x[i] - 'a'];
  }
  return f[last].end;
}
void tinsert(char *x, int tk) {
  int last = root;
  for (int i = 0; i < strlen(x); i++) {
    if (x[i] == 'f') continue;
    if (!tf[last].next[x[i] - 'a']) top++, tf[last].next[x[i] - 'a'] = top;
    last = tf[last].next[x[i] - 'a'];
  }
  tf[last].end = tk;
}
bool tfind(char *x, int p) {
  int last = root;
  for (int i = 0; i < strlen(x); i++) {
    if (x[i] == 'f') continue;
    if (!tf[last].next[x[i] - 'a']) return 0;
    last = tf[last].next[x[i] - 'a'];
  }
  return (tf[last].end == p);
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", a);
    insert(a);
  }
  for (int u = 1; u <= m; u++) {
    scanf("%s", a);
    int t[3] = {0, 0, 0}, pop = 0;
    for (int i = 0; i < strlen(a); i++)
      if (a[i] == '?') t[pop] = i, pop++;
    if (!pop) {
      sum = find(a);
    }
    if (pop == 1) {
      sum = 0;
      for (int i = 0; i <= 5; i++) {
        a[t[0]] = 'a' + i;
        if (tfind(a, u)) continue;
        sum += find(a);
        tinsert(a, u);
      }
    }
    if (pop == 2) {
      sum = 0;
      for (int i = 0; i <= 5; i++)
        for (int j = 0; j <= 5; j++) {
          a[t[0]] = 'a' + i;
          a[t[1]] = 'a' + j;
          if (tfind(a, u)) continue;
          sum += find(a);
          tinsert(a, u);
        }
    }
    if (pop == 3) {
      sum = 0;
      for (int i = 0; i <= 5; i++)
        for (int j = 0; j <= 5; j++)
          for (int k = 0; k <= 5; k++) {
            a[t[0]] = 'a' + i;
            a[t[1]] = 'a' + j;
            a[t[2]] = 'a' + k;
            if (tfind(a, u)) continue;
            sum += find(a);
            tinsert(a, u);
          }
    }
    printf("%d\n", sum);
  }
  return 0;
}
