#include <iostream>
#include <fstream>
using namespace std;
ifstream f("castel.in");
ofstream g("castel.out");
int n, cod, i, k, cif, fr[10], cheie, apmax, caut;
int main()
{
f >> n;
for (i=1; i<=n; ++i)
{
f >> cod;
while (cod)
{
cif=cod%10;
++fr[cif];
cod=cod/10;
}
}
f >> k;
for (i=1; i<=k; ++i)
{
f >> cheie;
if (fr[cheie]>apmax) {apmax=fr[cheie]; caut=cheie;}
}
g << caut << " " << apmax;
}