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.

Source: codechef | Difficulty: EASY
Tags: Mathematics

Implementation


Solutions:
t = int(input())
for i in range(t):
	(a, b, m) = map(int, input().split())
	s = max(b, a) - min(a, b)
	b = m - max(b, a) + min(a, b)
	print(min(b, s))


for i in range(int(input())):
	(a, b, m) = map(int, input().split())
	f = abs(a - b)
	print(min(f, m - f))


t = int(input())
for h in range(t):
	(a, b, m) = map(int, input().split())
	x = abs(a - b)
	y = min(m - x, x)
	print(y)


t = int(input())
for i in range(t):
	(a, b, m) = map(int, input().split())
	if a > b:
		print(min(a - b, m - a + b))
	else:
		print(min(b - a, a + m - b))


T = int(input())
for i in range(T):
	(a, b, m) = map(int, input().split())
	d = abs(a - b)
	print(min(d, m - d))


for t in range(int(input())):
	(a, b, m) = map(int, input().split())
	diff = abs(a - b)
	print(min(diff, m - diff))


n = int(input())
for i in range(n):
	(a, b, c) = list(map(int, input().split()))
	value = abs(a - b)
	if value > c // 2:
		print(c - value)
	else:
		print(value)


t = int(input())
for i in range(t):
	(a, b, c) = map(int, input().split())
	d = abs(a - b)
	if d <= c / 2:
		print(d)
	else:
		print(c - d)


for i in range(int(input())):
	(a, b, m) = map(int, input().split())
	if abs(b - a) > m - abs(b - a):
		print(m - abs(b - a))
	else:
		print(abs(b - a))


for _ in range(int(input())):
	(a, b, c) = map(int, input().split())
	d = abs(a - b)
	e = abs(c - d)
	print(min(e, d))


t = int(input())
for i in range(t):
	(a, b, m) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	x = abs(a - b)
	y = a + m - b
	print(min(x, y))


t = int(input())
for i in range(t):
	(a, b, t) = list(map(int, input().split(' ')))
	check = abs(a - b)
	print(min(check, t - check))


t = int(input())
for _ in range(t):
	(a, b, m) = map(int, input().split())
	f = abs(a - b)
	print(min(f, m - f))


n = int(input())
for i in range(n):
	(a, b, c) = map(int, input().split())
	d = abs(a - b)
	e = abs(d - c)
	print(min(d, e))


T = int(input())
for _ in range(T):
	(A, B, M) = map(int, input().split())
	option1 = abs(A - B)
	option2 = M - abs(A - B)
	print(min(option1, option2))


for t in range(int(input())):
	(a, b, m) = map(int, input().split())
	print(abs(b - a)) if abs(b - a) <= m // 2 else print(m - max(a, b) + min(a, b))


n = int(input())
for i in range(1, n + 1):
	(a, b, c) = map(int, input().split())
	f = abs(a - b)
	print(min(f, c - f))


t = int(input())
for i in range(0, t):
	(a, b, m) = map(int, input().split())
	res1 = abs(a - b)
	res2 = m - res1
	print(min(res1, res2))


t = int(input())
for i in range(t):
	(a, b, m) = map(int, input().split())
	g = abs(a - b)
	print(min(g, m - g))


for i in range(int(input())):
	(a, b, m) = map(int, input().split())
	c = max(a, b) - min(a, b)
	if c > m / 2:
		print(m - c)
	else:
		print(c)


t = int(input())
for i in range(t):
	(a, b, m) = map(int, input().split())
	ans1 = abs(b - a)
	ans2 = m - ans1
	print(min(ans1, ans2))


t = int(input())
for i in range(t):
	(x, y, a) = map(int, input().split())
	print(min(abs(y - x), a - abs(y - x)))


for i in range(int(input())):
	(a, b, c) = map(int, input().split())
	print(min((abs(a - b), c - abs(a - b))))


for i in range(int(input())):
	(A, B, M) = map(int, input().split())
	res = max(A, B)
	ans = min(A, B)
	if abs(A - B) < M / 2:
		print(abs(A - B))
	else:
		print(M - res + ans)


for i in range(int(input())):
	(A, B, M) = map(int, input().split())
	BIG_NUMBER = max(A, B)
	SMALL_NUMBER = min(A, B)
	if abs(A - B) < M / 2:
		print(abs(A - B))
	else:
		print(M - BIG_NUMBER + SMALL_NUMBER)


for _ in range(int(input())):
	(a, b, m) = map(int, input().split())
	if b < a:
		(a, b) = (b, a)
	min_route = min(b - a, m - b + a)
	print(min_route)


testcases = int(input())
for i in range(testcases):
	(a, b, m) = map(int, input().split())
	num1 = abs(b - a)
	num2 = m - abs(b - a)
	num3 = min(num1, num2)
	print(num3)


t = int(input())
for i in range(t):
	(A, B, M) = map(int, input().split())
	print(min(abs(B - A), abs(M - abs(B - A))))


T = int(input())
for i in range(T):
	(a, b, m) = map(int, input().split())
	n1 = abs(b - a)
	n2 = m - abs(b - a)
	print(min(n1, n2))


for i in range(0, int(input())):
	(a, b, m) = map(int, input().split())
	x = abs(b - a)
	y = abs(m - x)
	print(min(x, y))


import math

def solution():
	pass
T = int(input())
for _ in range(T):
	(A, B, M) = map(int, input().split())
	print(min(abs(A - B), M - abs(A - B)))


import math

def solution():
	pass
T = int(input())
for _ in range(T):
	(A, B, M) = map(int, input().split())
	(cw, acw) = (0, 0)
	if A < B:
		cw = abs(A - B)
		acw = abs(B - M) + A
	else:
		cw = abs(M - A) + B
		acw = abs(A - B)
	print(min(cw, acw))


t = int(input())
for i in range(t):
	(a, b, m) = [int(j) for j in input().split()]
	if abs(a - b) <= m - max(a, b) + min(a, b):
		print(abs(a - b))
	else:
		print(m - max(a, b) + min(a, b))


import math
for i in range(int(input())):
	(a, b, m) = map(int, input().split())
	x = math.fabs(b - a)
	y = math.fabs(x - m)
	if x >= y:
		print(int(y))
	else:
		print(int(x))


for _ in range(int(input())):
	(x, y, d) = map(int, input().split(' '))
	print(min(abs(x - y), d - abs(x - y)))


for i in range(int(input())):
	(a, b, m) = map(int, input().split())
	x = abs(a - b)
	n = m - x
	print(min(x, n))