Under construction

Still being built. Things may change

Shifting Paths (Large)

Time limit30sMemory limit512 MB

Summary
Count how many paths the alternating left-right walk takes from clearing 1 to clearing N, or report Infinity if it never arrives.
Level

Medium4 of 10

Topics
Simulation, Graph
Solved
No attempts yet

Problem

You have been walking in the woods for hours, and now you want to go home.

The woods contain NN clearings numbered 1,2,…,N1, 2, \dots, N. You are standing in clearing 1, and you must reach clearing NN to leave the woods. Each clearing from 1 to N−1N-1 has one left path and one right path leading out to other clearings, and it may also have several one way paths leading in. The woods are haunted, so every time you enter a clearing, shifting trees block one of its two outgoing paths. On your kk-th visit to a single clearing the rule is this.

  • You must leave along the left path if kk is odd.
  • You must leave along the right path if kk is even.
  • Every path is one way, so you have no choice at any step. You must go forward through the one path that is not blocked.

So the first time you stand in clearing 1 you leave along its left path. If you come back to clearing 1 a second time you leave along its right path, and on the third visit you leave along the left path again.

You begin in clearing 1, and you leave the woods once you get to clearing NN. How many paths do you follow before you get out?

Input

The first line contains the number of test cases TT. TT test cases follow, each beginning with a line that contains one integer NN.

The next N−1N-1 lines each contain two integers LiL_i and RiR_i. LiL_i is the clearing you end up at if you follow the left path out of clearing ii, and RiR_i is the clearing you end up at if you follow the right path out of clearing ii.

No paths are given for clearing NN, because you are finished once you get there.

Output

For each test case, print one line containing "Case #x: y", where xx is the test case number starting from 1 and yy is the number of paths you follow to get to clearing NN. If you will never get to clearing NN, print "Infinity" in place of yy.

Constraints

  • 1≤T≤301 \le T \le 30
  • 2≤N≤402 \le N \le 40
  • 1≤Li,Ri≤N1 \le L_i, R_i \le N for every ii
  • If you can leave the woods, the number of paths you follow is at most 10510^5.

Hint

In the first test case of the first example input, your route through the woods is the one below.

Paths followedClearingPath taken
01left
12left
23left
32right
41right
51left
62left
73right
84you leave the woods

Examples2

  1. Example 1

    Input
    2
    4
    2 1
    3 1
    2 4
    3
    2 2
    1 2
    
    Expected output
    Case #1: 8
    Case #2: Infinity
    
  2. Example 2

    Input
    4
    2
    1 2
    2
    2 1
    2
    2 2
    2
    1 1
    
    Expected output
    Case #1: 2
    Case #2: 1
    Case #3: 1
    Case #4: Infinity