'Snakes and Ladders' or 'Shap-Ludu' is a game commonly played in Bangladesh. The game is so common that it would be tough to find a person who hasn't played it. But those who haven't played it (unlucky of course!) the rules are as follows.
- There is a 10 x 10 board containing some cells numbered from 1 to 100.
- You start at position 1.
- Each time you throw a perfect dice containing numbers 1 to 6.
- There are some snakes and some ladders on the board. Ladders will take you up from one cell to another. Snakes will take you down.
- If you reach a cell that contains the bottom part of a ladder, you will immediately move to the cell which contains the upper side of that ladder. Similarly, if you reach a cell that has a snake-head you immediately go down to the cell where the tail of that snake ends.
- The board is designed so that from any cell you can jump at most once. (For example, there is a snake from 62 to 19, assume that another is from 19 to 2. So, if you reach 62, you will first jump to 19, you will jump to 2. These kinds of cases will not be given.)
- There is no snake head in the 100-th cell and no ladder (bottom part) in the first cell.
- If you reach cell 100, the game ends. But if you have to go outside the board in any time your move will be lost. That means you will not take that move and you have to throw the dice again.
Now given a board, you have to find the expected number of times you need to throw the dice to win the game. The cases will be given such that a result will be found.
Input
Input starts with an integer T (≤ 105), denoting the number of test cases.
The first line of a case is a blank line. The next line gives you an integer n denoting the number of snakes and ladders. Each of the next n lines contain two integers a and b (1 ≤ a, b ≤ 100, a ≠ b). If a < b, it means that there is a ladder which takes you from a to b. If a > b, it means that there is a snake which takes you from a to b. Assume that the given board follows the above restrictions.
Output
For each case of input, print the case number and the expected number of times you need to throw the dice. Errors less than 10-6 will be ignored.
Sample
Sample Input | Sample Output |
---|---|
2 14 4 42 9 30 16 8 14 77 32 12 37 58 47 26 48 73 62 19 70 89 71 67 80 98 87 24 96 76 0 | Case 1: 31.54880806 Case 2: 33.0476190476 |