import java.io.*;

import java.util.*;
 
 
public class Main{
	public static final int mod = 1000000007;
	public static HashMap<String,Integer> map ;
	public static int count ;
	public static ArrayList<ArrayList<Integer>> adj;
	public static boolean visited[],recstack[];
	public static void main(String[] args)throws IOException {
		// TODO Auto-generated method stub
		InputStream input = System.in;
		InputReader in = new InputReader(input);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		//File file = new File("C:\\Users\\SAWANT\\Downloads\\C-small-attempt0 (1).in");
	  
	   // BufferedReader br = new BufferedReader(new FileReader(file));
		int t = Integer.parseInt(br.readLine());
		
		for(int o=1; o<=t; o++)
		{
			int n = Integer.parseInt(br.readLine());
			
			map = new HashMap<String,Integer>();
			adj = new ArrayList<ArrayList<Integer>>();  //basically a 2-D array for storing adj.
			int c = 1; // dummy node count
			adj.add(new ArrayList<Integer>()); // dummy node
			count = 0;  //visited node count in dfs
			for(int i=1; i<=n; i++)
			{
				String s = br.readLine();
				
				String var = s.substring(0, s.indexOf('='));
				int varindex = -1;
				if(map.containsKey(var))
					{
					varindex = map.get(var);
					
					}
				else
				{
					varindex= c++;
					
					map.put(var,varindex);
				
					adj.add(new ArrayList<Integer>());
				}
			String ss = s.substring(s.indexOf('(') +1,s.indexOf(')'));
			
			if(ss.equals(""))
			{
				adj.get(0).add(varindex);continue;
			}
				
			StringTokenizer	st = new StringTokenizer(ss,",");
				
			while(st.hasMoreTokens())
			{
				String next = st.nextToken();int argindex = -1;
				
				if(!map.containsKey(next))
				{	
					argindex  = c++;
					map.put(next,argindex);
				
					adj.add(new ArrayList<Integer>());
				}
				else
				{
					argindex = map.get(next);
				}
				
				adj.get(argindex).add(varindex);
			
			}
			
			}
			
			visited = new boolean[adj.size()];
			recstack = new boolean[adj.size()];
			boolean cycle = false;
		
			
				if(cycle(0))
					{
					cycle = true;
					
					}
				
			
			
			
			if(cycle || count != visited.length)
				System.out.println("Case #"+o+": BAD");
			else
				System.out.println("Case #"+o+": GOOD");
		}
		
		
		
		
	
	
	
	
	
	
	
	 }
	static boolean cycle(int i)
	{	
		if(visited[i] == false)
		{	count++;
			  visited[i] = true;
		       recstack[i] = true;
		       
		       int s = adj.get(i).size();
		      ArrayList<Integer> aa = adj.get(i);
		      for(int ii=0; ii<s;ii++)
		        {
		           int next = aa.get(ii);
		           
		           if(!visited[next] && cycle(next))
		        	   return true;
		           else if(recstack[next])
		        	   return true;
		        }
		 
		    }
		    recstack[i] = false;  
		    return false;
			
			
			
		
		
		
		
	}
	
	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
 
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
 
        public String next() {
            while  (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
 
        public int nextInt() {
            return Integer.parseInt(next());
        }
 
    }
}   