// mcb.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <map>
#include <string>
#include <cstring>
using namespace std;


map<char, string> alpha;
map<string, int> beta;

char litera;
string morse;
char cuv[100005];
int i, n, maxim;
int main()
{
    for (i = 'a'; i <= 'z'; i++)
    {
        cin >> litera;
        cin >> morse;
        alpha[litera] = morse;
    }

    cin >> n;
    for (i = 1; i <= n; i++)
    {
        cin >> cuv;
        int p = strlen(cuv);
        morse = "";
        for (int j = 0; j < p; j++)
        {
            morse += alpha[cuv[j]];
        }
        if (beta[morse] == NULL)
        {
            beta[morse] = 1;
        }
        else
        {
            beta[morse]++;
        }

    }

    map<string, int>::iterator it;
    for (it = beta.begin(); it != beta.end(); it++)
    {
        if ((*it).second > maxim)
        {
            maxim = (*it).second;
        }
    }

    cout << maxim;

}