#include <bits/stdc++.h>
struct Vertex {
  std::vector<long long int> next;
  std::set<long long int> leaf;
  Vertex();
};
Vertex::Vertex() : next(5, -1), leaf() {}
void AddString(std::vector<Vertex>& data, const std::string& word,
               long long int val1, long long int val2, long long int result);
void FindString(std::vector<Vertex>& data, const std::string& word,
                std::vector<long long int>& counter);
void AddString(std::vector<Vertex>& data, const std::string& word,
               long long int val1, long long int val2, long long int result) {
  long long int current = val1;
  for (long long int i = val2; i < word.size(); i++) {
    if (word[i] != '?') {
      long long int index = word[i] - 'a';
      if (data[current].next[index] == -1) {
        data.push_back(Vertex());
        data[current].next[index] = data.size() - 1;
      }
      current = data[current].next[index];
    } else {
      for (long long int j = 0; j < 5; j++) {
        if (data[current].next[j] == -1) {
          data.push_back(Vertex());
          data[current].next[j] = data.size() - 1;
        }
        AddString(data, word, data[current].next[j], i + 1, result);
      }
    }
  }
  data[current].leaf.insert(result);
}
void FindString(std::vector<Vertex>& data, const std::string& word,
                std::vector<long long int>& counter) {
  long long int current = 0;
  for (long long int i = 0; i < word.size(); i++) {
    long long int index = word[i] - 'a';
    if (data[current].next[index] == -1) return;
    current = data[current].next[index];
  }
  for (auto it = data[current].leaf.begin(); it != data[current].leaf.end();
       it++)
    counter[(*it)]++;
}
int main() {
  std::vector<Vertex> data;
  data.push_back(Vertex());
  long long int n = 0, m = 0;
  std::cin >> n >> m;
  std::vector<std::string> check(n);
  for (long long int i = 0; i < check.size(); i++) std::cin >> check[i];
  std::vector<long long int> counter(m);
  for (long long int i = 0; i < counter.size(); i++) {
    std::string current;
    std::cin >> current;
    AddString(data, current, 0, 0, i);
  }
  for (long long int i = 0; i < check.size(); i++)
    FindString(data, check[i], counter);
  for (long long int i = 0; i < counter.size(); i++)
    std::cout << counter[i] << '\n';
  return 0;
}
