CF BUDDY
← Problems·

1958B · Clock in the Pool

1400 · *special, math

Problem: You are swimming in the pool, and you need to control the time of swimming.

The pool has a clock that cycles between three different modes: showing water temperature, showing air temperature, and showing time. At the start of the 00-th second it starts showing water temperature, at the start of the kk-th second it switches to air temperature. At the start of the 2k2k-th second, it switches to showing time.

At the start of the 3k3k-th second the clock starts showing water temperature again, at the start of the 4k4k-th second — air temperature, and so on.

You looked at the clock during the mm-th second to check the time, but it may be that the clock is not showing time right now. How much time do you have to wait to see the time on the clock?

Answer tt independent test cases.

Input Format: The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases. Next tt cases follow.

The first and only line of each test case contains two integers kk and mm (1k108;1m1091 \le k \le 10^8; 1 \le m \le 10^9) — the length of the period of the clock and the moment of time you check the clock.

Output Format: For each test case, print a single integer — the time you have to wait from the moment mm until the moment the clock starts showing the time.

You can assume that you are able to read the time instantly the moment it displays on the clock.

Note: In the first test case, the clock will start showing time during the 22-nd second, so you have to wait 21=12 - 1 = 1 second.

In the second test case, the clock shows time from the 1010-th until the 1515-th second, so during the 1414-th second it shows time, and you don't have to wait.

In the third test case, during the 1515-th second, the clock is already showing water temperature. So you have to wait till the 2525-th second, when the clock starts showing time again. You'll wait 2515=1025 - 15 = 10 seconds.

In the fourth test case, the clock will start showing time at the start of the 110110-th second, so you'll wait 110110=0110 - 110 = 0 seconds.

Sample Cases

Case 1

Input

5
1 1
5 14
5 15
10 110
99999999 1000000000

Output

1
0
10
0
99999989

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback