#include <bits/stdc++.h>

using namespace std;
using i64 = int64_t;

template <i64 mod>
struct ModInt{
    i64 p;
    ModInt() : p(0){};
    ModInt(i64 x){p = x >= 0 ? x % mod : x + (-x + mod - 1) / mod * mod; assert(0 <= p && p < mod);};
    ModInt& operator+=(const ModInt& y){
        p = (p + y.p);
        if(p >= mod)
            p -= mod;
        assert(0 <= p && p < mod);
        return *this;
    }
    ModInt& operator-=(const ModInt& y){
        p = (p - y.p);
        if(p < 0)
            p += mod;
        assert(0 <= p && p < mod);
        return *this;
    }
    ModInt& operator*=(const ModInt& y){
        p = (p * y.p) % mod;
        assert(0 <= p && p < mod);
        return *this;
    }
    friend ModInt operator*(ModInt x, const ModInt& y){
        return x *= y;
    }
    friend ModInt operator+(ModInt x, const ModInt& y){
        return x += y;
    }
    friend ModInt operator-(ModInt x, const ModInt& y){
        return x -= y;
    }
    friend bool operator==(ModInt x, const ModInt& y){
        return x.p == y.p;
    }
};

template<typename T>
T mpow(T x, i64 y){
    T z = x;
    T val = y & 1 ? x : T(1);
    while(z *= z, y >>= 1)
        if(y & 1)
            val *= z;
    return val;
}

const i64 mod1 = 1e9 + 7;
const i64 mod2 = mod1 + 2;
const i64 base = 10007;
using mint1 = ModInt<mod1>;
using mint2 = ModInt<mod2>;
using pair_type = pair<mint1, mint2>;

struct RollingHash{
    int len;
    std::vector<pair_type> v;
    static std::vector<pair_type> power, inv;
    RollingHash(string s) : len(s.size()){
        v.assign(1, make_pair(mint1(0), mint2(0)));
        for(int i = 0; i < len; ++i){
            auto c = s[i];
            v.emplace_back(v.back().first + power[i].first * c,
                           v.back().second + power[i].second * c);
            if(static_cast<int>(power.size()) == i + 1){
                power.emplace_back(power.back().first * base, power.back().second * base);
                inv.emplace_back(mpow<mint1>(power.back().first, mod1 - 2),
                                 mpow<mint2>(power.back().second, mod2 - 2));
            }
        }
    }
    pair_type get(int l, int r){
        auto l_cut = make_pair(v[r].first - v[l].first,
                               v[r].second - v[l].second);
        
        return make_pair(l_cut.first * inv[l].first,
                         l_cut.second * inv[l].second);
    }
    static pair_type connect(pair_type l, pair_type r, int l_len){
        return make_pair(
        l.first + power[l_len].first * r.first,
            l.second + power[l_len].second * r.second);
    }
};

vector<pair_type> RollingHash::power = {make_pair(mint1(1), mint2(1))};
vector<pair_type> RollingHash::inv = {make_pair(mint1(1), mint2(1))};

signed main(){
    
    int n, m;
    cin >> n >> m;
    
    auto f = [](pair_type p){
        return ((p.first.p << 32) + p.second.p);
    };
    
    unordered_map<i64, int> s_l;
    unordered_map<i64, int> s_r;
    unordered_map<i64, int> s_center;
    
    for(int i = 0; i < n; ++i){
        string word;
        cin >> word;
        RollingHash rh(word);
        for(int j = 1; j <= word.size(); ++j){
            ++s_l[f(rh.get(0, j))];
            ++s_r[f(rh.get(word.size() - j, word.size()))];
        }
        ++s_center[f(rh.get(0, rh.len))];
    }
    
    for(int _ = 0; _ < m; ++_){
        string s;
        cin >> s;
        int idx = -1;
        bool l_ast = (s.front() == '*');
        bool r_ast = (s.back() == '*');
        s = s.substr(l_ast, s.size() - (l_ast | r_ast));
        for(int i = 0; i < s.size(); ++i)
            if(s[i] == '?'){
                idx = i;
                break;
            }
        int ans = 0;
        RollingHash rh(s);
        for(int i = 'a'; i <= 'z'; ++i){
            auto raw_rh = rh.get(0, rh.len);
            if(idx != -1)
                raw_rh = RollingHash::connect(RollingHash::connect(rh.get(0, idx), pair_type(mint1(i), mint2(i)), idx),
                    rh.get(idx + 1, rh.len), idx + 1);
            if(l_ast){
                if(s_r.find(f(raw_rh)) != s_r.end())
                    ans += s_r[f(raw_rh)];
            }else if(r_ast){
                if(s_l.find(f(raw_rh)) != s_l.end())
                    ans += s_l[f(raw_rh)];
            }else{
                if(s_center.find(f(raw_rh)) != s_center.end())
                    ans += s_center[f(raw_rh)];
            }
            if(idx == -1)
                break;
        }
        cout << ans << endl;
    }
    
    return 0;
}


Measured memory_bytes: 249104