# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
#include<fstream>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourd; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Mfor(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, t, s, p, q, last, cnt, sum, ans, dp[2000][2000];
string a[60000], b[60000];
string str, ss;
bool f[210000];
char c;
int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
struct Edge { LL to, cost; };
vector<Edge>vec[200000];
vector<LL>v;
map<string, LL>ma;
multiset<LL>st;

void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}


int main() {
	cin >> n >>  m;
	rep(i, n) {
		cin >> a[i];
		ma[a[i]]++;
		b[i] = a[i];
		std::reverse(b[i].begin(),b[i].end());
	}
	sort(a, a + n);
	sort(b, b + n);
	rep(i, m) {
		ans = 0;
		cin >> str;
		int F1 = 0, F2 = 0;		

		if (str[0] == '*') {
			F2 = 2;
			reverse(str.begin(), str.end());
			str.pop_back();
		}
		if (str[str.size() - 1] == '*') {
			F2 = 1;
			str.pop_back();
		}
	
		rep(j, str.size()) {
			if (str[j] == '?') {
				if (j + 1 == str.size()&&F2!=0) {
					str.pop_back();
				}
				else {
					F1 = 1, cnt = j;
				}
			}
		}


		if (F2 == 2) {
			if (F1) {
				rep(j, 26) {
					str[cnt] = 'a' + j;
					int l = lower_bound(b, b + n, str) - b;
					str[str.size() - 1]++;
					int r = lower_bound(b, b + n, str) - b;
					str[str.size() - 1]--;
					ans += r - l;
				}
			}
			else {
				int l = lower_bound(b, b + n, str) - b;
				str[str.size() - 1]++;
				int r = lower_bound(b, b + n, str) - b;
				str[str.size() - 1]--;
				ans = r - l;
			}

		}
		else if(F2==1) {

			if (F1) {
				rep(j, 26) {
					str[cnt] = 'a' + j;
					int l = lower_bound(a, a + n, str) - a;
					str[str.size() - 1]++;
					int r = lower_bound(a, a + n, str) - a;
					str[str.size() - 1]--;
					ans += r - l;
				}
			}
			else {
				int l = lower_bound(a, a + n, str) - a;
				str[str.size() - 1]++;
				int r = lower_bound(a, a + n, str) - a;
				str[str.size() - 1]--;
				ans = r - l;
			}

		}
		else {

			if (F1) {
				rep(j, 26) {
					str[cnt] = 'a' + j;
					ans += ma[str];
				}
			}
			else {
				ans = ma[str];
			}
		}

		cout << ans << endl;
	}
	return 0;
}



Measured memory_bytes: 87620