#include using namespace std; //#define int long long #define f(i,a,b) for(int i = (a); i < (b); ++i) using ll = long long; signed main() { ios_base::sync_with_stdio(0); int n; cin >> n; map counter; while (n--) { string num; cin >> num; ll hash = 0; for (int i = 0; i < 9; ++i) hash = hash * 11ll + (num[i] - '0' + 1); counter[hash] += 1; for (ll i = 8, power = 1; i>= 0; --i, power *= 11) { ll npower = power; ll hash2 = 0; for (int j = i; j >= 0; --j, npower *= 11) { hash2 += npower * (num[j] - '0' + 1); ++counter[hash - hash2]; } } } int q; cin >> q; while (q--) { string num; cin >> num; string nnum = ""; int sz = num.size(); for (char i: num) { nnum += i; if (i == '*') { while (sz < 9) { ++sz; nnum += '*'; } } } num = nnum; int i1 = -1, i2 = -1; for (int i = 0; i < 9; ++i) { if (num[i] == '?') { if (i1 == -1) i1 = i; else i2 = i; } } ll hash = 0; for (char i : num) { if (i == '?') hash *= 11ll; else if (i == '*') hash *= 11ll; else hash = hash * 11 + (i - '0' + 1); } if (i1 == -1) cout << counter[hash] << '\n'; else { ll power1 = 1; for (int i = i1; i < 8; ++i) power1 *= 11ll; ll res = 0; for (int i = 1; i <= 10; ++i) { if (i2 == -1) res += counter[hash + i * power1]; else { ll power2 = 1; for (int j = i2; j < 8; ++j) power2 *= 11ll; for (int j = 1; j <= 10; ++j) res += counter[hash + i * power1 + j * power2]; } } cout << res << '\n'; } } return 0; }