// Arup Guha
// 3/1/2016
// Solution to 2016 Mercer Programming Contest Problem 6: FSM2

import java.util.*;

public class prob6 {

	public static int MAXSTATES = 257;
	public static int NUMLETTERS = 26;

	public static ArrayList[][] table;
	public static boolean[] accept;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numMoves = Integer.parseInt(stdin.nextLine().trim());
		int loop = 1;

		// Process each case.
		while (numMoves != 0) {

			// Allocate enough space for the transition table - it's overkill, but I'm being lazy!
			accept = new boolean[MAXSTATES];
			table = new ArrayList[MAXSTATES][NUMLETTERS];
			for (int i=0; i<MAXSTATES; i++)
				for (int j=0; j<NUMLETTERS; j++)
					table[i][j] = new ArrayList<Integer>();

			// Add in each move to the appropriate table.
			for (int i=0; i<numMoves; i++) {
				StringTokenizer tok = new StringTokenizer(stdin.nextLine());
				int s1 = Integer.parseInt(tok.nextToken())-1;
				int ch = (int)(tok.nextToken().charAt(0) - 'a');
				int s2 = Integer.parseInt(tok.nextToken())-1;
				table[s1][ch].add(s2);
			}

			// Set all accept states.
			StringTokenizer tok = new StringTokenizer(stdin.nextLine());
			while (tok.hasMoreTokens())
				accept[Integer.parseInt(tok.nextToken())-1] = true;

			// Print header.
			System.out.printf("FSM2 #%d\n", loop);

			// Get the number of strings to test.
			int numStrings = Integer.parseInt(stdin.nextLine());
			for (int i=0; i<numStrings; i++) {

				// Get the next test word.
				String word = stdin.nextLine().trim();

				// Test and output the result.
				if (accepted(word))
					System.out.print("\""+word+"\""+" ACCEPTED\n");
				else
					System.out.print("\""+word+"\""+" NOT ACCEPTED\n");
			}

			// Get next case.
			numMoves = Integer.parseInt(stdin.nextLine().trim());
			loop++;
		}

		// This is dumb.
		System.out.printf("END\n");
	}

	public static boolean accepted(String word) {

		// Stores which branch of the move we'll make. We default to all 0s (first move).
		int[][] curMove = new int[MAXSTATES][NUMLETTERS];

		// We start here...
		int curState = 0;

		// Go through each letter.
		for (int i=0; i<word.length(); i++) {

			int ch = (int)(word.charAt(i) - 'a');

			// Which transition we'll take from here.
			int whichMove = curMove[curState][ch];

			// We need to know where we were...
			int saveState = curState;

			//System.out.println("state is "+curState);

			// Just in case a transition isn't defined.
			if (table[saveState][ch].size() == 0) return false;

			// Make the next move.
			curState = ((ArrayList<Integer>)table[curState][ch]).get(whichMove);

			// Need to rotate this so that our next move from this position adjusts accordingly.
			curMove[saveState][ch] = (curMove[saveState][ch] + 1)%table[saveState][ch].size();
		}

		// This tells us whether or not we're accepted.
		return accept[curState];
	}
}