MOHIB - Mohib and series

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

Added by:MoHib85
Date:2015-05-17
Time limit:0.100s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ADA95 ASM32-GCC ASM32 ASM64 GAWK BASH JS-MONKEY

hide comments
2019-01-08 13:10:42
remember that sequence has distinct elements
2018-06-09 16:13:35 D Pratap
got TLE for O(1) solution in Java .
And got accepted for c/c++ solution
2017-06-06 19:58:11
just take take that 0 is neither positive nor negative
2017-01-21 07:07:47
distinct number sequence starts from 1 not zero cost me 2 wa..but ques is simple...
2017-01-06 07:18:18
Just use of pen and paper....
It must be given that sequence consists of positive integer numbers rather only +ve numbers...

Last edit: 2017-01-06 07:18:38
2016-07-02 13:57:33 Mostafa 36a2
Please edit the problem description: all numbers are integers.
2016-06-26 08:27:09 Akshat Jain
its a pen paper thing....more of maths less of coding!
2016-06-21 12:00:34 Admin Deepak Baghel
@author
how can time complexity be optimized current complexity is O(1) solution id: 17149843
2016-05-05 19:12:29
Quite easy but rejected twice only because for numbers beyond limit,i was printing nothing but a garbage should be printed btw very very pow(10,100) easy question.
2016-03-02 10:33:49 minhthai
@Kalita: big save there :) thanks
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.