#P1353A. Most Unstable Array
Most Unstable Array
No submission language available for this problem.
Description
You are given two integers and . You have to construct the array of length consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly and the value is the maximum possible. Recall that is the absolute value of .
In other words, you have to maximize the sum of absolute differences between adjacent (consecutive) elements. For example, if the array then the value above for this array is . Note that this example doesn't show the optimal answer but it shows how the required value for some array is calculated.
You have to answer independent test cases.
The first line of the input contains one integer () — the number of test cases. Then test cases follow.
The only line of the test case contains two integers and () — the length of the array and its sum correspondingly.
For each test case, print the answer — the maximum possible value of for the array consisting of non-negative integers with the sum .
Input
The first line of the input contains one integer () — the number of test cases. Then test cases follow.
The only line of the test case contains two integers and () — the length of the array and its sum correspondingly.
Output
For each test case, print the answer — the maximum possible value of for the array consisting of non-negative integers with the sum .
Samples
Note
In the first test case of the example, the only possible array is and the answer is obviously .
In the second test case of the example, one of the possible arrays is and the answer is .
In the third test case of the example, one of the possible arrays is and the answer is .