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[ ] ;
// TODO Auto-generated method stub
InputReader in = new InputReader( input) ;
//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 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 ;
}
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 {
tokenizer = null ;
}
while ( tokenizer == null || ! tokenizer.hasMoreTokens ( ) ) {
try {
}
}
return tokenizer.nextToken ( ) ;
}
public int nextInt( ) {
}
}
}
aW1wb3J0IGphdmEuaW8uKjsKCmltcG9ydCBqYXZhLnV0aWwuKjsKIAogCnB1YmxpYyBjbGFzcyBNYWluewoJcHVibGljIHN0YXRpYyBmaW5hbCBpbnQgbW9kID0gMTAwMDAwMDAwNzsKCXB1YmxpYyBzdGF0aWMgSGFzaE1hcDxTdHJpbmcsSW50ZWdlcj4gbWFwIDsKCXB1YmxpYyBzdGF0aWMgaW50IGNvdW50IDsKCXB1YmxpYyBzdGF0aWMgQXJyYXlMaXN0PEFycmF5TGlzdDxJbnRlZ2VyPj4gYWRqOwoJcHVibGljIHN0YXRpYyBib29sZWFuIHZpc2l0ZWRbXSxyZWNzdGFja1tdOwoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncyl0aHJvd3MgSU9FeGNlcHRpb24gewoJCS8vIFRPRE8gQXV0by1nZW5lcmF0ZWQgbWV0aG9kIHN0dWIKCQlJbnB1dFN0cmVhbSBpbnB1dCA9IFN5c3RlbS5pbjsKCQlJbnB1dFJlYWRlciBpbiA9IG5ldyBJbnB1dFJlYWRlcihpbnB1dCk7CgkJCgkJQnVmZmVyZWRSZWFkZXIgYnIgPSBuZXcgQnVmZmVyZWRSZWFkZXIobmV3IElucHV0U3RyZWFtUmVhZGVyKFN5c3RlbS5pbikpOwoJCS8vRmlsZSBmaWxlID0gbmV3IEZpbGUoIkM6XFxVc2Vyc1xcU0FXQU5UXFxEb3dubG9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kZXIgPSBuZXcgQnVmZmVyZWRSZWFkZXIobmV3IElucHV0U3RyZWFtUmVhZGVyKHN0cmVhbSksIDMyNzY4KTsKICAgICAgICAgICAgdG9rZW5pemVyID0gbnVsbDsKICAgICAgICB9CiAKICAgICAgICBwdWJsaWMgU3RyaW5nIG5leHQoKSB7CiAgICAgICAgICAgIHdoaWxlICAodG9rZW5pemVyID09IG51bGwgfHwgIXRva2VuaXplci5oYXNNb3JlVG9rZW5zKCkpIHsKICAgICAgICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgICAgICAgICAgdG9rZW5pemVyID0gbmV3IFN0cmluZ1Rva2VuaXplcihyZWFkZXIucmVhZExpbmUoKSk7CiAgICAgICAgICAgICAgICB9IGNhdGNoIChJT0V4Y2VwdGlvbiBlKSB7CiAgICAgICAgICAgICAgICAgICAgdGhyb3cgbmV3IFJ1bnRpbWVFeGNlcHRpb24oZSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuIHRva2VuaXplci5uZXh0VG9rZW4oKTsKICAgICAgICB9CiAKICAgICAgICBwdWJsaWMgaW50IG5leHRJbnQoKSB7CiAgICAgICAgICAgIHJldHVybiBJbnRlZ2VyLnBhcnNlSW50KG5leHQoKSk7CiAgICAgICAgfQogCiAgICB9Cn0gICA=