#include <bits/stdc++.h>
using namespace std;
set<int> ind;
struct node {
  node *arr[5];
  vector<int> ind;
  node() {
    for (int i = 0; i < 5; ++i) arr[i] = NULL;
  }
};
node *ins(node *curr, string &s, int pos, int i) {
  if (curr == NULL) curr = new node;
  if (pos == s.size()) {
    curr->ind.push_back(i);
    return curr;
  }
  curr->arr[s[pos] - 'a'] = ins(curr->arr[s[pos] - 'a'], s, pos + 1, i);
  return curr;
}
void nmatches(node *curr, string &s, int pos) {
  if (curr == NULL) return;
  if (pos == s.size()) {
    for (auto it : curr->ind) ind.insert(it);
    return;
  }
  if (s[pos] == '?') {
    nmatches(curr, s, pos + 1);
    for (int i = 0; i < 5; ++i) nmatches(curr->arr[i], s, pos + 1);
  } else
    nmatches(curr->arr[s[pos] - 'a'], s, pos + 1);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  string s;
  node *root = NULL;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    root = ins(root, s, 0, i);
  }
  while (m--) {
    ind.clear();
    cin >> s;
    nmatches(root, s, 0);
    cout << ind.size() << "\n";
  }
}
