#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return ((a * b) / gcd(a, b)); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long n, m;
  cin >> n >> m;
  map<string, long long> mapp;
  for (long long i = 0; i < n; i++) {
    string s;
    cin >> s;
    mapp[s]++;
  }
  for (long long i = 0; i < m; i++) {
    set<string> combinations;
    vector<long long> pos;
    long long noOfBlanks = 0;
    string s;
    cin >> s;
    for (long long j = 0; j < s.length(); j++) {
      if (s[j] == '?') {
        ++noOfBlanks;
        pos.push_back(j);
      }
    }
    if (noOfBlanks == 0) {
      combinations.insert(s);
    } else if (noOfBlanks == 1) {
      for (long long k = 0; k < 6; k++) {
        string extra;
        string now = "";
        if (k == 0) {
          extra = 'a';
        } else if (k == 1) {
          extra = 'b';
        } else if (k == 2) {
          extra = 'c';
        } else if (k == 3) {
          extra = 'd';
        } else if (k == 4) {
          extra = 'e';
        } else {
          extra = "";
        }
        now = s.substr(0, pos[0]);
        now += extra;
        now += s.substr(pos[0] + 1, n - pos[0] - 1);
        combinations.insert(now);
      }
    } else if (noOfBlanks == 2) {
      for (long long k = 0; k < 6; k++) {
        string extra;
        string now = "";
        if (k == 0) {
          extra = 'a';
        } else if (k == 1) {
          extra = 'b';
        } else if (k == 2) {
          extra = 'c';
        } else if (k == 3) {
          extra = 'd';
        } else if (k == 4) {
          extra = 'e';
        } else {
          extra = "";
        }
        now = s.substr(0, pos[0]);
        now += extra;
        now += s.substr(pos[0] + 1, pos[1] - pos[0] - 1);
        for (long long l = 0; l < 6; l++) {
          string extra1 = "";
          if (l == 0) {
            extra1 = 'a';
          } else if (l == 1) {
            extra1 = 'b';
          } else if (l == 2) {
            extra1 = 'c';
          } else if (l == 3) {
            extra1 = 'd';
          } else if (l == 4) {
            extra1 = 'e';
          } else {
            extra1 = "";
          }
          now += extra1;
          now += s.substr(pos[1] + 1, n - pos[1] - 1);
          combinations.insert(now);
          if (l < 5) {
            now = s.substr(0, pos[0]);
            now += extra;
            now += s.substr(pos[0] + 1, pos[1] - pos[0] - 1);
          }
        }
      }
    } else {
      for (long long k = 0; k < 6; k++) {
        string extra;
        string now = "";
        if (k == 0) {
          extra = 'a';
        } else if (k == 1) {
          extra = 'b';
        } else if (k == 2) {
          extra = 'c';
        } else if (k == 3) {
          extra = 'd';
        } else if (k == 4) {
          extra = 'e';
        } else {
          extra = "";
        }
        now = s.substr(0, pos[0]);
        now += extra;
        now += s.substr(pos[0] + 1, pos[1] - pos[0] - 1);
        for (long long l = 0; l < 6; l++) {
          string extra1 = "";
          if (l == 0) {
            extra1 = 'a';
          } else if (l == 1) {
            extra1 = 'b';
          } else if (l == 2) {
            extra1 = 'c';
          } else if (l == 3) {
            extra1 = 'd';
          } else if (l == 4) {
            extra1 = 'e';
          } else {
            extra1 = "";
          }
          now += extra1;
          now += s.substr(pos[1] + 1, pos[2] - pos[1] - 1);
          for (long long o = 0; o < 6; o++) {
            string extra2 = "";
            if (o == 0) {
              extra2 = 'a';
            } else if (o == 1) {
              extra2 = 'b';
            } else if (o == 2) {
              extra2 = 'c';
            } else if (o == 3) {
              extra2 = 'd';
            } else if (o == 4) {
              extra2 = 'e';
            } else {
              extra2 = "";
            }
            now += extra2;
            now += s.substr(pos[2] + 1, n - pos[2] - 1);
            combinations.insert(now);
            if (o < 5) {
              now = s.substr(0, pos[0]);
              now += extra;
              now += s.substr(pos[0] + 1, pos[1] - pos[0] - 1);
              now += extra1;
              now += s.substr(pos[1] + 1, pos[2] - pos[1] - 1);
            }
          }
          combinations.insert(now);
          now = s.substr(0, pos[0]);
          now += extra;
          now += s.substr(pos[0] + 1, pos[1] - pos[0] - 1);
        }
      }
    }
    long long counter = 0;
    set<string>::iterator it;
    for (it = combinations.begin(); it != combinations.end(); it++) {
      if (mapp[*it] >= 1) {
        counter += (mapp[*it]);
      }
    }
    cout << counter << '\n';
  }
  return 0;
}
