#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC target("avx,avx2")
using namespace std;
template <class t>
inline void read(t &s) {
  s = 0;
  register int f = 1;
  register char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
  s *= f;
  return;
}
template <class t, class... A>
inline void read(t &x, A &...a) {
  read(x);
  read(a...);
  return;
}
template <class t>
inline void write(t x) {
  if (x < 0) putchar('-'), x = -x;
  int buf[21], top = 0;
  while (x) buf[++top] = x % 10, x /= 10;
  if (!top) buf[++top] = 0;
  while (top) putchar(buf[top--] ^ '0');
  return;
}
inline void setIn(string s) {
  freopen(s.c_str(), "r", stdin);
  return;
}
inline void setOut(string s) {
  freopen(s.c_str(), "w", stdout);
  return;
}
inline void setIO(string s = "") {
  setIn(s + ".in");
  setOut(s + ".out");
  return;
}
template <class t>
inline bool checkmin(t &x, t y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class t>
inline bool checkmax(t &x, t y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
inline int lowbit(int x) { return x & (-x); }
int ch[2300000][26], cnt[2300000], ndn;
char s[55], t[55];
inline void insert(char *s) {
  register int p = 0;
  for (; *s; ++s) {
    register int d = *s - 'a';
    if (!ch[p][d]) ch[p][d] = ++ndn;
    p = ch[p][d];
  }
  ++cnt[p];
  return;
}
inline int query(char *s) {
  register int p = 0;
  for (; *s; ++s) {
    register int d = *s - 'a';
    if (!ch[p][d]) return 0;
    p = ch[p][d];
  }
  return cnt[p];
}
int len, cur;
map<string, int> vis;
inline void dfs(int dep) {
  if (dep >= len) {
    register int tot = 0;
    string str = "";
    for (int i = 0; i < len; ++i)
      if (s[i] != 'f') t[tot++] = s[i], str += s[i];
    t[tot] = '\0';
    if (vis.count(str)) return;
    vis[str] = 1;
    cur += query(t);
    return;
  }
  if (s[dep] != '?')
    dfs(dep + 1);
  else {
    for (char i = 'a'; i <= 'f'; ++i) s[dep] = i, dfs(dep + 1);
    s[dep] = '?';
  }
  return;
}
signed main(void) {
  int n, m;
  read(n, m);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s);
    insert(s);
  }
  for (int i = 1; i <= m; ++i) {
    vis.clear();
    scanf("%s", s);
    len = strlen(s);
    cur = 0;
    dfs(0);
    write(cur), puts("");
  }
  return 0;
}
