The sum of the points of the remaining cards in a deck of playing cards after removing the jokers can be easily calculated as follows: $$$\dfrac{4 \times 13 \times (13+ 1)}{2} = 364$$$. We define numbers like $$$364$$$ as Poker Numbers, as they can be written as $$$K \cdot \dfrac{X(X+1)}{2}$$$ for some $$$K\ge 1, X \ge 2$$$.
Your task is to solve the following problem. Given $$$n, c, p$$$, compute:
$$$$$$ \left( \sum\limits_{u \in S_n}c^{u} \right) \bmod p $$$$$$
where $$$S_n$$$ denotes the set of Poker Numbers between $$$1$$$ and $$$n$$$.
The first line contains an integer $$$T$$$ ($$$1\le T \le 20$$$), representing the number of test cases.
Each test case consists of a single line containing three integers $$$n,c,p$$$ ($$$1\le n \le 10^{10}$$$, $$$1 \le c \lt p \le 10^{9} + 7$$$). It is guaranteed that $$$p$$$ is a prime.
For each test case, output the answer in a single line.
210 2 100000000720 2 1000000007
1608 1349192
For $$$n=10$$$, $$$S_{10}=\{3,6,9,10\}$$$, so for the first example, the answer is $$$2^3+2^6+2^9+2^{10}=1608$$$.
| Name |
|---|


