#include using namespace std; // Optimization stuff inline void debugMode() { #ifndef ONLINE_JUDGE freopen("debug.in", "r", stdin); #endif // ONLINE_JUDGE } inline void optimizeIt() { ios::sync_with_stdio(false); cin.tie(NULL); } // End optimization stuff inline double ABS(const int &x) { return max(x, -x); } inline bool isPrime(const int &x) { if(x == 1) return false; for(int d = 2; d * d <= x; d++) { if(x % d == 0) return false; } return true; } const int NMax = 1e6 + 5; const int LIM = 1e6; const int MOD = 1e7 + 7; string v[40]; int main(){ debugMode(); optimizeIt(); for(int i = 0; i < 26; i++) { char c; cin >> c >> v[i]; } int n; cin >> n; map < string, int > Map; for(int i = 1; i <= n; i++) { string s; cin >> s; string aux (""); for(auto const &it: s) { aux = aux + v[it - 'a']; } Map[aux]++; } int best = 0; for(auto const &it: Map) { best = max(best, it.second); } if(best == 1) { cout << "-1"; } else { cout << best; } return 0; }