/*
Outline: First, sort the dictionary and remove duplicates.
The idea is to pick a prefix and suffix from the dictionary, so that the fixed
letters appear in the right place in the conjunction, and that the set of
letters used in the conjunction equals the set of letters used in the query.

Each word is put in an index with the set of letters used as key. Now, we can
try each word in the dictionary as a prefix, and find all words that complete
the set of letters efficiently.

Solving took about 20-30 minutes.
*/

#include <cctype>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <map>
#include <iostream>
using namespace std;

struct CharCount
{
    int count[26];

    CharCount() { };
    CharCount(const std::string &s) {
        memset(count, 0, sizeof(count));
        for(string::const_iterator i = s.begin(); i != s.end(); ++i)
            ++count[tolower(*i) - 'a'];
    }

    bool operator< (const CharCount &cc) const {
        return memcmp(count, cc.count, sizeof(count)) < 0;
    }
};


void run()
{
    // Read dictionary
    int N,Q;
    cin >> N;
    vector<string> dict(N);
    for(int n = 0; n < N; ++n)
        cin >> dict[n];
    sort(dict.begin(), dict.end());
    dict.erase(unique(dict.begin(), dict.end()), dict.end());
    N = int(dict.size());

    // Build dictionary index
    typedef map<CharCount, vector<string*> > Index;
    Index index;
    vector<CharCount> dc;
    for(int n = 0; n < N; ++n)
    {
        dc.push_back(CharCount(dict[n]));
        index[dc.back()].push_back(&dict[n]);
    }

    // Process queries
    cin >> Q;
    for(int q = 0; q < Q; ++q)
    {
        typedef vector<pair<string*,string*> > Results;
        Results results;

        // Read a query, and build the set of letters used
        string qs;
        cin >> qs;
        CharCount qcc(qs);
        vector<int> fixed;
        for(int n = 0; n < 10; ++n)
            if(islower(qs[n]))
                fixed.push_back(n);

        // Try all possible prefixes
        for(int n = 0; n < N; ++n)
        {
            CharCount scc;

            // Make sure prefix matches query
            vector<int>::const_iterator fi = fixed.begin();
            for(; fi != fixed.end() && *fi < int(dict[n].size()); ++fi)
                if(dict[n][*fi] != qs[*fi])
                    goto skip_prefix;
                    
            // Build the set of letters to be used by the suffix
            for(int c = 0; c < 26; ++c)
                if(dc[n].count[c] > qcc.count[c])
                    goto skip_prefix;
                else
                    scc.count[c] = qcc.count[c] - dc[n].count[c];

            {
                // Find all possible suffixes
                vector<string*> &suffixes = index[scc];
                for( vector<string*>::const_iterator i = suffixes.begin();
                     i != suffixes.end(); ++i )
                {
                    // Make sure suffix matches query
                    for(vector<int>::const_iterator fi2=fi; fi2 != fixed.end(); ++fi2)
                        if((**i)[*fi2 - dict[n].size()] != qs[*fi2])
                            goto skip_suffix;
                    results.push_back(make_pair(&dict[n], *i));
                skip_suffix: continue;
                }
            }
        
        skip_prefix: continue;
        }
        
        // Output results
        cout << results.size() << '\n';
	if(results.size() > 1000)
	    results.resize(1000);
        for(Results::const_iterator i = results.begin(); i != results.end(); ++i)
            cout << *i->first << '-' << *i->second << '\n';
    }
}

int main()
{
    int runs;
    cin >> runs;
    while(runs--)
        run();
}
