Your task is to find the number of integral solutions to x^{2} = 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 x^{2} = 1 (mod p) in the closed interval [1, b].

Explanation:
In the interval [1,5] there is only one solution to x^{2} = 1(mod 7), x = 1
In the interval [1,8] there are three solutions to x^{2} = 1(mod 6), x = 1,5,7
In the interval [1,4] there are three solutions to x^{2} = 1(mod 3), x = 1,2,4