#include <fstream>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <iomanip>
#include <algorithm>

#define N 1000000
using namespace std;
char c, c2;
string v[30];
map<string, int> dex;

int main()
{
	for (int i = 0; i < 26; i++)
	{
		cin >> c;
		cin >> v[c - 'a'];
	}

	int n;
	string word;

	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> word;
		string s = "";
		for (int i = 0; i < word.size(); i++)
		{
			s += v[word[i] - 'a'];
		}

		dex[s] ++;
	}

	int best = 0;
	for (map<string, int>::iterator it = dex.begin(); it != dex.end(); it++)
	{
		if (best < it->second)
			best = it->second;
	}
	if (best <= 1)
		best = -1;
	cout << best;
	return 0;
}