// Arup Guha
// 4/11/2023
// Solution to 2017 MAPS Problem G: Paintings

import java.util.*;
import java.io.*;

public class paintings {

	public static int n;
	public static String[] colors;
	public static HashMap<String,Integer> map;
	public static boolean[][] banned;
	public static ArrayList<Integer> res;
	
	public static void main(String[] args) throws Exception {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {
		
			// Get colors.
			n = stdin.nextInt();
			colors = new String[n];
			map = new HashMap<String,Integer>();
			for (int i=0; i<n; i++) {
				colors[i] = stdin.next();
				map.put(colors[i], i);
			}
			banned = new boolean[n][n];
			
			// Build banned list.
			int numB = stdin.nextInt();
			for (int i=0; i<numB; i++) {
				int u = map.get(stdin.next());
				int v = map.get(stdin.next());
				banned[u][v] = true;
				banned[v][u] = true;
			}
			
			// Set up brute force.
			int[] perm = new int[n];
			boolean[] used = new boolean[n];
			res = null;
			
			// Run it.
			int cnt = go(perm, used, 0);
			
			// Ta da!
			System.out.println(cnt);
			System.out.print(colors[res.get(0)]);
			for (int i=1; i<n; i++)
				System.out.print(" "+colors[res.get(i)]);
			System.out.println();
				
		}

	}

	// Returns the # of solutions with perm[0..k-1] fixed.
	public static int go(int[] perm, boolean[] used, int k) {
		
		// Finished one; record it if it's the first.
		if (k == n) {
			if (res == null) {
				res = new ArrayList<Integer>();
				for (int i=0; i<n; i++) res.add(perm[i]);
			}
			return 1;
		}
		
		// Try each color in order of preference.
		int ans = 0;
		for (int i=0; i<n; i++) {
			if (used[i]) continue;
			if (k > 0 && banned[perm[k-1]][i]) continue;
			used[i] = true;
			perm[k] = i;
			ans += go(perm, used, k+1);
			used[i] = false;
		}
		
		return ans;
	}
}	