A jail has a number of prisoners and a number of treats to pass out to them.  Their jailer decides the fairest way to divide the treats is to seat the prisoners around a circular table in sequentially numbered chairs.  A chair number will be drawn from a hat.  Beginning with the prisoner in that chair, one candy will be handed to each prisoner sequentially around the table until all have been distributed.

The jailer is playing a little joke, though.  The last piece of candy looks like all the others, but it tastes awful.  Determine the chair number occupied by the prisoner who will receive that candy.

Example  

$n=4$ 

$m=6$ 

$s=2$  

There are $4$ prisoners, $6$ pieces of candy and distribution starts at chair $2$.  The prisoners arrange themselves in seats numbered $1$ to $4$.  Prisoners receive candy at positions $2,3,4,1,2,3$.  The prisoner to be warned sits in chair number $3$.  

Function Description

Complete the saveThePrisoner function in the editor below.  It should return an integer representing the chair number of the prisoner to warn.  

saveThePrisoner has the following parameter(s):  

int n:  the number of prisoners  
int m:  the number of sweets  
int s:  the chair number to begin passing out sweets from  

Returns  

int: the chair number of the prisoner to warn

Input Format

The first line contains an integer, $\boldsymbol{\boldsymbol{t}}$, the number of test cases. 

The next $\boldsymbol{\boldsymbol{t}}$ lines each contain $3$ space-separated integers: 

$n$: the number of prisoners  
$m$: the number of sweets  
$s$: the chair number to start passing out treats at  

Constraints

$1\leq t\leq100$  
$1\leq n\leq10^9$
$1\leq m\leq10^9$
$1\leq s\leq n$

Sample Input 0
2
5 2 1
5 2 2

Sample Output 0
2
3

Explanation 0

In the first query, there are $n=5$ prisoners and $m=2$ sweets. Distribution starts at seat number $s=1$.  Prisoners in seats numbered $1$ and $2$ get sweets.  Warn prisoner $2$.   

In the second query, distribution starts at seat $2$ so prisoners in seats $2$ and $3$ get sweets.  Warn prisoner $3$.

Sample Input 1
2
7 19 2
3 7 3

Sample Output 1
6
3

Explanation 1

In the first test case, there are $n=7$ prisoners, $m=19$ sweets and they are passed out starting at chair $s=2$.  The candies go all around twice and there are $5$ more candies passed to each prisoner from seat $2$ to seat $\boldsymbol{6}$.  

In the second test case, there are $n=3$ prisoners, $m=7$ candies and they are passed out starting at seat $s=3$.  They go around twice, and there is one more to pass out to the prisoner at seat $3$.

Difficulty: EASY
Tags: Implementation

Starter code:

Solutions:
for _ in range(int(input().strip())):
	(n, m, s) = map(int, input().strip().split(' '))
	print((s - 2 + m) % n + 1)


t = int(input())
for _ in range(t):
	(n, m, s) = list(map(int, input().split()))
	print((m + s - 2) % n + 1)


T = int(input())
for i in range(T):
	(N, M, S) = list(map(int, input().split()))
	res = (S + M - 1) % N
	if res is 0:
		res = N
	print(res)


t = int(input())
while t > 0:
	t -= 1
	(n, m, s) = map(int, input().strip().split(' '))
	k = (s + m - 1) % n
	if k == 0:
		print(n)
	else:
		print(k)


t = int(input().strip())
for i in range(t):
	(n, m, s) = map(int, input().strip().split(' '))
	s = s - 1
	answer = (s + m - 1) % n
	answer += 1
	print(answer)


x = int(input())
for i in range(x):
	(n, m, s) = tuple((int(num) for num in input().split()))
	m = m % n
	s -= 1
	s += m
	s -= 1
	s = s % n
	print(s + 1)


noCases = int(input())
for a in range(noCases):
	inp = input().split(' ')
	N = int(inp[0])
	M = int(inp[1])
	S = int(inp[2])
	ID = (M % N + (S - 1)) % N
	if ID == 0:
		print(N)
	else:
		print(ID % N)


n = int(input())
for loop in range(n):
	[N, M, S] = map(int, input().split(' '))
	S = (S + M - 2) % N + 1
	print(S)


