Statement
Statement language
There is an infinite sequence . We have , , and for every nonnegative integer ,
- ,
- .
You are given two coprime positive integers . It is guaranteed that there exists a unique positive integer satisfying .
Find modulo .
Input
The input is given from Standard Input in the following format:
Each case is given in the following format:
Output
For each test case, print the corresponding modulo .
Constraints
- .
- .
- .
Subtasks
Samples
Input
3
1 1
3 2
2 3
Output
1
5
6
The first several terms of the sequence are
Therefore, the values of satisfying the condition in the three test cases are , respectively.