P15740 [CodeChef] LOOP
普及-
题目描述

There is a circular track of length M consisting of M checkpoints and M bidirectional roads such that each road has a length of 1 unit.

Chef is currently at checkpoint A and wants to reach checkpoint B. Find the minimum length of the road he needs to travel.

------ Input Format ------

  • First line will contain T, the number of test cases. Then the test cases follow.
  • Each test case contains a single line of input, three integers A, B, and M - the initial checkpoint, the final checkpoint, and the total number of checkpoints respectively.

------ Output Format ------

For each test case, output the minimum length Chef needs to travel.

------ Constraints ------

$1 ≤ T ≤ 1000$
$2 ≤ M ≤ 10^{9}$
$1 ≤ A, B ≤ M$
$A \neq B$

----- Sample Input 1 ------
4
1 3 100
1 98 100
40 30 50
2 1 2

----- Sample Output 1 ------
2
3
10
1

----- explanation 1 ------
Test Case $1$: Chef can go from $1$ to $3$ as: $1 \rightarrow 2$ and then $2 \rightarrow 3$. Thus, total length travelled is $2$ units.

Test Case $2$: Chef can go from $1$ to $98$ as: $98 arrow 99 arrow 100 arrow 1$. Thus, minimum distance travelled is $3$ units.

Test Case $3$: Chef can go from $40$ to $30$ as: $30 arrow 31 arrow 32 arrow \dots arrow 39 arrow 40$. Thus, minimum distance travelled is $10$ units.

Test Case $4$: Chef can go from $2$ to $1$ as: $1 arrow 2$. Thus, minimum distance travelled is $1$ unit.

输入格式

见题面

输出格式

见题面

样例输入
4 1 3 100 1 98 100 40 30 50 2 1 2
样例输出
2 3 10 1