#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

struct el{
    int bit, n;
    el * next[2] = {0, 0};
}* prim, *p, * q;

vector < int > LIT[200];
char s[200], c[200][100005];
int n;

int Solve(el * p) {
    if (p == NULL) return 0;
    if (p->next[0] == NULL && p->next[1] == NULL)
        return p->n;
    else return max(Solve(p->next[0]), Solve(p->next[1]));
}

int main()
{
    for (int i = 'a'; i <= 'z'; i++)
    {
        cin.getline(s, 199);
        strcpy(s, s + 2);
        for (int j = 0; s[j] == '-' || s[j] == '.'; j++)
        {
            if (s[j] == '-') LIT[i].push_back(1);
            else LIT[i].push_back(0);
        }
    }
    cin>>n;
    //cin.gets();
    char c[100000];
        vector < int > word;
    prim = new el;
    prim->next[0] = prim->next[1] = NULL;
    int Max = 0;
    for (int i = 0; i < n; i++)
    {
        cin>>c;
        p = prim;
        word.clear();
        for (int j = 0; j<strlen(c); j++)
            {
                for (int q = 0; q < LIT[c[j]].size(); q++)
                    word.push_back(LIT[c[j]][q]);
            }
        for (int j = 0; j < word.size(); j++) {
            if (p->next[word[j]] == NULL) {
                q = new el;
                q->bit = word[j];
                q->next[0] = q->next[1] = NULL;
                q->n = 1;
                p->next[word[j]] = q;
            }
            else
            {
                p->next[word[j]]->n++;
            }
            if (j == word.size() - 1)
            {
                Max = max(Max, p->next[word[j]]->n);
            }

            p = p->next[word[j]];
        }
    }
    p = prim;

    cout <<Max;
    return 0;
}