#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
const int inf = (int)1e9 + 1;
const long long big = (long long)1e18 + 1;
const int P = 239;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  int n, m;
  cin >> n >> m;
  map<string, int> mapa;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    mapa[s]++;
  }
  for (int i = 0; i < m; i++) {
    string s;
    cin >> s;
    vector<int> pos;
    for (int j = 0; j < (int)s.size(); j++)
      if (s[j] == '?') pos.push_back(j);
    int cnt = 1;
    for (int i = 0; i < (int)pos.size(); i++) cnt *= 6;
    set<string> tmp;
    for (int xxx = 0; xxx < cnt; xxx++) {
      int mask = xxx;
      for (int j = 0; j < (int)pos.size(); j++) {
        if (mask % 6 < 5)
          s[pos[j]] = ('a' + mask % 6);
        else
          s[pos[j]] = '#';
        mask /= 6;
      }
      string t;
      for (int j = 0; j < (int)s.size(); j++)
        if (s[j] != '#') t += s[j];
      tmp.insert(t);
    }
    int ans = 0;
    for (string t : tmp) ans += mapa[t];
    cout << ans << "\n";
  }
  return 0;
}
