Sherlock and Watson have recently enrolled in a computer programming course. Today, the tutor taught them about the balanced parentheses problem. A string `S`

consisting only of characters `(`

and/or `)`

is *balanced* if:

- It is the empty string, or:
- It has the form
`(`

S`)`

, where S is a balanced string, or: - It has the form S
_{1}S_{2}, where S_{1}is a balanced string and S_{2}is a balanced string.

Sherlock coded up the solution very quickly and started bragging about how good he is, so Watson gave him a problem to test his knowledge. He asked Sherlock to generate a string S of

`(`

and a total of `)`

. Moreover, the string must have as many different balanced non-empty substrings as possible. (Two substrings are considered different as long as they start and end at different indexes of the string, even if their content happens to be the same). Note that S itself does not have to be balanced.Sherlock is sure that once he knows the maximum possible number of balanced non-empty substrings, he will be able to solve the problem. Can you help him find that maximum number?

The first line of the input gives the number of test cases, **T**. **T** test cases follow. Each test case consists of one line with two integers: **L** and **R**.

For each test case, output one line containing `Case #x: y`

, where `x`

is the test case number (starting from 1) and `y`

is the answer, as described above.

1 ≤ **T** ≤ 100.

Time limit: 20 seconds per test set.

Memory limit: 1GB.

0 ≤ **L** ≤ 20.

0 ≤ **R** ≤ 20.

1 ≤ **L + R** ≤ 20.

0 ≤ **L** ≤ 10^{5}.

0 ≤ **R** ≤ 10^{5}.

1 ≤ **L + R** ≤ 10^{5}.

Sample Input

3 1 0 1 1 3 2

Sample Output

Case #1: 0 Case #2: 1 Case #3: 3

`(`

. There are no balanced non-empty substrings.In Case 2, the optimal string is

`()`

. There is only one balanced non-empty substring: the entire string itself.In Case 3, both strings

`()()(`

and `(()()`

give the same optimal answer.For the case

`()()(`

, for example, the three balanced substrings are `()`

from indexes 1 to 2, `()`

from indexes 3 to 4, and `()()`

from indexes 1 to 4.
Watson and Sherlock are gym buddies.

Their gym trainer has given them three numbers, **A**, **B**, and **N**, and has asked Watson and Sherlock to pick two different **positive integers** i and j, where i and j are both less than or equal to **N**. Watson is expected to eat exactly i^{A} sprouts every day, and Sherlock is expected to eat exactly j^{B} sprouts every day.

Watson and Sherlock have noticed that if the total number of sprouts eaten by them on a given day is divisible by a certain integer **K**, then they get along well that day.

So, Watson and Sherlock need your help to determine how many such pairs of **(i, j)** exist, where i != j. As the number of pairs can be really high, please output it modulo **10 ^{9}+7 (1000000007)**.

The first line of the input gives the number of test cases, **T**. **T** test cases follow. Each test case consists of one line with 4 integers **A**, **B**, **N** and **K**, as described above.

For each test case, output one line containing `Case #x: y`

, where `x`

is the test case number (starting from 1) and `y`

is the required answer.

1 ≤ **T** ≤ 100.

Time limit: 60 seconds per test set.

Memory limit: 1GB.

0 ≤ **A** ≤ 10^{6}.

0 ≤ **B** ≤ 10^{6}.

1 ≤ **K** ≤ 10000.

1 ≤ **N** ≤ 1000.

1 ≤ **K** ≤ 100000.

1 ≤ **N** ≤ 10^{18}.

Sample Input

3 1 1 5 3 1 2 4 5 1 1 2 2

Sample Output

Case #1: 8 Case #2: 3 Case #3: 0

Sherlock and Watson have mastered the intricacies of the language C++ in their programming course, so they have moved on to algorithmic problems. In today's class, the tutor introduced the problem of merging one-dimensional intervals. **N** intervals are given, and the `i`

th interval is defined by the inclusive endpoints [**L _{i}, R_{i}**], where

The tutor defined the

`p`

≤ Now, Watson always likes to challenge Sherlock. He has asked Sherlock to remove exactly one interval such that the covered area of the remaining intervals is minimized. Help Sherlock find this minimum possible covered area, after removing exactly one of the

Each test case consists of one line with eight integers **N**, **L _{1}, R_{1}**,

First define

`x`_{1}

= `y`_{1}

= `x`_{i}, y_{i}

for `i`

= 2 to `x`

= (_{i}**A***`x`

+_{i-1}**B***`y`

+_{i-1}**C**) modulo_{1}**M**.-
`y`

