CF1650B.DIV + MOD

传统题 时间 2000 ms 内存 256 MiB 4 尝试 1 已通过 1 标签

DIV + MOD

Not so long ago, Vlad came up with an interesting function:

  • $f_a(x)=\left\lfloor\frac{x}{a}\right\rfloor + x \bmod a$, where xa\left\lfloor\frac{x}{a}\right\rfloor is xa\frac{x}{a}, rounded down, xmodax \bmod a — the remainder of the integer division of xx by aa.

For example, with a=3a=3 and x=11x=11, the value $f_3(11) = \left\lfloor\frac{11}{3}\right\rfloor + 11 \bmod 3 = 3 + 2 = 5$.

The number aa is fixed and known to Vlad. Help Vlad find the maximum value of fa(x)f_a(x) if xx can take any integer value from ll to rr inclusive (lxrl \le x \le r).

Input

The first line of input data contains an integer tt (1t1041 \le t \le 10^4) — the number of input test cases.

This is followed by tt lines, each of which contains three integers lil_i, rir_i and aia_i (1liri109,1ai1091 \le l_i \le r_i \le 10^9, 1 \le a_i \le 10^9) — the left and right boundaries of the segment and the fixed value of aa.

Output

For each test case, output one number on a separate line — the maximum value of the function on a given segment for a given aa.

Note

In the first sample:

  • $f_3(1) = \left\lfloor\frac{1}{3}\right\rfloor + 1 \bmod 3 = 0 + 1 = 1$,
  • $f_3(2) = \left\lfloor\frac{2}{3}\right\rfloor + 2 \bmod 3 = 0 + 2 = 2$,
  • $f_3(3) = \left\lfloor\frac{3}{3}\right\rfloor + 3 \bmod 3 = 1 + 0 = 1$,
  • $f_3(4) = \left\lfloor\frac{4}{3}\right\rfloor + 4 \bmod 3 = 1 + 1 = 2$

As an answer, obviously, f3(2)f_3(2) and f3(4)f_3(4) are suitable.

Samples

5
1 4 3
5 8 4
6 10 6
1 1000000000 1000000000
10 12 8
2
4
5
999999999
5

在线编程 IDE

建议全屏模式获得最佳体验