tests = int(input())
for test in range(tests):
	(prisoners, sweets, position) = map(int, input().split(' '))
	val = (position + sweets - 1) % prisoners
	if val != 0:
		print(val)
	else:
		print(prisoners)


for i in range(int(input())):
	(n, m, s) = map(int, input().split())
	m -= 1
	s -= 1
	print((s + m) % n + 1)


T = int(input())
while T > 0:
	testcase = input()
	case = [int(n) for n in testcase.split()]
	N = case[0]
	M = case[1]
	S = case[2]
	temp1 = S + M - 1
	temp2 = temp1 - N
	temp3 = temp2 % N
	if temp3 == 0:
		print(N)
	else:
		print(temp3)
	T = T - 1


t = int(input())
while t:
	(n, m, s) = map(int, input().split())
	p = s + m - 1
	while p > n:
		p = p - n
	print(p)
	t = t - 1


test_cases = int(input())
for _ in range(test_cases):
	(n, m, s) = tuple([int(s) for s in input().split(' ')])
	ending = (s + m - 1) % n
	if ending == 0:
		print(n)
	else:
		print(ending)


for _ in range(int(input())):
	(n, m, s) = map(int, input().split())
	print((s - 1 + m - 1) % n + 1)


t = int(input())
while t > 0:
	(n, m, s) = map(int, input().split())
	p = s + m - 1
	while p > n:
		p = p - n
	print(p)
	t = t - 1


for t in range(int(input())):
	(n, m, s) = map(int, input().split())
	ans = (m + s) % n - 1
	if ans == -1:
		print(n - 1)
	elif ans == 0:
		print(n)
	else:
		print(ans)


t = int(input())
for _ in range(t):
	parts = list(map(int, input().split(' ')))
	print((parts[1] + parts[2] - 2) % parts[0] + 1)


t = int(input())
for j in range(t):
	arr = [int(i) for i in input().split(' ')]
	n = arr[0]
	m = arr[1]
	s = arr[2]
	last = s + (m - 1 % n)
	if last > n:
		last = last % n
	if last == 0:
		last = n
	print(last)


t = int(input())
for case in range(t):
	[n, m, s] = list(map(int, input().split()))
	dead_guy = ((m + s) % n - 1) % n
	if dead_guy != 0:
		print(dead_guy)
	else:
		print(n)


import sys
tests = int(input())
for i in range(tests):
	(n_pris, m_sweets, s_start) = map(int, input().strip().split(' '))
	res = s_start + m_sweets - 1
	while res > n_pris:
		res -= n_pris
	print(res)


import sys
t = int(input().strip())
for a0 in range(t):
	(n, m, s) = [int(x) for x in input().strip().split(' ')]
	out = (m + s - 1) % n
	if out == 0:
		out = n
	print(out)


import sys

def lastCandyRecipient(prisonerCount, candyCount, startingPrisonerID):
	lastPrisonerID = (startingPrisonerID + candyCount - 1) % prisonerCount
	if lastPrisonerID == 0:
		return prisonerCount
	return lastPrisonerID
input = sys.stdin.readlines()
T = int(input[0])
for t in range(1, T + 1):
	NMS = input[t].split(' ')
	N = int(NMS[0])
	M = int(NMS[1])
	S = int(NMS[2])
	print(lastCandyRecipient(N, M, S))


T = int(input())
for t in range(T):
	(N, M, S) = [int(i) for i in input().split()]
	last = (M + S - 1) % N
	if last == 0:
		last = N
	print(last)


num_test_cases = int(input())
for i in range(num_test_cases):
	st = input()
	n = int(st.split()[0])
	m = int(st.split()[1])
	s = int(st.split()[2])
	if (s + m - 1) % n == 0:
		print(n)
	else:
		print((s + m - 1) % n)


from sys import stdin
T = int(stdin.readline().strip())
for _ in range(T):
	(N, M, S) = [int(x) for x in stdin.readline().strip().split(' ')]
	M -= 1
	M = M % N
	S += M
	if S > N:
		S -= N
	print(S)


t = int(input())
for i in range(t):
	(n, m, s) = list(map(int, input().split()))
	result = (s + m % n - 1) % n
	if result == 0:
		print(n)
	else:
		print(result)


