#include <bits/stdc++.h>
using namespace std;
struct Node {
  vector<Node*> edges;
  bool term;
  vector<int> last;
  Node() {
    edges.resize(5);
    fill(edges.begin(), edges.end(), nullptr);
    term = false;
  }
};
void add_string(Node* root, string s) {
  for (char c : s) {
    c -= 'a';
    if (!root->edges[c]) {
      Node* node = new Node();
      root->edges[c] = node;
    }
    root = root->edges[c];
  }
  root->term = true;
  root->last.push_back(0);
}
void get_ans(Node* node, string s, int cnt, int& ans, int num) {
  if (cnt >= s.size()) {
    if (node->term) {
      for (int i = 0; i < node->last.size(); i++) {
        if (node->last[i] < num) {
          node->last[i] = num;
          ans++;
        }
      }
    }
    return;
  }
  char c = s[cnt];
  c -= 'a';
  if (s[cnt] != '?') {
    if (node->edges[c]) {
      get_ans(node->edges[c], s, cnt + 1, ans, num);
    }
  } else {
    get_ans(node, s, cnt + 1, ans, num);
    for (int i = 0; i < 5; i++) {
      if (node->edges[i]) {
        get_ans(node->edges[i], s, cnt + 1, ans, num);
      }
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  Node* root = new Node();
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    add_string(root, s);
  }
  for (int i = 0; i < m; i++) {
    string s;
    cin >> s;
    int ans = 0;
    get_ans(root, s, 0, ans, i + 1);
    cout << ans << "\n";
  }
  return 0;
}
