//#include #include #include #include using namespace std; //ifstream cin ("x.in"); ofstream cout ("x.out"); const int mod = 666013; const int base = 3; const int nmax = 1e5 + 10; struct str{ int x, y; str() {} str (int _x, int _y) { x = _x, y = _y; } inline str operator + (const int &a) const { return str((x + a) % mod, y + a); } inline str operator * (const int &a) const { return str((x * a) % mod, y * a); } inline bool operator < (const str &a) const { return ((x != a.x) ? (x < a.x) : (y < a.y)); } inline bool operator == (const str &a) const { return (x == a.x && y == a.y); } }; str v[nmax + 1]; const int omega = 26; string s[omega + 1]; string S; int main() { for (int i = 1; i <= omega; ++ i) { char c; cin >> c; cin >> s[c - 'a']; } int n; cin >> n; for (int i = 1; i <= n; ++ i) { cin >> S; str h = str(0, 0); for (auto j : S) { for (auto k : s[j - 'a']) { int val = (k == '.' ? 1 : 2); h = h * base + val; } } v[ i ] = h; } sort(v + 1, v + n + 1); int ans = 0; for (int i = 1; i <= n; ) { int j = i; while (j <= n && v[ i ] == v[ j ]) { ++ j; } ans = max(ans, j - i); i = j; } if (ans == 1) ans = -1; cout << ans << "\n"; return 0; }