#include "bits/stdc++.h"
using namespace std;
#define all(x) x.begin(), x.end()

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int N, M; cin >> N >> M;
    vector<string> w(N), s(M), S, RS;
    for (auto &wi: w) cin >> wi, S.push_back(wi), RS.push_back(string(wi.rbegin(), wi.rend()));
    sort(all(S)), sort(all(RS));
    auto f1 = [&](string s) {
        s.pop_back();
        return int(lower_bound(all(S), s + '~') - lower_bound(all(S), s));
    };
    auto f2 = [&](string s) {
        s.pop_back();
        return int(lower_bound(all(RS), s + '~') - lower_bound(all(RS), s));
    };
    auto f3 = [&](string s) {
        return (binary_search(all(S), s) ? 1 : 0);
    };
    auto slv = [&](string s) {
        if (s.back() == '*') {
            return f1(s);
        } else if (s[0] == '*') {
            return f2(string(s.rbegin(), s.rend()));
        } else {
            return f3(s);
        }
    };
    for (auto &si: s) {
        cin >> si;
        int pos;
        if ((pos = si.find('?')) == string::npos) {
            cout << slv(si) << "\n";
        } else {
            int res = 0;
            for (int i = 0; i < 26; ++i) {
                si[pos] = char('a' + i);
                res += slv(si);
            }
            cout << res << "\n";
        }
    }
}

