#include <iostream>
#include <cstdio>
#include <map>
#include <string>

using namespace std;

map <char, string> Morse;
map <string, int> My;

int main() {
//    #ifndef ONLINE_JUDGE
//    freopen("input.txt","r",stdin);
//    #endif // ONLINE_JUDGE

    char c;
    string code;
    for(char ch = 'a'; ch <= 'z'; ++ch) {
        cin >> c >> code;

        Morse[c] = code;
    }

    int N;
    cin >> N;
    int ans = 0;
    for(int i = 1; i <= N; ++i) {
        cin >> code;

        string coded = "";
        for(unsigned int j = 0; j < code.length(); ++j) {
            coded += Morse[(char)code[j]];
        }

        ans = max(ans, ++My[coded]);
    }

    cout << (ans == 1 ? -1 : ans) << '\n';

    return 0;
}