#include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; #define umap unordered_map #define uset unordered_set #define int ll namespace std { template struct hash> { size_t operator()(const pair &a) const { return std::hash()(a.first) ^ (std::hash()(a.second) << 10); } }; } struct State { pair node; uset> seenAtLevel; ll seen; ll dist; bool operator<(const State &other) const { return dist > other.dist; } }; struct Node { int val; std::array, 10> children; Node *parent; Node(int val, const Node *parent) : val(val), children({}), parent(nullptr) {} void addString(const char *str) { if (!*str) return; int next = *str - '0'; if (!(children[next])) { children[next] = make_unique(next, this); } children[next]->addString(str + 1); } int match(const char *fmt, const string &pref) { if (!*fmt) { return pref.size() == 9 ? 1 : 0; } // if (*fmt == '*') { // int matches = 0; // matches += match(fmt + 1, pref + to_string(val)); // // for (int i = 0; i < 10; ++i) { // if (children[i]) matches += children[i]->match(fmt, pref + to_string(i)); // } // // return matches; // } if (*fmt == '?') { int matches = 0; for (int i = 0; i < 10; ++i) { if (children[i]) matches += children[i]->match(fmt + 1, pref + to_string(i)); } return matches; } int val = *fmt - '0'; if (children[val]) { return children[val]->match(fmt + 1, pref + to_string(val)); } return 0; } }; string replaceStar(string str) { ll starIdx = -1; for (ll i = 0; i < str.size(); ++i) { if (str[i] == '*') { starIdx = i; break; } } if (starIdx == -1) return str; ll originalLen = str.size(); str.resize(9, ' '); string result; ll qcount = 10 - originalLen; for (int i = 0; i < 9; ++i) { if (i >= starIdx) { if (i >= qcount + starIdx) { result += str[i-qcount+1]; } else{ result += '?'; } } else { result += str[i]; } } return result; } signed main() { Node tree(-1, nullptr); int n; cin >> n; while (n--) { string str; cin >> str; tree.addString(str.c_str()); } cin >> n; while (n--) { string q; cin >> q; string noStar = replaceStar(std::move(q)); cout << tree.match(noStar.c_str(), "") << endl; } return 0; }