/* Class: COP3530
 * Date: Feb 24, 2006
 * Instructor: Arup Guha
 * TA: Adam Campbell
 *
 * Recitation #4, Problem #1
 **/

import java.io.*;
import java.util.*;

public class Rec4Prob1{

	// it's not pretty to make this global
	// but we'll do it anyways
	private static int answerIndex;
	private static boolean isCorrectDFS, followThrough;

	/* I have main throw exception so I don't need the try/catch blocks everywhere when reading in from the file.
	 * In general, this is not good programming practice, but for these simple problems we are more interested in the
	 *   algorithm.
	 **/
	public static void main(String[] args) throws Exception{

		BufferedReader userInputReader, fileReader;
		String fileName;
		StringTokenizer tokenizer;
		boolean[][] adj; // the adjacency matrix
		int[] answer; // the answer that the student gave (the one we are checking to determine if it's valid or not)
		boolean[] used; // keeps track of which vertices we have used when checking the answer
		int n, s;

		// initialize the BufferedReader to read from standard input
		userInputReader = new BufferedReader(new InputStreamReader(System.in));

		// prompt the user for the file name, and get the file name from the user
		System.out.print("Please enter the file name: ");
		fileName = userInputReader.readLine();

		// open up the input file
		fileReader = new BufferedReader(new FileReader(fileName));

		// read n, number of vertices
		n = Integer.parseInt(fileReader.readLine());

		adj = new boolean[n][n];
		used = new boolean[n];

		// read in the edges
		for(int fromVert = 0; fromVert < n; fromVert++){

			tokenizer = new StringTokenizer(fileReader.readLine());

			// loop through connections to curVert
			while(tokenizer.hasMoreTokens()){
				// subtract one to zero index the vertices (the input file has them from 1 to n, we want from 0 to n-1)
				int toVert = Integer.parseInt(tokenizer.nextToken()) - 1;
				adj[fromVert][toVert] = true;
			}

		}

		s = Integer.parseInt(fileReader.readLine());

		// loop through all student answers
		while(s-- > 0){

			tokenizer = new StringTokenizer(fileReader.readLine());

			answer = new int[tokenizer.countTokens()];

			for(int i = 0; i < answer.length; i++){
				// subtract one because we zero-index things
				answer[i] = Integer.parseInt(tokenizer.nextToken()) - 1;
			}

			answerIndex = 1;
			Rec4Prob1.isCorrectDFS = false;
			Rec4Prob1.followThrough = true;
			Arrays.fill(used, false);
			used[0] = true;
			Rec4Prob1.checkDFS(adj, answer, used, 0);

			if(Rec4Prob1.isCorrectDFS && Rec4Prob1.followThrough){
				System.out.println("CORRECT!");
			}else{
				System.out.println("SORRY, NOT A VALID SEARCH!");
			}

		}

	}

	/* this function returns true if the given answer is a correct dfs of the graph
	 **/
	private static void checkDFS(boolean[][] adj, int[] answer, boolean[] used, int fromVert){

		// all elements in the array have been checked, so it's a correct DFS
		if(answer.length == Rec4Prob1.answerIndex){
			Rec4Prob1.isCorrectDFS = true;
			return;
		}

		boolean changed = true;
		while(changed){
			changed = false;

			for(int toVert = 0; toVert < adj[fromVert].length && Rec4Prob1.answerIndex < answer.length; toVert++){
				if(!used[toVert] && adj[fromVert][toVert] && answer[Rec4Prob1.answerIndex] == toVert){
					Rec4Prob1.answerIndex++;
					used[toVert] = true;
					changed = true;
					Rec4Prob1.checkDFS(adj, answer, used, toVert);
				}
			}
		}

		// check to make sure that every connection to the current vertex was traversed
		for(int toVert = 0; toVert < adj[fromVert].length; toVert++){
			if(!used[toVert] && adj[fromVert][toVert]){
				Rec4Prob1.followThrough = false;
			}
		}

	}

}
