luogu#P16336. 「ALFR Round 10」Another Add Problem
「ALFR Round 10」Another Add Problem
Problem Description
You are given an integer and a parameter . You need to perform exactly operations on this number. In each operation, you may choose one of the following two actions:
- Add to the number, i.e. .
- Take the number modulo , i.e. .
Find the minimum possible value of after performing operations.
Input Format
There are test cases.
The first line contains a positive integer , the number of test cases.
For each test case:
- A single line contains three positive integers .
Output Format
For each test case, output a single non-negative integer — the minimum possible value of .
7
1 2 2
3 1 5
2 3 3
6 7 1
1 6 1
4 9 1
9 1 1
0
3
0
0
0
0
0
Hint
Explanation
There are test cases in the sample.
For the first test case:
- In the first operation, set , so now .
- In the second operation, set , so now .
For the second test case:
- In the first operation, set , so now .
It can be proven that these operation sequences both make as small as possible.
Constraints
For of the data:
- ;
- ;
- .
| Subtask | Special Property | |
|---|---|---|
| 1 | None | |
| 2 | ^ | |
| 3 | ||
| 4 | None |