#include <bits/stdc++.h>
using namespace std;
const int mx1 = 20;
const int mx2 = 105;
const int mx3 = 1005;
const int mx4 = 30005;
const int mx5 = 200005;
const int mx6 = 1000005;
int _toggle(int N, int pos) { return N = N ^ (1 << pos); }
int _set(int N, int pos) { return N = N | (1 << pos); }
int _reset(int N, int pos) { return N = N & ~(1 << pos); }
bool _check(int N, int pos) { return (bool)(N & (1 << pos)); }
bool _upper(char a) { return a >= 'A' && a <= 'Z'; }
bool _lower(char a) { return a >= 'a' && a <= 'z'; }
bool _digit(char a) { return a >= '0' && a <= '9'; }
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};
int l, n, m;
map<string, int> mp;
map<string, bool> has;
string s;
int call(int id, string t) {
  if (id == l) {
    string q;
    for (int i = 0; i < id; i++) {
      if (t[i] < 'f') {
        q.push_back(t[i]);
      }
    }
    if (has[q]) {
      return 0;
    }
    has[q] = 1;
    return mp[q];
  }
  int ans = 0;
  if (t[id] != '?') {
    ans += call(id + 1, t);
    return ans;
  }
  for (char i = 'a'; i < 'g'; i++) {
    t[id] = i;
    ans += call(id + 1, t);
  }
  return ans;
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s;
    mp[s]++;
  }
  for (int i = 0; i < m; i++) {
    cin >> s;
    l = s.size();
    has.clear();
    cout << call(0, s) << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int tc = 1;
  while (tc--) {
    solve();
  }
}
