#include using namespace std; const int SIGMA = 26; string rep[SIGMA]; const int P = 1e9 + 7; mapap; void calc(const string &s) { string cur; for (int i = 0; i < s.size(); ++i) cur += rep[s[i] - 'a']; ++ap[cur]; } int main() { #ifndef ONLINE_JUDGE freopen("cutit.in", "r", stdin); #endif // ONLINE_JUDGE ios::sync_with_stdio(0); for (int i = 0; i < SIGMA; ++i) { char ch; cin >> ch; cin >> rep[ch - 'a']; } int n; string s; cin >> n; for (int i = 0; i < n; ++i) { cin >> s; calc(s); } int ans = 0; for (auto i : ap) ans = max(ans, i.second); if(ans != 1) cout << ans; else cout << -1; return 0; }