#include <iostream>
#include <unordered_map>
#include <map>

using namespace std;

int main()
{
    char c;
    string s;
    int nr;
    unordered_map<char, string> alphabet;
    map<string, int> result;

    for( int i = 0; i<26; ++i )
    {
        cin>> c>> s;
        alphabet[c] = s;
    }

    int numberOfWords;
    cin>> numberOfWords;
    for( int i = 0; i<numberOfWords; ++i )
    {
        cin>> s;
        string aux = "";
        for( int j = 0; j<s.length(); ++j )
        {
            aux.append(alphabet[s[j]]);
        }
        cout<<aux<< endl;
        result[aux]++;
    }
    cout<<endl<<endl;
    for(auto it : result)
    {
        cout<< it.first<<" "<<it.second<<endl;
    }

    cout<< result.begin()->second;

    return 0;
}