// Arup Guha
// 1/26/05
// Initially written for COP 3530 Spring '05, used for COP 3503 Fall '06

import java.io.*;
import java.util.*;

public class TwoColors {

  final static int NOCOLOR = 0;
  final static int WHITE = 1;
  final static int BLACK = 2;

  public static void main(String[] args) throws IOException {

    int numcases;
	BufferedReader sa = new BufferedReader(new InputStreamReader(System.in));
	System.out.print("Enter name of input file : ");
	String fileName = sa.readLine();
	
    Scanner sc = new Scanner(new File(fileName));
    
    numcases = sc.nextInt();

    // Loop through each input case, one by one.
    for (int i=1; i<=numcases; i++) {

      // Read in the number of vertices for this graph.
      int numv = sc.nextInt();
      int[][] adjmat = new int[numv][];
      int[] colors = new int[numv];
      init(colors);
	  sc.nextLine();	
	  
      // Read in the adjacent vertices for each vertex.
      for (int j=0; j<numv; j++) {
      	
		String next = sc.nextLine();
	
        StringTokenizer tok = new StringTokenizer(next);
 
        // Read in the number of vertices adjacent to vertex j.
        
        int numadj = tok.countTokens();
        adjmat[j] = new int[numadj];

        for (int k=0; k<numadj; k++)
          adjmat[j][k] = Integer.parseInt(tok.nextToken())-1;

      }  // end j loop

      // Test this graph and output the answer.
      if (canColor(adjmat, colors, 0))
        System.out.println("Graph #"+i+" can be colored with two colors.");
      else
        System.out.println("Graph #"+i+" is NOT two-colorable.");
     
    } // end i loop
    
    sc.close();

  } // end main


  public static void init(int[] colors) {

    colors[0] = WHITE;
    for (int i=1; i<colors.length; i++)
      colors[i] = NOCOLOR;
  }

  public static boolean canColor(int[][] adjmat, int[] colors, int v) {
 
    int[] queue = new int[adjmat.length];

    int size=0, count=0;
    queue[size++] = v;

    while (!assigned(colors) && count < size) {

      // Color all vertices adjacent to v.
      for (int i=0; i<adjmat[v].length; i++) {
   
        if (colors[adjmat[v][i]] == NOCOLOR) {

          if (consistent(adjmat, colors, opposite(colors[v]), adjmat[v][i])) {
            colors[adjmat[v][i]] = opposite(colors[v]);
            queue[size++] = adjmat[v][i];
          }
          else 
            return false;        
        }

        // Impossible to two color since we've hit a conflict.
        else if (colors[adjmat[v][i]] == colors[v]) 
          return false;
      
      } // end i

      // Adjust queue stuff.
      count++;
      v = queue[count];
    
    } // end while

    return true;

  } // end canColor

  public static boolean assigned(int[] colors) {

    for (int i=0; i<colors.length; i++)
      if (colors[i] == NOCOLOR)
        return false;
    return true;
 
  } // end assigned

  public static int opposite(int color) {
  
    if (color == BLACK)
      return WHITE;
    return BLACK;
  } // end opposite


  public static boolean consistent(int[][] adjmat, int[] colors,
                                   int color , int ver) {

    for (int i=0; i<adjmat[ver].length; i++)
      if (colors[adjmat[ver][i]] == color)
        return false;
    return true;

  }

  public static void print(int[] colors) {
    for (int i=0; i<colors.length; i++)
      System.out.print(colors[i]+" ");
    System.out.println();
  }

} // end class
