Problem7229--CodeChef SMOL - Smallest Possible Whole Number

7229: CodeChef SMOL - Smallest Possible Whole Number

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 256 MiB

Description

You are given two integers N and K. 
You may perform the following operation any number of times (including zero): change N to N-K, i.e. subtract K from N. 
Find the smallest non-negative integer value of N you can obtain this way.

Input

The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first and only line of each test case contains two space-separated integers N and K.

Output

For each test case, print a single line containing one integer — the smallest value you can get.

Constraints

$1≤T≤10^5$
$1 \leq N \leq 10^9$
$0 \leq K \leq 10^9$

Sample 1 Input

3
5 2
4 4
2 5

Sample 1 Output

1
0
2
Example case 1:
  • First, we change N = 5 to N - K = 5 - 2 = 3.
  • Then, we have N = 3 and we change it to N - K = 3 - 2 = 1.
Since 1<K, the process stops here and the smallest value is 1.
Example case 2: We change N = 4 to N - K = 4 - 4 = 0. Since 0<K, the process stops here and the smallest value is 0.
Example case 3: Since 2<K initially, we should not perform any operations and the smallest value is 2.

HINT

题目来源:CodeChef SMOL

Source/Category