// Arup Guha
// 1/26/05
// Solution to COP 3530 Homework #1 Programming Problem

import java.io.*;
import java.util.StringTokenizer;

public class TwoColors {

  // Constants for the status of a node while trying to color.
  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 fin = new BufferedReader(new FileReader("graphs.in"));
    numcases = Integer.parseInt(fin.readLine().trim());

    // 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 = Integer.parseInt(fin.readLine().trim());
      int[][] adjmat = new int[numv][];

      int[] colors = new int[numv];
      init(colors);

      // Read in the adjacent vertices for each vertex.
      for (int j=0; j<numv; j++) {

        StringTokenizer tok = new StringTokenizer(fin.readLine());
 
        // 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

  } // end main


  public static void init(int[] colors) {

    colors[0] = WHITE; // Color this node since we start searching here.

    // None of the rest have colors assigned yet.
    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;

    // Continue until all nodes have colors, or the queue is full.
    while (!assigned(colors) && count < size) {

      // Color all vertices adjacent to v.
      for (int i=0; i<adjmat[v].length; i++) {
   
        // If this node hasn't been colored yet, check it out.
        if (colors[adjmat[v][i]] == NOCOLOR) {

          // Check if we can color it and keep consistency with other
          // colored nodes.
          if (consistent(adjmat, colors, opposite(colors[v]), adjmat[v][i])) {
            colors[adjmat[v][i]] = opposite(colors[v]);
            queue[size++] = adjmat[v][i];
          }

          // If not, return false indicating we can't color!
          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

  // Returns true if all nodes are assigned a color, false otherwise.
  public static boolean assigned(int[] colors) {

    for (int i=0; i<colors.length; i++)
      if (colors[i] == NOCOLOR)
        return false;
    return true;
 
  } // end assigned

  // Returns the opposite color of color.
  public static int opposite(int color) {
  
    if (color == BLACK)
      return WHITE;
    return BLACK;
  } // end opposite

  // Checks if coloring vertex ver the color color is consistent with the
  // rest of the colorings.
  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
