Mixing Bowls (Large)

Given a recipe where each mixture's ingredients are other mixtures, find the minimum number of bowls needed to prepare it.

Medium7TreeDFSGreedyDynamic programmingNo attempts yetTime limit5sMemory limit512 MB

Problem

You are following a recipe to make your lunch.

The recipe is a mixture made by combining ingredients in a bowl. Each ingredient is one of two things:

  • another mixture, which you must make first in a separate bowl
  • a basic ingredient you already have in your kitchen, which you can add directly

To make a mixture, get all of its ingredients ready, take an empty bowl, and mix the ingredients in it. You cannot make a new mixture by adding ingredients to a bowl that already holds a mixture.

For example, to make the mixture CAKE out of the mixture CAKEMIX and the basic ingredient lies, you must first make CAKEMIX in its own bowl, then add CAKEMIX and lies to a second bowl to make CAKE.

Once you have used a mixture as an ingredient and emptied the bowl it was prepared in, you can reuse that bowl for another mixture. The number of bowls you need to prepare the recipe therefore depends on the order in which you make the mixtures.

Find the minimum number of bowls you need.

Input

The first line contains an integer CC, the number of test cases.

Each test case has this format:

  • one line with an integer NN, the number of mixtures in the test case
  • NN lines, one per mixture, each holding the mixture name, an integer MM giving the number of ingredients of that mixture, and the names of the MM ingredients

Tokens on one line are separated by single spaces.

The first mixture of a test case is the recipe you are making.

Mixture names are strings of 1 to 20 uppercase letters. Basic ingredient names are strings of 1 to 20 lowercase letters.

Every mixture except the recipe is used as an ingredient in exactly one other mixture, and the recipe is used in none. No name appears twice in the ingredient list of one mixture. No mixture requires itself, directly or indirectly.

Limits

  • 1C101 \le C \le 10
  • 1N10001 \le N \le 1000
  • 2M102 \le M \le 10

Output

For each test case, print one line in the form Case #X: Y, where XX is the test case number starting from 1 and YY is the minimum number of mixing bowls required.

Hint

In the first test case of the example input, you can make SOUP like this.

  1. Make VEGETABLES by mixing celery and onions in a bowl.
  2. Make STOCK in a second bowl by mixing chicken and the VEGETABLES from the first bowl. The first bowl becomes empty.
  3. Make SOUP in the now empty first bowl by mixing STOCK, salt and water.

In the second test case you choose whether to make FLAVOR or FRUIT first, before mixing them with milk and icecream to get MILKSHAKE.

Making FRUIT first uses four bowls.

  1. Make FRUIT in a bowl by mixing banana and berries.
  2. Make SPICES in a second bowl by mixing nutmeg and cinnamon, and CHOCOLATE in a third bowl by mixing cocoa and syrup, in either order.
  3. Make FLAVOR in a fourth bowl by mixing SPICES and CHOCOLATE.
  4. Make MILKSHAKE in the now empty second or third bowl by mixing FRUIT, FLAVOR, milk and icecream.

Making FRUIT after FLAVOR uses only three bowls.

  1. Make SPICES in a bowl by mixing nutmeg and cinnamon, and CHOCOLATE in a second bowl by mixing cocoa and syrup, in either order.
  2. Make FLAVOR in a third bowl by mixing SPICES and CHOCOLATE.
  3. Make FRUIT in the now empty first bowl by mixing banana and berries.
  4. Make MILKSHAKE in the now empty second bowl by mixing FRUIT, FLAVOR, milk and icecream.