Algorithm


problem link- https://www.spoj.com/problems/MOHIB/

MOHIB - Mohib and series

no tags 

 

Mohib (CoolWink), as we all known, is very brilliant in mathematics. His friend Bar has given him another problem to solve.

Bar gives him a sequence of distinct positive integers, whose average is (A+1).

If Bar append an integer x to the sequence, then the average of integers in the sequence will be A.

He asked the Mohib to tell him the largest possible integer in the sequence.

Input

First line contains T, the number of testcases (T <= 5000). 

Next T lines contain an integer x (1 <= x <= 49999) and A (x+1 <= A <= 50000).

Output

Print the largest possible integer in the sequence.

Example

Input:
1
1 2

Output:
3

 

Code Examples

#1 Code Example with Python Programming

Code - Python Programming

T = int(input())
for i in range(T):
    x, a = map(int, input().split())
    delta = a * a - x * x - 3 * x + 3 * a
    print (delta >> 1)
Copy The Code & Try With Live Editor

Input

x
+
cmd
1
1 2

Output

x
+
cmd
3
Advertisements

Demonstration


SPOJ Solution-Mohib and series-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python