// Arup Guha
// 7/19/2013
// Solution to 2010 UCF High School Contest Problem: Choose Your Own Adventure
import java.util.*;

public class adventure {

	public static int n;
	public static int[] memo;
	public static ArrayList[] eList;
	final public static int UNFILLED = -1;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = Integer.parseInt(stdin.nextLine());

		// Go through each case.
		for (int loop=1; loop<=numCases; loop++) {

			// Set up key variables for this case.
			n = Integer.parseInt(stdin.nextLine());
			memo = new int[n];
			Arrays.fill(memo, UNFILLED);
			eList = new ArrayList[n];
			for (int i=0; i<n; i++)
				eList[i] = new ArrayList<Integer>();

			// Read in each pages's next items.
			for (int i=0; i<n; i++) {

				StringTokenizer tok = new StringTokenizer(stdin.nextLine());
				String first = tok.nextToken();

				// Add neighbors if necessary.
				if (!first.equals("ENDING")) {
					eList[i].add(Integer.parseInt(first)-1);
					while (tok.hasMoreTokens())
						eList[i].add(Integer.parseInt(tok.nextToken())-1);
				}
			}

			// Output answer.
			System.out.println("Book #"+loop+": The longest story is "+ solve(0) +" pages.");
		}
	}

	public static int solve(int page) {

		// Base cases.
		if (eList[page].size() == 0) return 1;
		if (memo[page] != UNFILLED) return memo[page];

		// Try each route and pick the longest.
		int best = 1;
		for (int i=0; i<eList[page].size(); i++)
			best = Math.max(best, 1+solve((Integer)eList[page].get(i)));

		// Store in memo table.
		memo[page] = best;

		// Here is our answer.
		return best;
	}
}