package nai1;

import java.io.*;
import java.util.ArrayList;
import nai1.graph.*;

/**
 * @author Daniel "3ICE" Berezvai
 */
public class Nai1{
  static File in=new File("src/i");
  static File out=new File("src/o.txt");
  static BufferedReader r;
  static PrintWriter w;
  static ArrayList<Node> nodes=new ArrayList<>();
  static ArrayList<Edge> edges=new ArrayList<>();
  static int white=0;
  static int gray=1;
  static int black=2;
  static int numDepth, numFinish;

  private static boolean MB(Node u){
    u.setColor(gray);
    numDepth++;
    if(null!=u.getParent()){
      for(Node v : u.getParent().getChildren()){
        if(v.getColor()==white){
          v.setP(u);
          MB(v);
        }else{
          /** 3ICE: This is a bug that I didn't have time to track down. Input 1
           * (3 easy graphs) needs the next line commented out to work properly,
           * while inputs 2 and 3 (8 medium and 13 hard graphs respectively)
           * need it uncommented to work.
           * @WONTFIX */
          //return false;
        }
      }
    }
    u.setColor(black);
    numFinish++;
    return true;
  }

  private static boolean Depth(){
    for(Node u : nodes){
      u.setColor(white);
      u.setP(null);
    }
    numDepth=0;
    numFinish=0;
    for(Node u : nodes){
      if(u.getColor()==white){
        return MB(u);
      }else{
        return false;
      }
    }
    return true;
  }

  public static Node findorCreateNode(String name){
    for(Node node : nodes){
      if(node.getName().equals(name)){
        //System.err.println("Found node "+name);
        return node;
      }
    }
    //System.err.println("Creating node "+name);
    Node node=new Node(name);
    nodes.add(node);
    return node;
  }

  private static boolean OnlyOneRoot(){
    int count=0;
    for(Node node : nodes){
      if(node.getParent()==null){
        count++;
      }
    }
    if(count!=1){
      return false;
    }else{
      return true;
    }
  }

  @SuppressWarnings({"null", "ConstantConditions"})
  public static boolean processOne(int k, String line){
    boolean odd=false;//even
    Node one=null, two;
    for(String name : line.split("\\s+")){
      if(odd){
        two=findorCreateNode(name);
        edges.add(new Edge(one, two));
        /** I am confident it won't break. Hence the:
         * <code>@SuppressWarnings({"null", "ConstantConditions"})</code>
         * above... */
        one.addChild(two);
        if(!two.setParent(one)){
          nodes.clear();
          edges.clear();
          //System.err.println("nem fa!");
          return false;
        }
      }else{
        one=findorCreateNode(name);
      }
      odd=!odd;
    }
    boolean ret;
    if(OnlyOneRoot()){
      ret=Depth();
    }else{
      ret=false;
    }


    //System.err.println("nodes = "+nodes);
    //System.err.println("edges = "+edges);
    nodes.clear();
    edges.clear();
    return ret;
  }

  public static void main(String[] args) throws IOException{
    r=new BufferedReader(new InputStreamReader(
            new FileInputStream(in)));
    w=new PrintWriter(out);
    int n=Integer.parseInt(r.readLine());
    for(int i=0;i<n;i++){
      if(processOne(Integer.parseInt(r.readLine()), r.readLine())){
        System.err.println(1+i+" fa");
        w.println(1+i+" fa");
      }else{
        System.err.println(1+i+" nem fa");
        w.println(1+i+" nem fa");
      }
    }
    w.flush();
  }
}
