#include <bits/stdc++.h>
using namespace std;
const long long MD1 = 1000 * 1000 * 1000 + 7;
const long long MD2 = 1000 * 1000 * 1000 + 31;
const long long P = 31;
int counter;
map<pair<int, int>, int> cnt;
set<pair<int, int>> h;
pair<int, int> getHash(const string &s) {
  pair<long long, long long> ans = {0, 0};
  long long mult1 = 1;
  long long mult2 = 1;
  for (int i = 0; i < s.length(); i++) {
    ans.first += (s[i] - 'a' + 1) * mult1;
    ans.second += (s[i] - 'a' + 1) * mult2;
    ans.first %= MD1;
    ans.second %= MD2;
    mult1 *= P;
    mult1 %= MD1;
    mult2 *= P;
    mult2 %= MD2;
  }
  return ans;
}
void go(string s) {
  int pos = -1;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] != '?') continue;
    pos = i;
    break;
  }
  if (pos == -1) {
    pair<int, int> hv = getHash(s);
    if (h.find(hv) == h.end()) {
      counter += cnt[hv];
      h.insert(hv);
    }
  } else {
    string t = "";
    for (int i = 0; i < s.length(); i++) {
      if (i == pos) continue;
      t += s[i];
    }
    go(t);
    for (char ch = 'a'; ch <= 'e'; ch++) {
      s[pos] = ch;
      go(s);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    cnt[getHash(s)]++;
  }
  for (int i = 0; i < m; i++) {
    string s;
    cin >> s;
    counter = 0;
    h.clear();
    go(s);
    cout << counter << "\n";
  }
  return 0;
}