= (_{i}**A***`y`

+_{i-1}**B***`x`

+_{i-1}**C**) modulo_{2}**M**.

`min(x`_{i}, y_{i})

and `max(x`_{i}, y_{i})

, for all `i`

= 2 to
For each test case, output one line containing `Case #x: y`

, where `x`

is the test case number (starting from 1) and `y`

is the minimum possible covered area of all of the intervals remaining after removing exactly one interval.

1 ≤ **T** ≤ 50.

Memory limit: 1GB.

0 ≤ **L _{1}** ≤

0 ≤

0 ≤

0 ≤

0 ≤

1 ≤

Time limit: 30 seconds.

1 ≤ **N** ≤ 1000.

Time limit: 200 seconds.

1 ≤ **N** ≤ 5 * 10^{5}(500000).

Sample Input

3 1 1 1 1 1 1 1 1 3 2 5 1 2 3 4 10 4 3 4 3 3 8 10 10

Sample Output

Case #1: 0 Case #2: 4 Case #3: 9

In case 2, using the generation method, the set of intervals generated are: {[2, 5], [3, 5], [4, 7]}. Removing the first, second or third interval would cause the covered area of remaining intervals to be 5, 6 and 4, respectively.

In case 3, using the generation method, the set of intervals generated are: {[3, 4], [1, 9], [0, 8], [2, 4]}. Removing the first, second, third or fourth interval would cause the covered area of remaining intervals to be 10, 9, 9 and 10, respectively.

Sherlock and Watson have already been introduced to sorting in their computer programming course. Now, Watson has always been curious about parallel computing and wants to sort a permutation of the integers 1 through **N** by breaking it into chunks, sorting the chunks individually, and then concatenating them.

For a permutation `p`

, a chunk is a contiguous subarray of the permutation: i.e., a sequence of elements _{1}, p_{2}, ..., p_{N}`p`

, for the elements at indexes i and j such that 1 ≤ _{i}, p_{i + 1}, ..., p_{j}`i`

≤ `j`

≤ **N**.

Watson wants to partition his permutation into an ordered list of one or more chunks, without changing the order that the elements are in, in such a way that each element of the permutation is in exactly one chunk, and all elements in a chunk are smaller than all elements in any later chunk.

For example, for the permutation [2, 1, 3, 5, 4], these are the only four legal ways for Watson to break it into chunks: [[2, 1, 3], [5, 4]] or [[2, 1], [3, 5, 4]] or [[2, 1], [3], [5, 4]] or [[2, 1, 3, 5, 4]]. Watson is happiest when there are as many chunks as possible; we denote the maximum number of chunks for a permutation p as f(p). In this example, the maximum number of chunks is 3.

Watson wants to consider all permutations p of the numbers 1 through **N**, and find the **sum of squares** of f(p). Watson knows Sherlock might come in handy and comes to him for help, but Sherlock is as clueless as Watson and asks you for help. As the sum of squares can be large, please find it modulo **M**.

The first line of the input gives the number of test cases, **T**. **T** test cases follow. Each test case consists of one line with two integers **N** and **M**.

For each test case, output one line containing `Case #x: y`

, where `x`

is the test case number (starting from 1) and `y`

is the sum of squares of `f(p)`

for all permutations `p`

of size **N**, modulo **M**.

1 ≤ **M** ≤ 10^{9}.

1 ≤ **T** ≤ 100.

Time limit: 20 seconds.

1 ≤ **N** ≤ 100.

1 ≤ **T** ≤ 20.

Time limit: 60 seconds.

1 ≤ **N** ≤ 5000.

Sample Input

3 1 2 2 4 3 7

Sample Output

Case #1: 1 Case #2: 1 Case #3: 6

`f([1]) * f([1]) % 2`

= 1.
In Case 2, there are two permutations.

`f([1, 2])`

= 2.`f([2, 1])`

= 1.`(2`^{2} + 1^{2}) % 4

= 1.In Case 3, there are six permutations.

`f([1, 2, 3])`

= 3.`f([1, 3, 2])`

= 2.`f([2, 1, 3])`

= 2.`f([2, 3, 1])`

= 1.`f([3, 1, 2])`

= 1.`f([3, 2, 1])`

= 1.`(3`^{2} + 2^{2} + 2^{2} + 1^{2} + 1^{2} + 1^{2}) % 7

= 6.Test Data

We recommend that you practice debugging solutions without looking at the test data.

Test Data

We recommend that you practice debugging solutions without looking at the test data.

Test Data

We recommend that you practice debugging solutions without looking at the test data.

Test Data

We recommend that you practice debugging solutions without looking at the test data.