311264: CF1958B. Clock in the Pool

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Clock in the Pooltime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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 $0$-th second it starts showing water temperature, at the start of the $k$-th second it switches to air temperature. At the start of the $2k$-th second, it switches to showing time.

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

You looked at the clock during the $m$-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 $t$ independent test cases.

Input

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

The first and only line of each test case contains two integers $k$ and $m$ ($1 \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

For each test case, print a single integer — the time you have to wait from the moment $m$ 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.

ExampleInput
5
1 1
5 14
5 15
10 110
99999999 1000000000
Output
1
0
10
0
99999989
Note

In the first test case, the clock will start showing time during the $2$-nd second, so you have to wait $2 - 1 = 1$ second.

In the second test case, the clock shows time from the $10$-th until the $15$-th second, so during the $14$-th second it shows time, and you don't have to wait.

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

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

Output

题目大意:
你正在游泳池里游泳,需要控制游泳的时间。游泳池有一个时钟,它会在三种模式之间循环:显示水温、显示气温和显示时间。在第0秒开始时显示水温,在第k秒开始时切换到显示气温,在第2k秒开始时切换到显示时间。在第3k秒开始时,时钟又重新显示水温,在第4k秒开始时显示气温,依此类推。你在第m秒查看时钟以检查时间,但可能时钟此刻并没有显示时间。你需要等待多久才能在时钟上看到时间?解答t个独立的测试用例。

输入输出数据格式:
输入:
第一行包含一个整数t(1≤t≤10^4)——测试用例的数量。接下来是t个测试用例。
每个测试用例的第一行(也是唯一一行)包含两个整数k和m(1≤k≤10^8; 1≤m≤10^9)——时钟周期的长度和检查时钟的时间点。

输出:
对于每个测试用例,打印一个整数——从时刻m开始到时钟开始显示时间的等待时间。

示例:
输入:
```
5
1 1
5 14
5 15
10 110
99999999 1000000000
```
输出:
```
1
0
10
0
99999989
```

注意:
在第一个测试用例中,时钟将在第2秒开始显示时间,因此你需要等待2-1=1秒。
在第二个测试用例中,时钟从第10秒到第15秒显示时间,所以在第14秒它显示时间,你不需要等待。
在第三个测试用例中,在第15秒时钟已经显示水温。因此,你需要等待到第25秒时钟再次开始显示时间。你将等待25-15=10秒。
在第四个测试用例中,时钟将在第110秒开始显示时间,所以你将等待110-110=0秒。题目大意: 你正在游泳池里游泳,需要控制游泳的时间。游泳池有一个时钟,它会在三种模式之间循环:显示水温、显示气温和显示时间。在第0秒开始时显示水温,在第k秒开始时切换到显示气温,在第2k秒开始时切换到显示时间。在第3k秒开始时,时钟又重新显示水温,在第4k秒开始时显示气温,依此类推。你在第m秒查看时钟以检查时间,但可能时钟此刻并没有显示时间。你需要等待多久才能在时钟上看到时间?解答t个独立的测试用例。 输入输出数据格式: 输入: 第一行包含一个整数t(1≤t≤10^4)——测试用例的数量。接下来是t个测试用例。 每个测试用例的第一行(也是唯一一行)包含两个整数k和m(1≤k≤10^8; 1≤m≤10^9)——时钟周期的长度和检查时钟的时间点。 输出: 对于每个测试用例,打印一个整数——从时刻m开始到时钟开始显示时间的等待时间。 示例: 输入: ``` 5 1 1 5 14 5 15 10 110 99999999 1000000000 ``` 输出: ``` 1 0 10 0 99999989 ``` 注意: 在第一个测试用例中,时钟将在第2秒开始显示时间,因此你需要等待2-1=1秒。 在第二个测试用例中,时钟从第10秒到第15秒显示时间,所以在第14秒它显示时间,你不需要等待。 在第三个测试用例中,在第15秒时钟已经显示水温。因此,你需要等待到第25秒时钟再次开始显示时间。你将等待25-15=10秒。 在第四个测试用例中,时钟将在第110秒开始显示时间,所以你将等待110-110=0秒。

加入题单

上一题 下一题 算法标签: