#include <bits/stdc++.h>
using namespace std;
string words[100000];
class TrieNode {
 public:
  bool exists;
  TrieNode* children[5];
  TrieNode() {
    exists = false;
    for (int i = 0; i < 5; i++) {
      children[i] = NULL;
    }
  }
};
class Trie {
 public:
  TrieNode* root;
  Trie() { root = new TrieNode; }
  void insert(string& word) {
    int level;
    int length = word.size();
    int index;
    TrieNode* pCrawl = root;
    for (level = 0; level < length; level++) {
      index = word[level] - 'a';
      if (!pCrawl->children[index]) {
        pCrawl->children[index] = new TrieNode();
      }
      pCrawl = pCrawl->children[index];
    }
    pCrawl->exists = true;
  }
  bool find(string& word) {
    int level;
    int length = word.size();
    int index;
    TrieNode* pCrawl = root;
    for (level = 0; level < length; level++) {
      index = word[level] - 'a';
      if (!pCrawl->children[index]) {
        return false;
      }
      pCrawl = pCrawl->children[index];
    }
    return (pCrawl != NULL) && pCrawl->exists;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> words[i];
  }
  string pattern;
  vector<string> matches;
  for (int i = 0; i < m; i++) {
    cin >> pattern;
    matches.clear();
    matches.push_back(pattern);
    for (int j = 0; j < 3; j++) {
      vector<string> tset;
      for (string pn : matches) {
        int off = pn.find('?');
        if (off != string::npos) {
          for (char ich = 'a'; ich <= 'e'; ich++) {
            pn[off] = ich;
            tset.push_back(pn);
          }
          pn.erase(pn.begin() + off);
          tset.push_back(pn);
        } else {
          tset.push_back(pn);
        }
      }
      matches = tset;
    }
    Trie trie;
    for (string pn : matches) {
      trie.insert(pn);
    }
    int count = 0;
    for (int j = 0; j < n; j++) {
      if (trie.find(words[j])) {
        count++;
      }
    }
    cout << count << '\n';
  }
  return 0;
}
