// Arup Guha
// 7/12/2014
// Solution to 2006 UCF HS Contest Problem: Lice-N-Plates...Are Back!

import java.util.*;
import java.io.*;

public class lice {

	public static void main(String[] args) throws Exception {

		Scanner stdin = new Scanner(new File("lice.in"));
		int numCases = stdin.nextInt();

		// Go through each set of registrations.
		for (int loop=1; loop<=numCases; loop++) {

			// Read in current plates.
			int n = stdin.nextInt();
			ArrayList<String> plates = new ArrayList<String>();
			for (int i=0; i<n; i++)
				plates.add(stdin.next());

			// Header
			System.out.println("Registration "+loop+":");

			// Now process the extra applications.
			n = stdin.nextInt();
			for (int i=0; i<n; i++) {

				// No match, so add.
				String next = stdin.next();
				if (!match(plates, next)) {
					plates.add(next);
					System.out.println(next+" is registered.");
				}

				// Too close.
				else {
					System.out.println(next+" is NOT registered.");
				}
			}

			// Between cases.
			System.out.println();
		}

		stdin.close();
	}

	// Returns true iff next is <= 1 edit distance from a string in list.
	public static boolean match(ArrayList<String> list, String next) {

		// Try all matches for each string - an add is a del in the opposite direction.
		for (int i=0; i<list.size(); i++) {
			if (sub(list.get(i), next)) return true;
			if (del(list.get(i), next)) return true;
			if (del(next, list.get(i))) return true;
		}

		// Never found a match.
		return false;
	}

	// Returns true iff we can obtain b from a with at most 1 substitution.
	public static boolean sub(String a, String b) {

		// Necessary condition.
		if (a.length() != b.length()) return false;

		// Just count differences.
		int diff = 0;
		for (int i=0; i<a.length(); i++) {
			if (a.charAt(i) != b.charAt(i))
				diff++;
		}
		return diff < 2;
	}

	// Returns true iff deleting a character from a results in b.
	public static boolean del(String a, String b) {

		// Necessary condition.
		if (a.length()-1 != b.length()) return false;

		// Slow but good enough for small data.
		for (int i=0; i<a.length(); i++) {
			String tmp = a.substring(0,i) + a.substring(i+1);
			if (tmp.equals(b)) return true;
		}

		// No match found.
		return false;
	}
}