Wrong Modulo

Time limit: 1.5s Memory limit: 128MB Input: Output:

Andu made a terrible mistake! Just like the time he headed to Neamț and ended up in Târgu Neamț instead of Piatra Neamț, he made a similar mistake at school. He thought that (ax) % (ay)=x % y(a \cdot x) \ \% \ (a \cdot y) = x \ \% \ y.

Task

Still curious, he asked himself: given two integers NN and aa, how many pairs (x,y)(x, y) with 1x,yN1 \leq x, y \leq N exist such that (ax) % (ay)=x % y(a \cdot x) \ \% \ (a \cdot y) = x \ \% \ y?

Input data

Each test contains multiple test cases. The first line of input contains a single integer TT, representing the number of test cases.

Each test case consists of one line, containing two space-separated integers NN and aa.

Output data

For each test case, print the number of pairs (x,y)(x, y) that satisfy the conditions from the statement.

Constraints and clarifications

  • 1T1 0001 \leq T \leq 1 \ 000
  • 1N,a1091 \leq N, a \leq 10^9
# Score Constraints
0 0 Example
1 10 N,a100N, a \leq 100
2 25 T100, N,a1 000 000T \leq 100, \ N, a \leq 1 \ 000 \ 000
3 10 N,a4 000 000N, a \leq 4\ 000 \ 000
4 40 T100T \leq 100
5 15 No further restrictions.

Example

stdin

6
4 2
13 1
7 6
9 8
1 1000
1000000000 30030

stdout

8
169
16
23
1
20877697634

Explanation

In the first test case, the pairs that satisfy the conditions from the statement are: (1,1)(1,1), (2,2)(2,2), (2,1)(2,1), (3,3)(3,3), (3,1)(3,1), (4,1)(4,1), (4,2)(4,2) and (4,4)(4,4).

In the second test case, all 132=16913^2=169 pairs satisfy the conditions from the statement.

Log in or sign up to be able to send submissions!