for i in range(int(input())):
	(n, m, s) = list(map(int, input().strip().split()))
	print((s + (m - 1)) % n if (s + (m - 1)) % n != 0 else n)


t = int(input())
for i in range(t):
	(n, m, s) = input().strip().split(' ')
	n = int(n)
	m = int(m)
	s = int(s)
	z = m % n + s - 1
	if z == 0:
		print(n)
	elif z > n:
		print(z - n)
	else:
		print(z)


import sys

def solve(N, M, S):
	S = S - 1
	remain = M % N
	prev = calcPrev(S, N)
	res = (prev + remain) % N
	print(res + 1)

def calcPrev(S, N):
	if S == 0:
		return N - 1
	else:
		return S - 1
tests = int(sys.stdin.readline().strip())
for i in range(tests):
	(N, M, S) = [int(x) for x in sys.stdin.readline().strip().split()]
	solve(N, M, S)


import os
from sys import stdin
c = input()
ic = int(c)
count = 0
while count < ic:
	count += 1
	ln = input()
	retVal = 0
	(n, m, s) = ln.split(' ')
	iN = int(n)
	iM = int(m)
	iS = int(s)
	if iS + iM < iN:
		retVal = iS + iM - 1
	else:
		retVal = ((iM - 1) % iN + iS) % iN
		if retVal == 0:
			retVal = iN
	print(retVal)


import sys
T = int(sys.stdin.readline())
for t in range(T):
	(N, M, S) = map(int, sys.stdin.readline().split())
	sys.stderr.write('N=%d M=%d S=%d\n' % (N, M, S))
	p = 1 + (S + M - 2) % N
	print(p)


for t in range(int(input())):
	(n, m, s) = map(int, input().strip().split())
	print(((m - 1) % n + (s - 1)) % n + 1)


t = int(input())
for i in range(t):
	s = input()
	(n, m, k) = [int(j) for j in s.split(' ')]
	an = (k + m - 1) % n
	if an == 0:
		an = n
	print(an)


for _ in range(int(input().strip())):
	(N, M, S) = map(int, input().strip().split())
	X = N if M % N == 0 else M % N
	R = N if (X + S - 1) % N == 0 else (X + S - 1) % N
	print(R)


T = int(input())
for _ in range(T):
	(N, M, S) = map(int, input().split())
	if M + S - 1 <= N:
		print(M + S - 1)
	else:
		print(N if (M + S - 1) % N == 0 else (M + S - 1) % N)


for _ in range(int(input())):
	(prisioners_num, sweets_num, prisioner_id) = map(int, input().split())
	last = sweets_num % prisioners_num
	if last == 0:
		last = prisioners_num
	last = (last + prisioner_id - 1) % prisioners_num
	print(prisioners_num if last == 0 else last)


for _ in range(int(input())):
	(n, m, s) = [int(x) for x in input().split()]
	f = n if m % n == 0 else m % n
	print(n if (f + s - 1) % n == 0 else (f + s - 1) % n)


t = int(input())
for i in range(t):
	(n, m, s) = map(int, input().split())
	ans = (s + m - 1) % n
	print(ans if ans != 0 else n)


t = int(input())
for _ in range(t):
	(n, m, s) = map(int, input().split())
	m %= n
	if m + s > n:
		p = (m + s) % n - 1
	else:
		p = m + s - 1
	if p == 0:
		print(n)
	else:
		print(p)


T = int(input())
for t in range(0, T):
	L = input().strip().split()
	N = int(L[0])
	M = int(L[1])
	S = int(L[2])
	ans = (S - 1 + M % N) % N
	if ans == 0:
		ans = N
	print(ans)


def fun(n, m, s):
	res = (s + m - 1) % n
	if res == 0:
		return n
	else:
		return res
for _ in range(int(input())):
	(n, m, s) = [int(x) for x in input().split()]
	print(fun(n, m, s))


for _ in range(int(input())):
	[N, M, S] = [int(f) for f in input().split(' ')]
	M -= 1
	M = M % N
	X = M + S
	if X <= N:
		print(X)
	else:
		X = X % N
		print(X)


import sys
t = int(input())
for i in range(t):
	line = list(map(int, input().split(' ')))
	res = (line[2] + line[1] - 1) % line[0]
	print(line[0] if res == 0 else res)


