Codeforces Global Round 31 (Div. 1 + Div. 2)
A
Carnival Wheel
Codeforces
800
原题
←B →
翻译中…

You have a prize wheel divided into $l$ sections, numbered from $0$ to $l-1$. The sections are arranged in a circle, so after section $l-1$, the numbering continues again from section $0$.

翻译中…

Initially, the prize pointer is at section $a$. Each time you spin the wheel, the pointer moves exactly $b$ sections forward. That is, after one spin, the pointer moves from section $a$ to section $(a+b)\bmod l$, after two spins to $(a+2b)\bmod l$, and so on$^{\text{∗}}$.

翻译中…

You may spin the wheel any number of times (including zero). After you stop, the section where the pointer finally lands determines your prize: you receive an amount equal to the number of that section.

翻译中…

What is the maximum prize you can obtain?

翻译中…

$^{\text{∗}}$Here, $x \bmod y$ denotes the remainder from dividing $x$ by $y$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.

翻译中…

The first line of each test case contains three integers $l, a$, and $b$ ($1 \le l, b \le 5000$, $0 \le a \le l-1$).

翻译中…

Output

翻译中…

For each test case, output the maximum prize that can be obtained.

翻译中…

Note

翻译中…

In the first test case, by spinning the wheel three times and then claiming the reward, you can obtain a maximum value of $4$. The sequence of pointer positions is: $3, 0, 2, 4, 1, 3, 0, \ldots$

翻译中…

In the second test case, the pointer will remain on section $0$ indefinitely.

翻译中…

In the fourth test case, with $b = 1$ and starting from section $0$, the pointer will iterate over all sections, including the last one.

翻译中…

样例

Input #1
4
5 3 2
2 0 6
8 2 4
100 0 1
Output #1
4
0
6
99
InputCopy OutputCopy
题目信息
题号
2180A
时间限制
1 s
内存限制
256 MB
标签
暴力
数论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。