The Hash Table

아직 제출이 없습니다시간 제한1초메모리 제한512 MB

문제

There is a hash table with mm slots, numbered from 00 to m1m-1. Initially the slots are empty.

There are nn elements, numbered from 00 to n1n-1, which should be inserted into the hash table in this order.

The hash function is h(x)=x2modmh(x)=x^2 \bmod m, so the element number ii will be inserted into the slot numbered (i2modm)(i^2 \bmod m).

Because of the strange implementation, inserting an element into a slot costs TT, where TT is the number of elements this slot already contains. Please compute the total cost of inserting all these nn elements into the table.

입력

The first line contains an integer tt, denoting the number of test cases (1t51 \le t \le 5).

Each test case is given on a single line with two integers, nn and mm (1n1091 \le n \le 10^9, 2m1092 \le m \le 10^9).

출력

For each test case, print a single line containing the answer.

힌트

In the first test case, the elements 0,1,2,3,40,1,2,3,4 are inserted into slots 0,1,0,1,00,1,0,1,0 respectively, incurring costs of 0+0+1+1+2=40+0+1+1+2=4.