#include <iostream>
#include <fstream>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <stack>
#include <list>	
#include <algorithm>
#include <limits.h>
#include <map>

using namespace std;

int main(){
	map<char, string> charMapings;
	for(int i = 0; i < 26; i++){
		char letter;
		string repres;

		cin >> letter >> repres;
		charMapings[letter] = repres;
		//cout << rep;
	}	
	int n;
	cin >> n;

	map<string, vector<string>> reprezentations;
	for(int i = 0; i < n; i++){
		string inter;
		cin >> inter;
		//cout << inter << endl;

		string rep = "";
		for(char c: inter){
			rep += charMapings[c];
		}
		//cout << rep << endl;
		auto it = reprezentations.find(rep);
		if(it == reprezentations.end()){
			vector<string> aux;
			aux.push_back(inter);
			reprezentations[rep] = aux;
		} else {
			reprezentations[rep].push_back(inter);
		}
	}
	int maxx = INT_MIN;

	for(auto p: reprezentations){
		//cout << p.second.size() << endl;
		int size = p.second.size();
		// for(int i = 0; i < p.second.size(); i++){
		// 	cout << p.second[i] << " ";
		// }
		// cout << endl;
		if(size > maxx){
			maxx = size;
		}
	}
	if(maxx == 1){
		cout << "-1" << endl;
	} else {
		cout << maxx << endl;
	}
	return 0;
}