// Arup Guha
// 1/16/2024
// Solution to Kattis Problem: Paintings
// https://open.kattis.com/problems/paintings
// Used to illustrate backtracking in CS2.

import java.util.*;

public class paintings {

	public static int n;
	public static String[] colors;
	public static HashMap<String,Integer> map;
	public static boolean[][] conflict;
	public static int[] firstperm;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {
			
			n = stdin.nextInt();
			colors = new String[n];
			map = new HashMap<String,Integer>();
			
			// read colors.
			for (int i=0; i<n; i++) {
				colors[i] = stdin.next();
				map.put(colors[i], i);		
			}
			
			// Set up conflicts.
			conflict = new boolean[n][n];
			int numE = stdin.nextInt();
			
			// put in conflicts.
			for (int i=0; i<numE; i++) {
				int u = map.get(stdin.next());
				int v = map.get(stdin.next());
				conflict[u][v] = true;
				conflict[v][u] = true;
			}
			
			// Set up.
			int[] perm = new int[n];
			boolean[] used = new boolean[n];
			firstperm = null;
			
			// Solve it.
			int res = go(perm, used, 0);
			
			// Ta da!
			System.out.println(res);
			for (int i=0; i<n-1; i++)
				System.out.print(colors[firstperm[i]]+" ");
			System.out.println(colors[firstperm[n-1]]);
		}
	}
	
	// Returns # of solutions where perm[0..k-1] is fixed.
	public static int go(int[] perm, boolean[] used, int k) {
		
		// Filled in!
		if (k == n) {
			
			// copy if necessary.
			if (firstperm == null) {
				firstperm = new int[n];
				for (int i=0; i<n; i++) firstperm[i] = perm[i];
			}
			
			// # of solutions.
			return 1;
		}
		
		int res = 0;
		
		// try color i next.
		for (int i=0; i<n; i++) {
			
			// used color already.
			if (used[i]) continue;
			
			// Color combo not allowed.
			if (k > 0 && conflict[perm[k-1]][i]) continue;
			
			// Usual permutation code...
			used[i] = true;
			perm[k] = i;
			res += go(perm, used, k+1);
			used[i] = false;
		}
		
		return res;
	}
}