for _ in range(int(input())):
	print((lambda n, m, s: (m + s - 2) % n + 1)(*map(int, input().split())))


T = int(input())
for _ in range(T):
	(N, M, S) = map(int, input().split())
	ans = (S + M - 1) % N
	if ans == 0:
		ans = N
	print(ans)


t = int(input())
for i in range(t):
	(n, m, s) = [int(x) for x in input().split()]
	p = (m % n + s - 1) % n
	if p == 0:
		p = n
	print(p)


tc = int(input())
for i in range(tc):
	(n, m, s) = input().split()
	(n, m, s) = [int(n), int(m), int(s)]
	if m < n:
		print((s + m - 1) % n)
	elif m % n == 0:
		if s == 1:
			print(n)
		else:
			print((s - 1) % n)
	elif s == 1:
		print(m % n)
	elif (s + m % n - 1) % n == 0:
		print(n)
	else:
		print((s + m % n - 1) % n)


t = int(input())
for i in range(t):
	(n, m, s) = map(int, input().split())
	m = m % n
	if s + m > n:
		m -= n - s + 1
		if m == 0:
			print(n)
		else:
			print(m)
	elif s == 1 and m == 0:
		print(n)
	else:
		print(s + m - 1)


for _ in range(int(input())):
	(N, M, S) = map(int, input().split())
	P = (S + M - 1) % N
	print(P or N)


num_tests = int(input())
for _ in range(num_tests):
	(num_prisoners, num_sweets, starting_id) = [int(x) for x in input().split()]
	leftover = num_sweets % num_prisoners
	if leftover == 0:
		nominal_last_prisoner = num_prisoners
	else:
		nominal_last_prisoner = leftover
	last_prisoner = (nominal_last_prisoner + starting_id - 1) % num_prisoners
	if last_prisoner == 0:
		last_prisoner = num_prisoners
	print(last_prisoner)


for i in range(int(input())):
	(n, m, s) = map(int, input().split())
	id = (m + s - 1) % n
	print(id if id else n)


for _ in range(int(input())):
	(n, sw, pid) = [int(i) for i in input().strip().split()]
	print((sw + pid - 2) % n + 1)


t = int(input())
for x in range(t):
	(prisoners_count, sweets_count, prisoner_id) = [int(x) for x in input().split(' ')]

	def solve():
		offset = sweets_count % prisoners_count - 1
		poisoned_prisoner = (offset + prisoner_id) % prisoners_count
		if poisoned_prisoner == 0:
			poisoned_prisoner = prisoners_count
		print(poisoned_prisoner)
	solve()


for i in range(int(input())):
	li = [int(x) for x in input().split()]
	id = li[2]
	i = li[1] % li[0] + li[2]
	if i - 1 == 0:
		print(li[0])
	elif i - 1 > li[0]:
		x = li[1] % li[0] - (li[0] - li[2])
		print(x - 1)
	else:
		print(i - 1)


def last_prisoner(nprisoners, nsweets, first):
	last = (nsweets + first - 1) % nprisoners
	if last == 0:
		last = nprisoners
	return last
for i in range(int(input())):
	print(last_prisoner(*map(int, input().split())))


import sys
T = int(input().strip())
for j in range(T):
	arr = [int(i) for i in input().strip().split(' ')]
	warn = (arr[1] % arr[0] + arr[2] - 1) % arr[0]
	if warn == 0:
		print(arr[0])
	else:
		print(warn)


def last_prisoner(nprisoners, nsweets, first):
	last = (nsweets + first - 1) % nprisoners
	if last == 0:
		last = nprisoners
	return last
for i in range(int(input())):
	print(last_prisoner(*map(int, input().split())))


for t in range(int(input())):
	(n, m, s) = [int(i) for i in input().split()]
	print((m + s - 2) % n + 1)


T = int(input().strip())
for x in range(0, T):
	(N, M, S) = [int(x) for x in input().strip().split(' ')]
	warn = (S + M - 1) % N
	if warn == 0:
		warn = N
	print(warn)


def num(s):
	try:
		return int(s)
	except ValueError:
		return float(s)
T = num(input())
for x in range(T):
	values = input()
	values = values.split(' ')
	N = num(values[0])
	M = num(values[1])
	S = num(values[2])
	ans = (S + M - 1) % N
	if ans == 0:
		ans = N
	print(ans)


