#P7723. [2019年杭电多校]Equation
[2019年杭电多校]Equation
Equation
Problem Description
Cuber QQ has encountered a math problem during his research of "nothing related to math", he thought the problem too boring for him, and decided to leave it to you. You might be curious about more background about why this problem appeared in his research, but to save your time, let's say "the background isn't helpful for you to solve this problem". Given , , count the number of sequence such that $x_1^2 + x_2^2 + \cdots + x_{n-1}^2 \equiv x_n^2 \pmod m$ and for . is odd in this problem.
Input
The first line of the input is an integer (), where is the number of test cases. Then follows test cases, each of which is a line with two space-separated integers and ( and is odd).
Output
For each test case, output the answer modulo .
Sample Input
3
3 5
5 3
9 101
Sample Output
25
81
980480839
Source
2019 Multi-University Training Contest 7