#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

string map[128];
string words[100000];
int n;
int eq[100000];

int main() {
    for (char c = 'a'; c <= 'z'; c++) {
        cin >> c;
        cin >> map[c];
    }

    cin >> n;
    string word;
    for (int i = 0; i < n; i++) {
        cin >> word;
        for (int j = 0; j < word.size(); j++)
            words[i] += map[word[j]];
    }

    for (int i = 0; i < n; i++) {
        eq[i] = 1;
    }

    int maxeq = 0;
    for (int i = 0; i < n; i++) {
        if (eq[i] == 0)
            continue;
        for (int j = i + 1; j < n; j++) {
            if (eq[j] != 0 && words[i] == words[j]) {
                eq[i] += eq[j];
                eq[j] = 0;
            }
        }
        if (eq[i] > maxeq)
            maxeq = eq[i];
    }

    cout << maxeq << '\n';
}