#include <cstdio>
#include <cctype>
#include <map>
#include <string>
#include <vector>

using namespace std;

typedef multimap<string,string> MSS;
typedef MSS::iterator MIT;

int main()
{
	int cases; scanf("%d", &cases);

	while( cases-- ) {

		int n; scanf("%d", &n);

		vector<string> words;

		for( int i = 0; i < n; ++i ) {

			char buffer[10]; scanf("%s", buffer);

			words.push_back(buffer);
		}

		MSS combi;

		for( int i = 0; i < n; ++i ) for( int j = 0; j < n; ++j ) {

			if( words[i].length() + words[j].length() != 10 ) continue;

			string sorted = words[i]+words[j];
			sort(sorted.begin(),sorted.end());

			string combo = words[i]+"-"+words[j];

			combi.insert(make_pair(sorted,combo));
		}

		int q; scanf("%d", &q);

		while( q-- ) {

			char buffer[11]; scanf("%s", buffer);

			string query = buffer;

			for( int i = 0; i < 10; ++i ) buffer[i] = tolower(buffer[i]);

			string lower = buffer; sort(lower.begin(), lower.end());

			pair<MIT,MIT> range = combi.equal_range(lower);

			vector<string> result;

			for( MIT it = range.first; it != range.second; ++it ) {

				const char *qstr = query.c_str(), *p = it->second.c_str();
				int i;

				for( i = 0; i < 10; ++i, ++p ) {
					if( *p == '-' ) ++p;
					if( islower(qstr[i]) && qstr[i] != *p ) break;
				}

				if( i == 10 ) {
					result.push_back(it->second);
				}
			}

			sort(result.begin(),result.end());

			printf("%u\n",result.size());
			for( int i = 0; i < result.size() && i < 1000; ++i ) printf("%s\n", result[i].c_str());
		}
	}

	return 0;
}

