Circular Board Game

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.

Medium6Number theoryMathImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

A board game called Expo is played on a circular board with NN cells numbered 11 to NN. Cell NN is followed by cell 11 again, so a token that passes the end keeps going from the start.

A player rolls a die, reads the number SS on the top face and moves the token one cell forward expo(S)\operatorname{expo}(S) times, where

expo(S)=S(S1)(S2)21\operatorname{expo}(S) = S^{(S-1)^{(S-2)^{\,\cdots^{\,2^{1}}}}}

So expo(1)=1\operatorname{expo}(1) = 1, expo(2)=21=2\operatorname{expo}(2) = 2^{1} = 2, expo(3)=321=9\operatorname{expo}(3) = 3^{2^{1}} = 9 and expo(4)=49=262144\operatorname{expo}(4) = 4^{9} = 262144. A player starts on cell 11 and wins by standing exactly on cell NN once a move is complete. Passing cell NN in the middle of a move does not count.

Brad and Pete are playing this game. Once SS grows even a little, moving the token expo(S)\operatorname{expo}(S) 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 SS on the die and the cell MM the token stands on, report the cell the token stands on when the move is finished.

Input

The first line contains one integer TT (1T2561 \le T \le 256), the number of test cases.

Each of the next TT lines contains three space separated integers SS, MM and NN (1S1091 \le S \le 10^{9}, 1MN1091 \le M \le N \le 10^{9}): 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 expo(S)\operatorname{expo}(S) cells forward from cell MM.