#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 3000000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
int main() {
	iostream::sync_with_stdio(false);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	vector<string> normal, reversed;
	REP(i, n) {
		string s;
		cin >> s;
		string a = s;
		while (s.length() != 201) {
			s.push_back('z' + 1);
		}
		normal.push_back(s);
		reverse(a.begin(), a.end());
		while (a.length() != 201) {
			a.push_back('z' + 1);
		}
		reversed.push_back(a);
	}
	sort(normal.begin(), normal.end());
	sort(reversed.begin(), reversed.end());
	REP(i, m) {
		string pepe;
		cin >> pepe;
		int ans = 0;
		vector<string> boa;
		for (int i = 0; i < pepe.length(); ++i) {
			if (pepe[i] == '?') {
				for (int q = 'a'; q <= 'z'; ++q) {
					pepe[i] = q;
					boa.push_back(pepe);
				}
			}
		}
		if (boa.size() == 0) boa.push_back(pepe);
		for (int i = 0; i < boa.size(); ++i) {
			string s = boa[i];
			if (s[s.length() - 1] == '*') {
				int top = n - 1;
				int bot = 0;
				int failed = 0;
				for (int i = 0; i < s.length() - 1; ++i) {
					if (s[i] == '?')continue;
					int next_top = top;
					int next_bot = bot;
					while (next_top - next_bot > 1) {
						int mid = (next_bot + next_top) / 2;
						if (normal[mid][i] > s[i]) {
							next_top = mid;
						}
						else next_bot = mid;
					}
					if (normal[next_top][i] == s[i]) {
						top = next_top;
					}
					else if (normal[next_bot][i] == s[i]) {
						top = next_bot;
					}
					else {
						failed = 1;
						break;
					}
					next_top = top;
					next_bot = bot;
					while (next_top - next_bot > 1) {
						int mid = (next_bot + next_top) / 2;
						if (normal[mid][i] >= s[i]) {
							next_top = mid;
						}
						else next_bot = mid;
					}
					if (normal[next_bot][i] == s[i]) {
						bot = next_bot;
					}
					else if (normal[next_top][i] == s[i]) {
						bot = next_top;
					}
					else {
						failed = 1;
						break;
					}
				}
				if (failed == 1) {
					ans += 0;
				}
				else {
					ans += top - bot + 1;
				}
			}
			else if (s[0] == '*') {
				reverse(s.begin(), s.end());
				int top = n - 1;
				int bot = 0;
				int failed = 0;
				for (int i = 0; i < s.length() - 1; ++i) {
					if (s[i] == '?')continue;
					int next_top = top;
					int next_bot = bot;
					while (next_top - next_bot > 1) {
						int mid = (next_bot + next_top) / 2;
						if (reversed[mid][i] > s[i]) {
							next_top = mid;
						}
						else next_bot = mid;
					}
					if (reversed[next_top][i] == s[i]) {
						top = next_top;
					}
					else if (reversed[next_bot][i] == s[i]) {
						top = next_bot;
					}
					else {
						failed = 1;
						break;
					}
					next_top = top;
					next_bot = bot;
					while (next_top - next_bot > 1) {
						int mid = (next_bot + next_top) / 2;
						if (reversed[mid][i] >= s[i]) {
							next_top = mid;
						}
						else next_bot = mid;
					}
					if (reversed[next_bot][i] == s[i]) {
						bot = next_bot;
					}
					else if (reversed[next_top][i] == s[i]) {
						bot = next_top;
					}
					else {
						failed = 1;
						break;
					}
				}
				if (failed == 1) {
					ans += 0;
				}
				else {
					ans += top - bot + 1;
				}
			}
			else {
				while (s.length() != 201) {
					s.push_back('z' + 1);
				}
				s.push_back('*');
				int top = n - 1;
				int bot = 0;
				int failed = 0;
				for (int i = 0; i < s.length() - 1; ++i) {
					if (s[i] == '?')continue;
					int next_top = top;
					int next_bot = bot;
					while (next_top - next_bot > 1) {
						int mid = (next_bot + next_top) / 2;
						if (normal[mid][i] > s[i]) {
							next_top = mid;
						}
						else next_bot = mid;
					}
					if (normal[next_top][i] == s[i]) {
						top = next_top;
					}
					else if (normal[next_bot][i] == s[i]) {
						top = next_bot;
					}
					else {
						failed = 1;
						break;
					}
					next_top = top;
					next_bot = bot;
					while (next_top - next_bot > 1) {
						int mid = (next_bot + next_top) / 2;
						if (normal[mid][i] >= s[i]) {
							next_top = mid;
						}
						else next_bot = mid;
					}
					if (normal[next_bot][i] == s[i]) {
						bot = next_bot;
					}
					else if (normal[next_top][i] == s[i]) {
						bot = next_top;
					}
					else {
						failed = 1;
						break;
					}
				}
				if (failed == 1) {
					ans += 0;
				}
				else {
					ans += top - bot + 1;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}


Measured memory_bytes: 37080