import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class maks_slow3
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int cases = sc.nextInt();
		while (cases-- > 0)
		{
			String[] dict = new String[sc.nextInt()];
			for (int n = 0; n < dict.length; ++n)
				dict[n] = sc.next();
			int queries = sc.nextInt();
			while (queries-- > 0)
			{
				String q = sc.next();
				int qcount[] = new int[26];
				for (int n = 0; n < 10; ++n)
					qcount[Character.toLowerCase(q.charAt(n)) - 'a'] += 1;

				ArrayList<String> results = new ArrayList<String>();
			outer_loop:
				for (String a: dict)
				{
					for (int n = 0; n < a.length(); ++n)
						if (Character.isLowerCase(q.charAt(n)) && q.charAt(n) != a.charAt(n))
							continue outer_loop;
							
					for (String b: dict)
					{
						if (a.length() + b.length() != 10)
							continue;

						String c = a + b;
						boolean correct = true;
						int count[] = new int[26];
						for (int n = 0; n < 10; ++n)
						{
							if (Character.isLowerCase(q.charAt(n)))
								correct = correct && q.charAt(n) == c.charAt(n);
							++count[c.charAt(n) - 'a'];
						}
						for (int n = 0; n < 26; ++n)
							if (count[n] != qcount[n])
								correct = false;
						if (correct)
							results.add(a + '-' + b);
					}
				}
				System.out.println(results.size());
				Collections.sort(results);
				for (int n = 0; n < results.size() && n < 1000; ++n)
					System.out.println(results.get(n));
			}
		}
		
	}
};
