#include <bits/stdc++.h>
using namespace std;
const int L = 53;
const long long h[10] = {0, 10007, 20123, 864613, 1000000007, 64678646161};
long long now;
char st[L];
int f[10];
int n, m, len;
long long a[L];
int cnt, ans;
unordered_map<long long, int> mp;
unordered_map<long long, int> mp1;
void calc() {
  long long now = 0, H = 0;
  for (int i(1); i <= (len); ++i)
    if (a[i]) {
      ++now;
      (H += now * h[a[i]]);
    }
  if (!mp1[H]) {
    ans += mp[H];
    mp1[H] = 1;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i(1); i <= (n); ++i) {
    scanf("%s", st + 1);
    len = strlen(st + 1);
    now = 0;
    for (int j(1); j <= (len); ++j) {
      long long op = (long long)st[j] - 96LL;
      (now += h[op] * (long long)j);
    }
    ++mp[now];
  }
  for (int i(1); i <= (m); ++i) {
    mp1.clear();
    scanf("%s", st + 1);
    len = strlen(st + 1);
    cnt = 0;
    for (int j(1); j <= (len); ++j) {
      if (st[j] != '?')
        a[j] = (long long)st[j] - 96LL;
      else {
        a[j] = 0;
        f[++cnt] = j;
      }
    }
    ans = 0;
    if (cnt == 0) {
      calc();
    } else if (cnt == 1) {
      for (int j(0); j <= (5); ++j) {
        a[f[1]] = j;
        calc();
      }
    } else if (cnt == 2) {
      for (int j(0); j <= (5); ++j) {
        for (int k(0); k <= (5); ++k) {
          a[f[1]] = j;
          a[f[2]] = k;
          calc();
        }
      }
    } else if (cnt == 3) {
      for (int j(0); j <= (5); ++j) {
        for (int k(0); k <= (5); ++k) {
          for (int l(0); l <= (5); ++l) {
            a[f[1]] = j;
            a[f[2]] = k;
            a[f[3]] = l;
            calc();
          }
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
