Circular Board Game
Time limit2sMemory limit512 MB
For each query with S, M, N, compute the final cell after advancing expo(S) steps on a circular board of N cells, using modular reduction of a huge power tower.
- Level
Medium6 of 10
- Topics
- Number theory, Math, Implementation
- Solved
- No attempts yet
Problem
A board game called Expo is played on a circular board with cells numbered to . Cell is followed by cell again, so a token that passes the end keeps going from the start.
A player rolls a die, reads the number on the top face and moves the token one cell forward times, where
So , , and . A player starts on cell and wins by standing exactly on cell once a move is complete. Passing cell in the middle of a move does not count.
Brad and Pete are playing this game. Once grows even a little, moving the token times takes longer than a lifetime. Their grandfathers never finished the game they started in their youth the traditional way, so the two of them play on their behalf. Given the number on the die and the cell the token stands on, report the cell the token stands on when the move is finished.
Input
The first line contains one integer (), the number of test cases.
Each of the next lines contains three space separated integers , and (, ): the number on the die, the cell the token stands on, and the number of cells on the board.
Output
For each test case print one line with the number of the cell the token stands on after moving cells forward from cell .