#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

int main(){
    int n, m; cin>>n>>m;
    vector<string> w(n), rw(n);
    for(int i=0; i<n; i++){
        cin>>w[i];
        rw[i] = w[i];
        reverse(rw[i].begin(), rw[i].end());
    }

    sort(w.begin(), w.end());
    sort(rw.begin(), rw.end());

    for(int i=0; i<m; i++){
        string s; cin>>s;
        int si = s.size();
        bool exist = false;
        int id;
        for(int j=0; j<si; j++){
            if(s[j] == '?'){
                exist = true;
                id = j;
                break;
            }
        }

        int ans = 0;
        if(exist){
            int sum = 0;
            for(char ch='a'; ch<='z'; ch++){
                s[id] = ch;

                if(s[0] == '*'){
                    string rs = s;
                    reverse(rs.begin(), rs.end());
                    string trs = rs;
                    trs[si-2]++;
                    rs.pop_back();
                    trs.pop_back();

                    sum += lower_bound(rw.begin(), rw.end(), trs) - lower_bound(rw.begin(), rw.end(), rs);
                }
                else if(s[si-1] == '*'){
                    string t = s;
                    t[si-2]++;
                    s.pop_back();
                    t.pop_back();
                    sum += lower_bound(w.begin(), w.end(), t) - lower_bound(w.begin(), w.end(), s);
                    s += "*";
                }
                else{
                    sum += upper_bound(w.begin(), w.end(), s) - lower_bound(w.begin(), w.end(), s);
                }
            }

            ans = sum;
        }
        else{
            if(s[0] == '*'){
                string rs = s;
                reverse(rs.begin(), rs.end());
                string trs = rs;
                trs[si-2]++;
                rs.pop_back();
                trs.pop_back();

                ans = lower_bound(rw.begin(), rw.end(), trs) - lower_bound(rw.begin(), rw.end(), rs);
            }
            else if(s[si-1] == '*'){
                string t = s;
                t[si-2]++;
                s.pop_back();
                t.pop_back();
                ans = lower_bound(w.begin(), w.end(), t) - lower_bound(w.begin(), w.end(), s);
            }
            else{
                ans = upper_bound(w.begin(), w.end(), s) - lower_bound(w.begin(), w.end(), s);
            }
        }

        cout << ans << endl;
    }
}


Measured memory_bytes: 11792