#include <bits/stdc++.h>
using namespace std;
const int M = 5e6 + 10;
char str[100];
int num = 1, a[M][6], b[M], vis[M], n, m;
void add() {
  int root = 1;
  for (int i = 0; str[i]; ++i) {
    int xx = str[i] - 'a';
    if (!a[root][xx]) a[root][xx] = ++num;
    root = a[root][xx];
  }
  b[root]++;
}
int q(int root, int id) {
  if (!str[id]) {
    if (vis[root] != m + 1) {
      vis[root] = m + 1;
      return b[root];
    } else
      return 0;
  }
  int sum = 0;
  if (str[id] == '?') {
    sum += q(root, id + 1);
    for (int i = 0; i < 5; ++i) {
      int xx = a[root][i];
      if (xx) sum += q(xx, id + 1);
    }
  } else {
    int xx = a[root][str[id] - 'a'];
    if (xx) sum += q(xx, id + 1);
  }
  return sum;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%s", str);
    add();
  }
  while (m--) {
    scanf("%s", str);
    printf("%d\n", q(1, 0));
  }
  return 0;
}
