Number of solutions
Submissions: 671   Accuracy:

8.12%

  Difficulty: Easy   Marks: 2

Your task is to find the number of integral solutions to x2 = 1 (mod p) in the closed interval [1, b].

Input:
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.


Output:

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].


Constraints:

1 ≤ T ≤ 200

1 ≤ b ≤ 10

1 ≤ p ≤ 105


Example:

Input

3
5 7
8 6
4 3

Output

1
3
3

Explanation

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

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: Hemang Sarkar


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.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.