#include <bits/stdc++.h>
using namespace std;

int i, n, rs;
char c, x;
string s, aux;
map<char, string> morse;
map<string, char> imorse;
map<string, int> M;

int main() {
  ios_base::sync_with_stdio(0);

  for(c = 'a'; c <= 'z'; ++c) cin >> x >> s, morse[x] = s, imorse[s] = x;

  cin >> n;
  for(i = 1; i <= n; ++i) {
    cin >> s; aux.clear();

    for(auto it : s) aux += morse[it];

    ++M[aux];
    rs = max(rs, M[aux]);
  }

  cout << rs << '\n';

  return 0;
}