t = int(input().strip())
for _ in range(t):
	(n, m, s) = map(int, input().strip().split())
	r = (s + m - 1) % n
	if r == 0:
		r = n
	print(r)


for _ in range(int(input())):
	(n, m, s) = map(int, input().split())
	p = s + m - 1
	if n < p:
		p %= n
		if p == 0:
			p = n
	print(p)


def determine_last_prisoner(num_prisoners, num_sweets, start_position):
	return (num_sweets - 1 + (start_position - 1)) % num_prisoners + 1
T = int(input())
for _ in range(T):
	(N, M, S) = [int(x) for x in input().split()]
	print(determine_last_prisoner(N, M, S))


i = input()
for k in range(int(i)):
	l = input().split()
	res = (int(l[2]) + int(l[1]) - 1) % int(l[0])
	if res == 0:
		print(l[0])
	else:
		print(res)


T = int(input())
for test in range(T):
	myin = str(input()).split(' ')
	n = int(myin[0])
	m = (int(myin[1]) - 1) % n
	p = int(myin[2])
	if m is 0:
		print(p)
		continue
	p = p + m
	if p > n:
		p = p % n
	print(p)


num_test_cases = int(input())
input_array = []

def find_poison(tuple_of_three):
	(num_prisoners, num_sweets, start_id) = tuple_of_three
	if num_prisoners >= num_sweets:
		poison = num_prisoners - (num_prisoners - num_sweets) + (start_id - 1)
	else:
		poison = num_sweets % num_prisoners + (start_id - 1)
	if poison > num_prisoners:
		poison = poison % num_prisoners
	return poison
for i in range(num_test_cases):
	input_array.append(tuple(map(lambda x: int(x), input().split(' '))))
	print(find_poison(input_array[i]))


cases = int(input().strip())
for i in range(cases):
	arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
	num = arr[0]
	sweets = arr[1]
	start = arr[2]
	poisoned = (sweets + start - 1) % num
	if poisoned == 0:
		print(num)
	else:
		print(poisoned)


for _ in range(int(input())):
	(n, m, s) = map(int, input().split())
	print((m + s - 1) % n or n)


T = int(input().strip())
for i in range(0, T):
	inp = input().split()
	N = int(inp[0])
	M = int(inp[1]) - 1
	S = int(inp[2]) - 1
	answer = (S + M) % N + 1
	print(answer)


t = int(input())
for i in range(t):
	(m, n, s) = input().strip().split()
	m = int(m)
	n = int(n)
	s = int(s)
	r = n % m
	if r + s > m:
		if r + s - 1 - m != 0:
			print(r + s - 1 - m)
		else:
			print(m)
	elif r + s - 1 != 0:
		print(r + s - 1)
	else:
		print(m)


def prisnor(a, T):
	for j in range(T):
		x = int(a[j][2]) + (int(a[j][1]) % int(a[j][0]) - 1)
		if x == 0:
			print(int(a[j][0]))
		elif x > int(a[j][0]):
			print(x % int(a[j][0]))
		else:
			print(x)
T = int(input().strip())
a = []
for i in range(T):
	str_temp = input().strip().split(' ')
	a.append(str_temp)
prisnor(a, T)


t = int(input().strip())
n = []
m = []
s = []
for i in range(t):
	x = input().strip().split()
	n.append(int(x[0]))
	m.append(int(x[1]))
	s.append(int(x[2]))
for i in range(t):
	sweet = m[i]
	prisoner = s[i] - 1
	if sweet == n[i]:
		prisoner = prisoner - 1
		if prisoner == -1:
			prisoner = n[i]
	elif sweet % n[i] == 0:
		prisoner = prisoner
	elif sweet % n[i] != 0:
		prisoner = prisoner + sweet % n[i]
	if prisoner > n[i]:
		prisoner = prisoner - n[i]
	print(prisoner)


def solution(n, m, s):
	if m > n:
		m = m % n
	v = s + (m - 1)
	if v > n:
		v = v % n
	return v
cases = int(input())
for i in range(cases):
	(n, m, s) = input().strip().split(' ')
	print(solution(int(n), int(m), int(s)))


