Your task is to find the number of integral solutions to x2 = 1 (mod p) in the closed interval [1, b].
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of one line. There are two space separated integers in this line which denote b and p.
Corresponding to each test case, in a new line, print the number of integral solution to x2 = 1 (mod p) in the closed interval [1, b].
1 ≤ T ≤ 200
1 ≤ b ≤ 109
1 ≤ p ≤ 105
In the interval [1,5] there is only one solution to x2 = 1(mod 7), x = 1
In the interval [1,8] there are three solutions to x2 = 1(mod 6), x = 1,5,7
In the interval [1,4] there are three solutions to x2 = 1(mod 3), x = 1,2,4
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.