#include <bits/stdc++.h>

using namespace std;

int N, M;
vector<string> Dictionary, RDictionary;
map<string, int> Count;

int count(const vector<string> &dic, string word) {
	int left, right;

	left = lower_bound(dic.begin(), dic.end(), word) - dic.begin();
	word.push_back('z' + 1);
	right = lower_bound(dic.begin(), dic.end(), word) - dic.begin();

	//cout << "left=" << left << "  right=" << right << "  word=" << word << endl;
	return right - left;
}

int solve(const string& word) {
	int len;
	string tmp;

	len = word.size();
	if (word[0] == '*') {
		tmp = word.substr(1);
		reverse(tmp.begin(), tmp.end());
		return count(RDictionary, tmp);
	}
	
	if (word[len - 1] == '*') {
		tmp = word.substr(0, len - 1);
		return count(Dictionary, tmp);
	}

	return Count[word];
}

int main() {
	int pos, sum;
	string word;

	cin >> N >> M;
	for (int i = 0; i < N; ++i) {
		cin >> word;
		Dictionary.push_back(word);
		Count[word] = 1;
		//cout << "1:" << word << endl;
		reverse(word.begin(), word.end());
		RDictionary.push_back(word);
		//cout << "2:" << word << endl;
	}

	sort(Dictionary.begin(), Dictionary.end());
	sort(RDictionary.begin(), RDictionary.end());
	for (int i = 0; i < M; ++i) {
		cin >> word;
		pos = word.find('?');
		if (pos == string::npos) {
			cout << solve(word) << endl;
			continue;
		}
		sum = 0;
		for (char ch = 'a'; ch <= 'z'; ++ch) {
			word[pos] = ch;
			//cout << "word=" << word << endl;
			sum += solve(word);
		}
		cout << sum << endl;
	}

	return 0;
}

