#include using ll = long long; using namespace std; #define MOD 1000000007 const ll arr[] = { 11, 121, 1331, 14641, 161051, 1771561, 19487171, 214358881, 2357947691, 25937424601, 285311670611, 3138428376721, 34522712143931, 379749833583241 }; ll hash_s(string & s) { ll h = 0; ll offset = 9 - s.size(); bool useOffset = false; for(int i = 0; i < s.size(); ++i) { if (s[i] == '*'){ useOffset = true; continue; } if(s[i] != '*' && s[i] != '?') { if(useOffset) h += (ll)(1+s[i] - '0')*arr[i + offset] % MOD; else h += (ll)(1+s[i] - '0')*arr[i] % MOD; h = h % MOD; } } // cout << h; return h; } pair hash_in(string &s, int j, int k) { ll h = 0; ll offset = 9 - s.size(); ll h_with_star = 0; ll h_with_questions = 0; bool useOffset = false; for(int i = 0; i < s.size(); ++i) { if (i >= j && i <= k){ if(i == j) continue; if(i == k) continue; h_with_questions += (1+s[i] - '0')*arr[i] % MOD; h_with_questions = h_with_questions % MOD; continue; } h_with_questions += (1+s[i] - '0')*arr[i] % MOD; h_with_questions = h_with_questions % MOD; h_with_star += (1+s[i] - '0')*arr[i] % MOD; h_with_star = h_with_star % MOD; } return make_pair(h_with_questions,h_with_star); } ll hash_q(string &s, int j) { ll h = 0; ll offset = 9 - s.size(); ll h_with_questions = 0; bool useOffset = false; for(int i = 0; i < s.size(); ++i) { if(i == j) continue; h_with_questions += (1+s[i] - '0')*arr[i] % MOD; h_with_questions = h_with_questions % MOD; } return h_with_questions; } ll hash_string(string & s) { ll h = 0; ll offset = 9 - s.size(); bool useOffset = false; for(int i = 0; i < s.size(); ++i) { if (s[i] == '*'){ useOffset = true; continue; } if(s[i] != '*' && s[i] != '?') { if(useOffset) h += (ll)(1+s[i] - '0')*arr[i + offset] % MOD; else h += (ll)(1+s[i] - '0')*arr[i] % MOD; h = h % MOD; } } return h; } ll hash_one_q(string &s, int j) { ll h_q = 0; for(int i = 0; i < s.size(); ++i) { if(i == j) continue; h_q += (1 + s[i] - '0') * arr[i] % MOD; h_q = h_q % MOD; } return h_q; } ll hash_two_q(string &s, int j, int k) { ll h_with_questions = 0; for(int i = 0; i < s.size(); ++i) { if(i == j) continue; if(i == k) continue; h_with_questions += (1+s[i] - '0')*arr[i] % MOD; h_with_questions = h_with_questions % MOD; } return h_with_questions; } ll hash_with_s(string &s, int j, int k) { ll h_with_star = 0; for(int i = 0; i < s.size(); ++i) { if (i >= j && i <= k){ continue; } h_with_star += (1+s[i] - '0')*arr[i] % MOD; h_with_star = h_with_star % MOD; } return h_with_star; } bool cmp_orig(const string & orig, const string & match) { bool with_offset = false; ll offset = 9 - match.size(); for(int i = 0; i < match.size(); i++) { if(match[i] == '*') { with_offset = true; continue; } if(match[i] == '?') continue; if(with_offset){ if(match[i] != orig[i + offset]) { return false; } } else if(match[i] != orig[i]) return false; } return true; } unordered_map>> m; ll hash_add(char c, int index, ll h) { h += (1+c - '0')*arr[index] % MOD; return h % MOD; } ll hash_remove(char c, int index, ll h) { h += MOD - ((1+c - '0')*arr[index] % MOD); return h % MOD; } ll hash_minus(ll h1, ll h2) { return (MOD + h1 - h2) % MOD; } ll hash_plus(ll h1, ll h2) { return (h1 + h2) % MOD; } int main() { ll n; cin >> n; for(int i = 0; i < n; ++i) { string s; cin >> s; auto ptr = make_shared(s); vector front; vector back; ll h = 0; for(int j = 0; j < 9; ++j) { h = hash_add(s[j],j,h); front.push_back(h); } ll h_all = front[8]; for(int j = 0; j < 9; ++j) { h = hash_remove(s[j],j,h); back.push_back(h); } for(int j = 0; j < 9; ++j){ for(int k = j + 1; k < 9; ++k){ ll h_tmp = hash_plus(front[j],back[k]); m[h_tmp].insert(ptr); } } for(int j = 0; j < 9; ++j) { ll h_one_q = hash_remove(s[j], j, h_all); for(int k = j + 1; k < 9; ++k) { ll h_two_q = hash_remove(s[k], k, h_one_q); m[h_two_q].insert(ptr); } m[h_one_q].insert(ptr); } m[h_all].insert(ptr); } ll q; cin >> q; for(int i = 0; i < q; ++i){ string s; cin >> s; ll h = hash_string(s); ll cnt = 0; for(const auto & val : m[h]) { if(cmp_orig(*val, s)) cnt++; } cout << cnt << endl; } }