My solution is based on the josephus problem which gives solution in O(n) time but I am getting TLE.
My code is
#include <stdio.h>
int main(){
int t;
scanf("%d", &t);
while(t--){
int m, n, o;
scanf("%d %d %d", &n, &m, &o);
if(n == 1){
printf("1\n");
continue;
}
int ans = 1;
for(int i = 2; i <= n; ++i){
ans = (ans + o - 1)%i + 1;
}
printf("%d\n", (ans + m - 1)%n + 1);
}
return 0;
}