Save the Prisoner!

import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(;
int t = input.nextInt(); //number of test cases
for(int i = 1; i <= t; ++i) {
int n = input.nextInt();
//number of prisoners
int m = input.nextInt();
//number of sweets
int s = input.nextInt();
//starting id
//minus one because we count from (include) s
//if s = 2, m = 2
//then the prisoner having poisoined sweet is 3 (not 4)
m %= n;
System.out.println(m+s > n ? ((m+s)%n) : m+s);

A jail has N prisoners, and each prisoner has a unique id number, S, ranging from 1 to N. There are sweets that must be distributed to the prisoners.

The jailer decides the fairest way to do this is by sitting the prisoners down in a circle (ordered by ascending S), and then, starting with some random S, distribute one candy at a time to each sequentially numbered prisoner until all M candies are distributed. For example, if the jailer picks prisoner S=2, then his distribution order would be (2,3,4,5,,n1,n,1,2,3,4,until all M sweets are distributed.

But wait—there’s a catch—the very last sweet is poisoned! Can you find and print the ID number of the last prisoner to receive a sweet so he can be warned?

Input Format

The first line contains an integer, T, denoting the number of test cases.
The T subsequent lines each contain space-separated integers:
N (the number of prisoners), M (the number of sweets), and S (the prisoner ID), respectively.


  • 1T100
  • 1N10^9
  • 1M10^9
  • 1S10^9

Output Format

For each test case, print the ID number of the prisoner who receives the poisoned sweet on a new line.

Sample Input

5 2 1

Sample Output



There are N=5 prisoners and M=2 sweets. Distribution starts at ID number S=1, so prisoner 1 gets the first sweet and prisoner 2 gets the second (last) sweet. Thus, we must warn prisoner 2 about the poison, so we print 2 on a new line.