eolymp
bolt
Try our new interface for solving problems

Tower

Alan loves to construct the towers of building bricks. His towers consist of many cuboids with square base. All cuboids have the same height \textbf{h = 1}. Alan puts the consecutive cuboids one over another: \includegraphics{https://static.e-olymp.com/content/72/72b30416c307391dffa9acee9ce6568acee47cb3.jpg} \textit{\textbf{Figure 1}}: Tower of three bricks when Alan fixes \textbf{a_2 = 2}. Recently in math class, the concept of volume was introduced to Alan. Consequently, he wants to compute the volume of his tower now. The lengths of cuboids bases (from top to bottom) are constructed by Alan in the following way: \begin{enumerate} \item Length \textbf{a_1} of the first square is one. \item Next, Alan fixes the length \textbf{a_2} of the second square. \item Next, Alan calculates the length \textbf{a_n} (\textbf{n} > \textbf{2}) by \textbf{2a_2a_\{n-1\} - a_\{n-2\}}. Do not ask why he chose such a formula; let us just say that he is a really peculiar young fellow. \end{enumerate} \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} For example, if Alan fixes \textbf{a_2 = 2}, then \textbf{a_3 = 8 - a_1 = 7}; see \textit{\textbf{Figure 1}}. If Alan fixes \textbf{a_2 = 1}, then \textbf{a_n = 1} holds for all \textbf{n} \textbf{N}; see \textit{\textbf{Figure 2}}. Now Alan wonders if he can calculate the volume of tower of \textbf{N} consecutive building bricks. Help Alan and write the program that computes this volume. Since it can be quite large, it is enough to compute the answer modulo given natural number \textbf{m}. \InputFile The input contains several test cases. The first line contains the number \textbf{t} (\textbf{t} ≤ \textbf{10^5}) denoting the number of test cases. Then \textbf{t} test cases follow. Each of them is given in a separate line containing three integers \textbf{a_2}, \textbf{N}, \textbf{m} (\textbf{1} ≤ \textbf{a_2}, \textbf{m} ≤ \textbf{10^9}, \textbf{2} ≤ \textbf{N} ≤ \textbf{10^9}) separated by a single space, where \textbf{a_2} denotes the fixed length of second square in step \textbf{2}, while \textbf{N} denotes the number of bricks constructed by Alan. \OutputFile For each test case (\textbf{a_2}, \textbf{N}, \textbf{m}) compute the volume of tower of \textbf{N} consecutive bricks constructed by Alan according to steps (\textbf{1}-\textbf{3}) and output its remainder modulo \textbf{m}. \includegraphics{https://static.e-olymp.com/content/42/428a0cbf2f865ec2cc6f7d4c9dcf14299172a845.jpg} \textit{\textbf{Figure 2}}: Tower of four bricks when Alan fixes \textbf{a_2 = 1}.
Time limit 1 second
Memory limit 64 MiB
Input example #1
3
2 3 100
1 4 1000
3 3 1000000000
Output example #1
54
4
299
Source Central European Programming Contest 2008, Wroclaw, Poland, November 28-30, 2008