#include <bits/stdc++.h>

using namespace std;

int N;
unordered_map<char, string>H1;
unordered_map<string, int>H2;

int main()
{
    for(int i = 1; i <= 26; i ++)
    {
        char ch = 0;
        string s1 = "";
        cin >> ch >> s1;
        H1[ch] = s1;
    }
    cin >> N;
    for(int i = 1; i <= N; i ++)
    {
        string s1 = "", solution = "";
        cin >> s1;
        for(int i = 0; i < s1.size(); i ++)
        {
            solution += H1[s1[i]];
        }
        H2[solution] ++;
    }

    int solution = 0;
    for(auto it : H2)
    {
        solution = max(solution, it.second);
    }

    if(solution == 1)
    {
        cout << -1;
        return 0;
    }
    cout << solution;

    return 0;
}