#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

vector<string> s;
vector<string> t;

int calc(string now) {
    int len = now.size();

    if (now[0] == '*') {
        reverse(now.begin(), now.end());
        now.pop_back();
        auto down = lower_bound(t.begin(), t.end(), now);
        now.push_back('{');
        auto up = upper_bound(t.begin(), t.end(), now);
        return up - down;
    } else if (now[len - 1] == '*') {
        now.pop_back();
        auto down = lower_bound(s.begin(), s.end(), now);
        now.push_back('{');
        auto up = upper_bound(s.begin(), s.end(), now);
        return up - down;
    } else {
        return upper_bound(s.begin(), s.end(), now) - lower_bound(s.begin(), s.end(), now);
    }
}

int main() {
    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
        string now;
        cin >> now;
        s.push_back(now);
        reverse(now.begin(), now.end());
        t.push_back(now);
    }
    sort(s.begin(), s.end());
    sort(t.begin(), t.end());

    for (int i = 0; i < m; i++) {
        string now;
        cin >> now;

        int que = -1;
        for (int j = 0; j < now.size(); j++)
            if (now[j] == '?')que = j;

        if (que >= 0) {
            int ret = 0;
            for (int i = 'a'; i <= 'z'; i++) {
                now[que] = i;
                ret += calc(now);
            }
            cout << ret << endl;
        } else {
            cout << calc(now) << endl;
        }
    }
    return 0;
}
