#include <fstream>
#include <iostream>
#include <string.h>
#include <unordered_map>

#define chMax 26

using namespace std;


int n, Sol;
unordered_map<char, string> repr;
unordered_map<string, int> harta;

int main()
{
    for(int i=0; i<chMax; i++)
    {
        char ch;
        string code;
        cin>>ch>>code;
        repr[ch]=code;
    }

    cin>>n;
    for(int i=1; i<=n; i++)
    {
        char ch;
        string code, word="";
        cin>>code;
        for(int j=0; code[j]>='a' && code[j]<='z'; j++)
            word+=repr[code[j]];

        harta[word]++;
        Sol=max(Sol, harta[word]);
    }

    cout<<(Sol==1 || Sol==0 ? -1 : Sol);
}