atcoder#ABC186E. [ABC186E] Throne

[ABC186E] Throne

Score : 500500 points

Problem Statement

We have NN chairs arranged in a circle, one of which is a throne.

Takahashi is initially sitting on the chair that is SS chairs away from the throne in the clockwise direction. Now, he will repeat the move below.

Move: Go to the chair that is KK chairs away from the chair he is currently sitting on in the clockwise direction.

After how many moves will he be sitting on the throne for the first time? If he is never going to sit on it, report -1 instead.

You are asked to solve TT test cases.

Constraints

  • 1T1001\leq T \leq 100
  • 2N1092\leq N \leq 10^9
  • 1S<N1\leq S < N
  • 1K1091\leq K \leq 10^9

Input

Input is given from Standard Input in the following format. The first line is in the format below:

TT

Then, the following TT lines represent TT test cases. Each of these lines is in the format below:

NN SS KK

Output

For each test case, print the answer in its own line.

4
10 4 3
1000 11 2
998244353 897581057 595591169
10000 6 14
2
-1
249561088
3571

In the first test case, we have 1010 chairs, and Takahashi is initially sitting on the chair that is 44 chairs away from the throne in the clockwise direction. He will be sitting on the throne after 22 moves of moving 33 chairs in the clockwise direction.

In the second test case, he will never sit on the throne, so we should print -1.