#include <bits/stdc++.h>
class trie {
  struct trie_node {
    int num_occurrences;
    std::array<trie_node*, 5> children;
    trie_node() : num_occurrences(0) {
      for (int i = 0; i < 5; i++) {
        children[i] = nullptr;
      }
    }
  };
  trie_node* root;
  void insert_word(trie_node* node, std::string& word, size_t i) {
    if (i >= word.size()) {
      node->num_occurrences += 1;
    } else {
      trie_node*& child = node->children[word[i] - 'a'];
      if (child == nullptr) {
        child = new trie_node;
      }
      insert_word(child, word, i + 1);
    }
  }
  int count_matches(trie_node* node, std::string& pattern, size_t i_char,
                    std::unordered_set<trie_node*>& reached_nodes) {
    if (i_char == pattern.size()) {
      if (reached_nodes.find(node) == reached_nodes.end()) {
        reached_nodes.insert(node);
        return node->num_occurrences;
      } else {
        return 0;
      }
    }
    if (pattern[i_char] == '?') {
      int result = count_matches(node, pattern, i_char + 1, reached_nodes);
      for (trie_node* child : node->children) {
        if (child != nullptr) {
          result += count_matches(child, pattern, i_char + 1, reached_nodes);
        }
      }
      return result;
    } else {
      trie_node* child = node->children[pattern[i_char] - 'a'];
      if (child != nullptr) {
        return count_matches(child, pattern, i_char + 1, reached_nodes);
      } else {
        return 0;
      }
    }
  }

 public:
  trie() : root(new trie_node) {}
  void add_word(std::string& word) { insert_word(root, word, 0); }
  int count_matches(std::string& pattern) {
    std::unordered_set<trie_node*> reached_nodes;
    return count_matches(root, pattern, 0, reached_nodes);
  }
};
int main() {
  int num_words, num_patterns;
  std::cin >> num_words >> num_patterns;
  trie t;
  while (num_words--) {
    std::string word;
    std::cin >> word;
    t.add_word(word);
  }
  while (num_patterns--) {
    std::string patern;
    std::cin >> patern;
    std::cout << t.count_matches(patern) << '\n';
  }
}
