// Arup Guha
// Solution to COT 3100 Program #1: Set Operations
// 1/23/2014

// Note: This solution is specific to the input spec that says that all items 
//       in each set will be in between 0 and 100, inclusive

import java.util.*;

public class set {
	
	// Constants
	final public static int MAX = 100;
	final public static int ENDLIST = -1;
	
	// Instance variable
	private boolean[] members;
	
	// Set each element in items to true in our array.
	public set(ArrayList<Integer> items) {
		members = new boolean[MAX+1];
		for (Integer i: items)
			members[i] = true;
	}
	
	// We already have a true false list, so use it!
	public set(boolean[] memberList) {
		members = memberList;
	}
	
	// Returns a string version of this set, in numerical order.
	public String toString() {
		String ans = "";
		for (int i=0; i<MAX+1; i++)
			if (members[i])
				ans = ans + i + " ";
		return ans;
	}	
	
	// Returns the intersection of this set and other.
	public set intersect(set other) {
		
		// Use the definition of intersection through the universe of elements.
		boolean[] ans = new boolean[MAX+1];
		for (int i=0; i<ans.length; i++)
			ans[i] = members[i] && other.members[i];
			
		return new set(ans);
	}
	
	// Returns the union of this and other.
	public set union(set other) {

		// Use the definition of union through the universe of elements.
		boolean[] ans = new boolean[MAX+1];
		for (int i=0; i<ans.length; i++)
			ans[i] = members[i] || other.members[i];
			
		return new set(ans);		
	}

	// Returns this - other, using set difference.	
	public set subtract(set other) {

		// Use the definition of set difference through the universe of elements.
		boolean[] ans = new boolean[MAX+1];
		for (int i=0; i<ans.length; i++)
			ans[i] = members[i] && !other.members[i];
			
		return new set(ans);		
	}
	
	// I am cheating here...The way I store my sets, I can't represent the Cartesian Product of two
	// sets as a set.
	public void printCartesianProduct(set other) {
		
		// Go through all possible items in Cartesian Product in lexicographical order, printing
		// only the ones in the desired Cartesian Product.
		for (int i=0; i<MAX+1; i++)
			for (int j=0; j<MAX+1; j++)
				if (members[i] && other.members[j])
					System.out.print("("+i+","+j+") ");
		System.out.println();
	}	
	
	public static void main(String[] args) {
		
		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();
		
		// Go through each case.
		for (int loop=1; loop<=numCases; loop++) {
			
			// Read first list.
			ArrayList<Integer> list1 = new ArrayList<Integer>();
			int value = stdin.nextInt();
			while (value != ENDLIST) {
				list1.add(value);
				value = stdin.nextInt();
			}
			
			// Read second list.
			ArrayList<Integer> list2 = new ArrayList<Integer>();
			value = stdin.nextInt();
			while (value != ENDLIST) {
				list2.add(value);
				value = stdin.nextInt();
			}
			
			// Create sets.
			set set1 = new set(list1);
			set set2 = new set(list2);
			
			// Output desired results.
			System.out.println("Case "+loop);
			System.out.println(set1.union(set2));
			System.out.println(set1.intersect(set2));
			System.out.println(set1.subtract(set2));
			set1.printCartesianProduct(set2);
		}
	}
}