#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using pii = pair<int, int>;
#define dbg(x) cerr<<#x": "<<(x)<<'\n'
#define dbg_v(x, n) cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<(x)[_]<<' ';cerr<<'\n'
#define all(v) v.begin(), v.end()

struct container_hash {
    std::size_t operator() (const vector<int> &v) const
    {
        int i, h;
        for(h = 0, i = 0; i < v.size(); ++i)
        	h = (h << 1) | v[i];

        return h;
    }
};

unordered_map<vector<int>, int, container_hash> hMap;

int main()
{
#ifndef ONLINE_JUDGE
	ifstream cin("data.in");
	ofstream cout("data.out");
#endif
	ios_base::sync_with_stdio(false);

	int i, j, let, n, nrMax;
	char c;
	string repr, word;
	vector<int> v[26], aux;

	for(i = 0; i < 26; ++i)
	{
		cin >> c >> repr;
		let = c - 'a';

		v[let].clear();
		for(j = 0; j < repr.size(); ++j)
			if(repr[j] == '.')
				v[let].push_back(0);
			else
				v[let].push_back(1);
	}

	nrMax = 0;
	for(cin >> n; n; --n)
	{
		cin >> word;
		aux.clear();

		for(i = 0; i < word.size(); ++i)
			aux.insert(aux.end(), v[word[i] - 'a'].begin(), v[word[i] - 'a'].end());

		hMap[aux]++;
		nrMax = max(nrMax, hMap[aux]);
	}

	cout << nrMax << '\n';

	return 0;
}