#include using ll = long long; using namespace std; #define MOD 1000000007 #define Q 11 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; int main() { // string s = "61*5100"; // string s1 = "612345000"; // cout << cmp_orig(s1, s) << endl; ll n; cin >> n; for(int i = 0; i < n; ++i) { string s; cin >> s; auto ptr = make_shared(s); ll h = hash_string(s); m[h].insert(ptr); for(int j = 0; j < 9; ++j){ for(int k = j + 1; k < 9; ++k) { auto p = hash_in(s,j,k); m[p.first].insert(ptr); m[p.second].insert(ptr); } h = hash_one_q(s,j); m[h].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; } }