#include <bits/stdc++.h>

#define pb push_back
#define f first
#define s second
#define pii pair<int, int>
#define mp make_pair
 
using namespace std;

unordered_map<string, int> hash_table;
unordered_map<int, string> morse_rep;

void construct_repr(string& word) {
	string representation = "";
	for (int i = 0; i < word.size(); i++) {
		representation += morse_rep[word[i] - 'a'];
	}

	hash_table[representation]++;
}

int main() {
	cin.sync_with_stdio(false);

	char letter;
	string repr;
	for (int i = 0; i < 26; i++) {
		cin >> letter >> repr;
		morse_rep[i] = repr;
	}

	int q;
	cin >> q;
	string word;
	for (int i = 0; i < q; i++) {
		cin >> word;
		construct_repr(word);
	}

	int maxx = 0;
	for (auto entry : hash_table) {
		maxx = max(maxx, entry.second);
	}

	if (maxx == 1) {
		cout << -1;
	} else {
		cout << maxx;		
	}
	return 0;
}