#include <bits/stdc++.h>
using namespace std;
struct node {
  int sum;
  int adj[6];
  node() {
    sum = 0;
    memset(adj, -1, sizeof adj);
  }
};
int n, m;
vector<node> T;
map<string, int> S;
void add(string x) {
  int cur = 0;
  for (int i = 0; i < x.size(); i++) {
    int c = x[i] - 'a';
    if (T[cur].adj[c] == -1) {
      T.push_back(node());
      T[cur].adj[c] = T.size() - 1;
      cur = T.size() - 1;
    } else {
      cur = T[cur].adj[c];
    }
    if (i == x.size() - 1) T[cur].sum++;
  }
}
int ask(string x) {
  string y = "";
  for (int i = 0; i < x.size(); i++) {
    if (x[i] == '?') continue;
    y += x[i];
  }
  bool take = true;
  if (S.find(y) != S.end()) {
    take = false;
  } else {
    S[y] = 1;
  }
  int cur = 0;
  int ans = 0;
  int i = 0;
  for (i = 0; i < x.size(); i++) {
    if (x[i] == '?') {
      string y = x;
      for (char a = 'a'; a <= 'e'; a++) {
        y[i] = a;
        ans += ask(y);
      }
      if (i == x.size() - 1 && take) {
        ans += T[cur].sum;
      }
      continue;
    }
    int c = x[i] - 'a';
    if (T[cur].adj[c] != -1) {
      cur = T[cur].adj[c];
    } else {
      break;
    }
    if (i == x.size() - 1 && take) {
      ans += T[cur].sum;
    }
  }
  return ans;
}
int main() {
  cin >> n >> m;
  T.push_back(node());
  for (int i = 0; i < n; i++) {
    string x;
    cin >> x;
    add(x);
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    S.clear();
    ans = 0;
    string x;
    cin >> x;
    ans += ask(x);
    cout << ans << endl;
  }
  return 0;
}
