// Arup Guha
// 3/26/2014
// Solution to COT 3100 Optional Program #5: TV Networks and Actors

import java.util.*;

public class tv {
	
	public static void main(String[] args) {
		
		Scanner stdin = new Scanner(System.in);
		
		int numCases = Integer.parseInt(stdin.nextLine().trim());
		
		// Go through each input case.
		for (int loop=0; loop<numCases; loop++) {
			
			// Get # of networks and shows.
			StringTokenizer tok = new StringTokenizer(stdin.nextLine());
			int numNetworks = Integer.parseInt(tok.nextToken());
			int numShows = Integer.parseInt(tok.nextToken());
		
			// Will store which network hosts each show and which subset of networks each actor
			// gets paid from.
			HashMap<String,Integer> showList = new HashMap<String,Integer>();
			HashMap<String,Integer> actorList = new HashMap<String,Integer>();
			
			// All networks go here.
			networks networkList = new networks(numNetworks);
			
			// Go through each network.
			for (int i=0; i<numNetworks; i++) {
				
				tok = new StringTokenizer(stdin.nextLine(),",");
				
				// Get this network name
				int ID = networkList.add(tok.nextToken());
				
				// Go through each show, adding this network as where it's shown.
				while (tok.hasMoreTokens()) 
					showList.put(tok.nextToken(), ID);
			}
			
			// Go through each show, parsing out the actors and adding networks as necessary.
			for (int i=0; i<numShows; i++) {
				
				tok = new StringTokenizer(stdin.nextLine(),",");
				
				// Get the ID of the network with this show.
				int ID = showList.get(tok.nextToken());
				
				// Go through each actor.
				while (tok.hasMoreTokens()) {
					
					String actor = tok.nextToken();
					
					// Seen this person before add network ID to their list.
					if (actorList.containsKey(actor)) {
						int newMask = actorList.get(actor) | (1 << ID);
						actorList.put(actor, newMask);
					}
					
					// New actor, place just this network on his list.
					else {
						actorList.put(actor,(1 << ID));
					}
				}
			}
			
			// Get a sorted list of all actors.
			ArrayList<String> sortedActors = new ArrayList<String>(actorList.keySet());
			Collections.sort(sortedActors);

			// Print out infomration.
			for (int i=0; i<sortedActors.size(); i++)
				System.out.println(sortedActors.get(i)+": "+networkList.getMask(actorList.get(sortedActors.get(i))));
		}
	}
}

class networks {
	
	private String[] names;
	private HashMap<String,Integer> map;
	private int curSize;
	
	// Create a networks object with a capacity of size networks.
	public networks(int size) {
		names = new String[size];
		map = new HashMap<String,Integer>();
		curSize = 0;
	}
	
	// Add channel as the next network.
	public int add(String channel) {
		names[curSize] = channel;
		map.put(channel, curSize);
		int retVal = curSize;
		curSize++;
		return retVal;
	}
	
	// Returns a string representation as requested by the assignment of the subset of networks
	// designated by mask, so long as mask < (1 << curSize).
	public String getMask(int mask) {
		
		ArrayList<String> list = new ArrayList<String>();
		
		// Go through each item.
		for (int i=0; i<curSize; i++) {
			
			// Only add to list if this item is selected.
			if ((mask & (1 << i)) > 0)
				list.add(names[i]);
		}
		
		// We want alpha order.
		Collections.sort(list);
		
		// Build list with commas.
		String ans = "";
		for (int i=0; i<list.size()-1; i++)
			ans = ans + list.get(i)+",";
		ans = ans + list.get(list.size()-1);
		
		return ans;
	}
}