#P1872D. Plus Minus Permutation
Plus Minus Permutation
Description
You are given integers — , , . Let's call the score of a permutation the following value:
In other words, the score of a permutation is the sum of for all indices divisible by , minus the sum of for all indices divisible by .
You need to find the maximum possible score among all permutations of length .
For example, if , , , the maximum score is achieved by the permutation and is equal to .
A permutation of length is an array consisting of distinct integers from to in any order. For example, is a permutation, but is not a permutation (the number appears twice in the array) and is also not a permutation (, but the array contains ).
The first line of input contains an integer () — the number of test cases.
Then follows the description of each test case.
The only line of each test case description contains integers , , (, ).
For each test case, output a single integer — the maximum score among all permutations of length .
Input
The first line of input contains an integer () — the number of test cases.
Then follows the description of each test case.
The only line of each test case description contains integers , , (, ).
Output
For each test case, output a single integer — the maximum score among all permutations of length .
Note
The first test case is explained in the problem statement above.
In the second test case, one of the optimal permutations will be . The score of this permutation is . It can be shown that a score greater than can not be achieved. Note that the answer to the problem can be negative.
In the third test case, the score of the permutation will be . One of the optimal permutations for this case is , and its score is . It can be shown that a score greater than can not be achieved.
In the fourth test case, , so the score of any permutation will be .