#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, }; 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; } unordered_map>> m; int main() { // string s = "6*00"; // hash_s(s); ll n; cin >> n; for(int i = 0; i < n; ++i) { string s; auto ptr = make_shared(s); cin >> s; ll h = hash_s(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_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_s(s); cout << m[h].size() << endl; } }