#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
template <class T>
inline T biton(T n, T pos) {
  return n | ((T)1 << pos);
}
template <class T>
inline T bitoff(T n, T pos) {
  return n & ~((T)1 << pos);
}
template <class T>
inline T ison(T n, T pos) {
  return (bool)(n & ((T)1 << pos));
}
template <class T>
inline T gcd(T a, T b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
template <typename T>
string NumberToString(T Number) {
  ostringstream second;
  second << Number;
  return second.str();
}
inline int nxt() {
  int aaa;
  scanf("%d", &aaa);
  return aaa;
}
inline long long int lxt() {
  long long int aaa;
  scanf("%lld", &aaa);
  return aaa;
}
inline double dxt() {
  double aaa;
  scanf("%lf", &aaa);
  return aaa;
}
template <class T>
inline T bigmod(T p, T e, T m) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % m;
    p = (p * p) % m;
  }
  return (T)ret;
}
string a, b, c, d;
string temp;
int f = 0;
map<string, long long int> mp;
long long int ans = 0;
string s;
vector<int> v;
int bk = 0;
map<string, int> already;
void go(int pos) {
  if (pos >= s.size()) {
    if (already.count(temp)) return;
    if (mp.count(temp)) already[temp] = 1;
    ans += mp[temp];
    return;
  }
  if (s[pos] == '?') {
    for (int i = 'a' - 1; i <= 'e'; i++) {
      string temp1 = temp;
      if (i >= 'a') temp += i;
      go(pos + 1);
      temp = temp1;
    }
  } else {
    string temp1 = temp;
    temp += s[pos];
    go(pos + 1);
    temp = temp1;
  }
}
int main() {
  int n = nxt();
  int m = nxt();
  for (int i = 0; i < n; i++) {
    cin >> s;
    mp[s]++;
  }
  for (int i = 0; i < m; i++) {
    cin >> s;
    go(0);
    printf("%lld\n", ans);
    already.clear();
    ans = 0;
  }
  return 0;
}
