#include using namespace std; #define ll long long #define FOR(i, j, k, in) for (int i=j; i=k; i-=in) #define REP(i, j) FOR(i, 0, j, 1) #define RREP(i, j) RFOR(i, j, 0, 1) #define ALL(a) a.begin(), a.end() #define RALL(a) a.end(), a.begin() #define F first #define S second #define PB push_back #define MP make_pair #define pii pair #define MOD 1000000007 struct Trie { int count = 0; vector normal = vector(10, nullptr); vector reversed = vector(10, nullptr); }; void rev_insert(Trie* t, string w, int from); int rev_count(Trie* t, string w, int from, int i); void insert(Trie* t, string w) { REP(i, w.size()) { rev_insert(t, w, i); t->normal[w[i]] = t->normal[w[i]] == nullptr ? new Trie : t->normal[w[i]]; t = t->normal[w[i]]; t->count++; } } void rev_insert(Trie* t, string w, int from) { RFOR(i, w.size()-1, from, 1) { t->reversed[w[i]] = t->reversed[w[i]] == nullptr ? new Trie : t->reversed[w[i]]; t = t->reversed[w[i]]; t->count++; } } int count(Trie* t, string w, int i) { for (; i < w.size(); i++) { if (w[i] == '?') { int res = 0; for (auto* x : t->normal) { if (x) res += count(x, w, i + 1); } return res; } else if (w[i] == '*') { return rev_count(t, w, i + 1, w.size() - 1); } else { t = t->normal[w[i] - '0']; if (!t) return 0; } } return t->count; } int rev_count(Trie* t, string w, int from, int i) { for (; i >= from; i--) { if (w[i] == '?') { int res = 0; for (auto* x : t->reversed) { if (x) res += rev_count(x, w, from, i + 1); } return res; } else if (w[i] == '*') { //return rev_count(t, w, i + 1); } else { t = t->reversed[w[i] - '0']; if (!t) return 0; } } return t->count; } void solve() { Trie t; int N, Q; cin >> N; REP(i, N) { string w; cin >> w; for (char& c : w) c -= '0'; insert(&t, w); } cin >> Q; REP(i, Q) { string w; cin >> w; cout << count(&t, w, 0) << endl; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n=1; // cin >> n; REP(i, n) { solve(); } return 0; }