#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
	map<char, string> m;
	for (int i = 0; i < 26; i++) {
		char c; string s;
		cin >> c>>s;
		m[c] = s;
	}

	int n; cin >> n;
	map<string, int> w;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		int k = s.size(); string res="";
		for (int j = 0; j <k; j++) {
			
			res += m[s[j]];
		}
		if (w.find(res) != w.end()) {
			w[res]++;
		}
		else {
			w[res] = 1;
		}
	}
	int maxl = 0;
	for (auto it = w.begin(); it != w.end();++it) {
		maxl = max(maxl, it->second);
	}

	if (maxl <= 1) {
		cout << -1;
	}
	else
		cout << maxl;


    return 0;
}