import sys
t = int(input().strip())
for i in range(t):
	arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
	tmp = (arr[2] - 1 + arr[1]) % arr[0]
	if tmp == 0:
		tmp = arr[0]
	print(tmp)


T = int(input().strip())
i = 1
while i <= T:
	in_put = input().split(' ')
	N = int(in_put[0])
	M = int(in_put[1])
	S = int(in_put[2])
	if (M + S - 1) % N == 0:
		print(N)
	else:
		print((M + S - 1) % N)
	i += 1


import sys
T = int(input().strip())
for i in range(T):
	arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
	N = arr[0]
	M = arr[1]
	S = arr[2]
	num = (S % N + M - 1) % N
	if num == 0:
		num = N
	print(num)


t = int(input().strip())

def get_poisoned_id(prisoners, sweets, start_id):
	return (start_id + sweets - 1) % prisoners
for test in range(t):
	(prisoners, sweets, start_id) = [int(x) for x in input().split()]
	print(get_poisoned_id(prisoners, sweets, start_id - 1) + 1)


cases = int(input())

def save_the_prisoner(n, m, s):
	return (m + s - 1) % n or n
for case in range(cases):
	(n, m, s) = map(int, input().split())
	print(save_the_prisoner(n, m, s))


for i in range(int(input())):
	(n, m, s) = map(int, input().split())
	print((m + s - 1) % n or n)


T = int(input())
for i in range(T):
	(n, m, s) = [int(x) for x in input().strip().split(' ')]
	print((s + m - 2) % n + 1)


tests = int(input())
for t in range(tests):
	(n, m, s) = [int(x) for x in input().split()]
	p = s + m - 1
	if p > n:
		p %= n
	if p == 0:
		p = n
	print(p)


t = int(input())
cases = []
for i in range(t):
	cases.append([int(x) for x in input().strip().split(' ')])
for c in cases:
	n = c[0]
	m = c[1]
	s = c[2]
	r = m % n
	if m == n:
		k = m
	elif s - 1 + r <= n:
		k = r + s - 1
	else:
		k = r + s - 1 - n
	print(k)


T = int(input())
for i in range(T):
	(N, M, S) = map(int, input().split(' '))
	P = (S + M - 1) % N
	if P == 0:
		P = N
	print(P)


T = int(input())
for t in range(T):
	(N, M, S) = map(int, input().split())
	print((M + S - 2) % N + 1)


for t in range(int(input())):
	(n, m, s) = [int(x) for x in input().split()]
	m = m % n
	print((s - 2 + m) % n + 1)


t = int(input())

def solve(n, m, s):
	l = (s + m - 1) % n
	if l == 0:
		l = n
	return l
for _ in range(t):
	(n, m, s) = map(int, input().split())
	print(solve(n, m, s))


t = int(input())
for i in range(t):
	(n, m, s) = [int(x) for x in input().split()]
	tmp = (m + s - 1) % n
	print(tmp if tmp != 0 else n)


t = int(input())
for _ in range(t):
	(n, m, s) = [int(x) for x in input().strip().split()]
	m -= 1
	s -= 1
	print((s + m) % n + 1)


T = int(input())
while T > 0:
	T -= 1
	(prisoners, sweets, start) = [int(x) for x in input().split()]
	print((start - 1 + sweets) % prisoners if (start - 1 + sweets) % prisoners != 0 else prisoners)


test_cases = int(input().strip())
for i in range(0, test_cases):
	inputs = input().strip().split(' ')
	num_prisoners = int(inputs[0])
	num_sweets = int(inputs[1])
	starting_prisoner = int(inputs[2])
	prisoner_index = (num_sweets + starting_prisoner - 2) % num_prisoners
	print(1 + prisoner_index)


def find(m, n, s):
	if m + s <= n:
		return m + s
	else:
		return m + s - n
T = int(input().strip())
while T > 0:
	NMS = input().split(' ')
	(N, M, S) = (int(NMS[0]), int(NMS[1]) - 1, int(NMS[2]))
	if M > N:
		M %= N
		print(find(M, N, S))
	else:
		print(find(M, N, S))
	T -= 1


t = int(input())
while t > 0:
	(n, m, s) = map(int, input().split(' '))
	print((s - 1 + m - 1) % n + 1, end='\n')
	t -= 1