Let two functions be the following:
$$ f(n) = a_1 \times f(n-1) + b_1 \times f(n-2) + c_1 \times g(n-3) \\ g(n) = a_2 \times g(n-1) + b_2 \times g(n-2) + c_2 \times f(n-3) $$
Find fn % M and gn % M. (% stands for the modulo operation.)
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case starts with a blank line. Next line contains three integers a1 b1 c1 (0 ≤ a1, b1, c1 < 25000). Next line contains three integers a2 b2 c2 (0 ≤ a2, b2, c2 < 25000). Next line contains three integers f0 f1 f2 (0 ≤ f0, f1, f2 < 25000). Next line contains three integers g0 g1 g2 (0 ≤ g0, g1, g2 < 25000). The next line contains an integer M (1 ≤ M < 25000).
Next line contains an integer q (1 ≤ q ≤ 100) denoting the number of queries. Next line contains q space separated integers denoting n. Each of these integers is non-negative and less than 231.
Output
For each case, print the case number in a line. Then for each query, you have to print one line containing fn % M and gn % M.
Sample
Sample Input | Sample Output |
---|---|
2 1 1 0 0 0 0 0 1 1 0 0 0 20000 10 1 2 3 4 5 6 7 8 9 10 1 1 1 1 1 1 2 2 2 2 2 2 20000 5 2 4 6 8 10 | Case 1: 1 0 1 0 2 0 3 0 5 0 8 0 13 0 21 0 34 0 55 0 Case 2: 2 2 10 10 34 34 114 114 386 386 |