luogu#P16336. 「ALFR Round 10」Another Add Problem

    ID: 16192 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 1 上传者: 标签>数学洛谷原创O2优化洛谷月赛分类讨论

「ALFR Round 10」Another Add Problem

Problem Description

You are given an integer nn and a parameter kk. You need to perform exactly mm operations on this number. In each operation, you may choose one of the following two actions:

  • Add 11 to the number, i.e. nn+1n \gets n + 1.
  • Take the number modulo kk, i.e. nnmodkn \gets n \bmod k.

Find the minimum possible value of nn after performing mm operations.

Input Format

There are TT test cases.

The first line contains a positive integer TT, the number of test cases.

For each test case:

  • A single line contains three positive integers n,m,kn, m, k.

Output Format

For each test case, output a single non-negative integer — the minimum possible value of nn.

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 77 test cases in the sample.

For the first test case:

  • In the first operation, set nn+1n \gets n + 1, so now n=2n = 2.
  • In the second operation, set nnmodkn \gets n \bmod k, so now n=0n = 0.

For the second test case:

  • In the first operation, set nnmodkn \gets n \bmod k, so now n=3n = 3.

It can be proven that these operation sequences both make nn as small as possible.

Constraints

For 100%100\% of the data:

  • 1T2×1051 \le T \le 2 \times 10^5;
  • 0n,m,k<2300 \le n, m, k < 2^{30};
  • k1k \ge 1.
Subtask n,m,k<n,m,k < Special Property
1 232^3 None
2 292^9 ^
3 2172^{17} T100T \le 100
4 2302^{30} None