Mr. Ham learned about computational complexity in the algorithm course. Let $$$T\left(n\right)$$$ be the time the algorithm takes to run on input size $$$n$$$. For example, for the merge sort algorithm, we have the following recursion equation, $$$$$$T\left(n\right)=2T\left(\left \lfloor \frac{n}{2}\right \rfloor\right)+O\left(n\right).$$$$$$ And we can get the upper bound $$$T\left(n\right)=O\left(n\log n\right)$$$ from the algorithm textbook.
Mr. Ham is a good kid who loves to learn and explore, so he decided to try a harder problem. Consider two algorithms $$$A_1\left(n\right)$$$ and $$$A_2\left(n\right)$$$ that call each other. They satisfy the following calling relationship: $$$$$$A_1\left(n\right) \text{ calls } A_2\left(\left \lfloor \frac{n}{2}\right \rfloor\right), A_2\left(\left \lfloor \frac{n}{3}\right \rfloor\right), A_2\left(\left \lfloor \frac{n}{5}\right \rfloor\right)\text{ and } A_2\left(\left \lfloor \frac{n}{7}\right \rfloor\right),$$$$$$ $$$$$$A_2\left(n\right) \text{ calls } A_1\left(\left \lfloor \frac{n}{2}\right \rfloor\right), A_1\left(\left \lfloor \frac{n}{3}\right \rfloor\right), A_1\left(\left \lfloor \frac{n}{4}\right \rfloor\right)\text{ and } A_1\left(\left \lfloor \frac{n}{5}\right \rfloor\right),$$$$$$ Mr. Ham wants to know the precise time taken by both algorithms.
The problem can be formally stated as follows:
Let $$$f\left(n\right)$$$ be the number of operations required by $$$A_1\left(n\right)$$$, and $$$g\left(n\right)$$$ be the number of operations required by $$$A_2\left(n\right)$$$. They satisfy the following recursion relationship $$$$$$f\left(n\right)=\max \left(n, g\left(\left \lfloor \frac{n}{2}\right \rfloor\right)+ g\left(\left \lfloor \frac{n}{3}\right \rfloor\right)+ g\left(\left \lfloor \frac{n}{5}\right \rfloor\right)+ g\left(\left \lfloor \frac{n}{7}\right \rfloor\right)\right),$$$$$$ $$$$$$g\left(n\right)=\max \left(n, f\left(\left \lfloor \frac{n}{2}\right \rfloor\right)+ f\left(\left \lfloor \frac{n}{3}\right \rfloor\right)+ f\left(\left \lfloor \frac{n}{4}\right \rfloor\right)+ f\left(\left \lfloor \frac{n}{5}\right \rfloor\right)\right).$$$$$$ Given the values of $$$f\left(0\right), g\left(0\right)$$$ and $$$m$$$, Mr. Ham wants to know what $$$f\left(m\right)$$$ and $$$g\left(m\right)$$$ are, and the result is modulo $$$M$$$.
Note that $$$ \left \lfloor x\right \rfloor $$$ represents the largest integer not exceeding $$$x$$$, such as $$$ \left \lfloor 0.5\right \rfloor =0$$$, $$$ \left \lfloor 11.3\right \rfloor =11$$$, $$$ \left \lfloor 101.9\right \rfloor =101$$$, $$$ \left \lfloor 99\right \rfloor =99$$$, $$$ \left \lfloor 0\right \rfloor =0$$$, $$$ \left \lfloor 2\right \rfloor =2$$$.
The first line contains four numbers, namely $$$f(0)$$$, $$$g(0)$$$, $$$T$$$, $$$M$$$ ($$$0\le f(0), g(0), T \le 10^5, 2\le M \le 10^{15}$$$),
Each of the next $$$T$$$ lines contains a integer $$$m$$$ ($$$0\le m \le 10^{15}$$$) querying the values of $$$f(m)$$$ modulo $$$M$$$ and $$$g(m)$$$ modulo $$$M$$$.
Output $$$T$$$ lines, each line contains two numbers $$$f(m)$$$ modulo $$$M$$$ and $$$g(m)$$$ modulo $$$M$$$, separated by spaces.
1958 920 10 100000000000 0 1 2 3 10 100 200 1000 19580920 20232023
1958 920 3680 7832 10592 9554 17504 11276 50294 64826 784112 893714 1894550 1905470 12057866 12979424 71481494756 48626708512 28127864908 7251681354
0 0 10 100000000000 0 1 2 3 4 10 20 30 40 100
0 0 1 1 2 2 3 3 4 4 11 12 25 26 41 41 55 58 162 172