atcoder#ABC290D. [ABC290D] Marking

[ABC290D] Marking

Score : 400400 points

Problem Statement

There are NN squares indexed 00 through (N1)(N-1) arranged in a line. Snuke is going to mark every square by the following procedure.

  1. Mark square 00.
  2. Repeat the following steps i - iii (N1)(N-1) times.
    1. Initialize a variable xx with (A+D)modN(A+D) \bmod N, where AA is the index of the square marked last time.
    2. While square xx is marked, repeat replacing xx with (x+1)modN(x+1) \bmod N.
    3. Mark square xx.

Find the index of the square that Snuke marks for the KK-th time.

Given TT test cases, find the answer for each of them.

Constraints

  • 1T1051\leq T \leq 10^5
  • 1KN1091\leq K\leq N \leq 10^9
  • 1D1091\leq D \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format, where testi\mathrm{test}_i denotes the ii-th test case:

TT

test1\mathrm{test}_1

test2\mathrm{test}_2

\vdots

testT\mathrm{test}_T

Each test case is given in the following format:

NN DD KK

Output

Print TT lines.

The ii-th (1iT)(1\leq i \leq T) line should contain the answer to the ii-th test case.

9
4 2 1
4 2 2
4 2 3
4 2 4
5 8 1
5 8 2
5 8 3
5 8 4
5 8 5
0
2
1
3
0
3
1
4
2

If N=4N=4 and D=2D=2, Snuke marks the squares as follows.

  1. Mark square 00.
  2. (11-st iteration) Let x=(0+2)mod4=2x=(0+2)\bmod 4=2. Since square 22 is not marked, mark it. (22-nd iteration) Let x=(2+2)mod4=0x=(2+2)\bmod 4=0. Since square 00 is marked, let x=(0+1)mod4=1x=(0+1)\bmod 4=1. Since square 11 is not marked, mark it. (33-rd iteration) Let x=(1+2)mod4=3x=(1+2)\bmod 4=3. Since square 33 is not marked, mark it.