#include using namespace std; using uint = unsigned int; using ll = long long; using pii = pair; #define dbg(x) cerr<<#x": "<<(x)<<'\n' #define dbg_v(x, n) cerr<<#x"[]: ";for(long long _=0;_ &v) const { int i, h; for(h = 0, i = 0; i < v.size(); ++i) h = (h << 1) | v[i]; return h; } }; unordered_map, int, container_hash> hMap; int main() { #ifndef ONLINE_JUDGE ifstream cin("data.in"); ofstream cout("data.out"); #endif ios_base::sync_with_stdio(false); int i, j, let, n, nrMax; char c; string repr, word; vector v[26], aux; for(i = 0; i < 26; ++i) { cin >> c >> repr; let = c - 'a'; v[let].clear(); for(j = 0; j < repr.size(); ++j) if(repr[j] == '.') v[let].push_back(0); else v[let].push_back(1); } nrMax = 0; for(cin >> n; n; --n) { cin >> word; aux.clear(); for(i = 0; i < word.size(); ++i) aux.insert(aux.end(), v[word[i] - 'a'].begin(), v[word[i] - 'a'].end()); hMap[aux]++; nrMax = max(nrMax, hMap[aux]); } cout << (nrMax <= 1 ? -1 : nrMax) << '\n'; return 0; }