Since the latest Great War, the Galaxy has been ruled by the Galatic Empire. Lord Edsidious was finally able to conquer the status of Emperor, after centuries of planning. In his Empire, the following definitions and conventions apply:
To maintain his dominance, Lord Edsidious hired the famous botanic Collatz (the creator of the Collatz Function) to build him a Collatz-Star of Pain and Suffering, which is a massive device able to exterminate whole planets with a single blast. And to protect this strategic weapon, he assigned his most prestigious general: Darth Caetanus. Caetanus recently became aware of a weakness in the Star: it's energy board.
The energy board is composed of independent cells disposed in a $$$H$$$ x $$$W$$$ grid, and it is associated with a fixed integer $$$M \gt 2$$$. A cell $$$(i, j)$$$, at any time $$$t$$$ (measured in some integer unit) has two positive integers associated with it: $$$A_{t,i,j}$$$ and $$$B_{t,i,j}$$$. These numbers describe the probability of a failure in the cell at that time, which is equal to $$$\frac{A'_{t,i,j}}{B'_{t,i,j}}$$$, where $$$B'_{t,i,j} = NCP(SSF(B_{t,i,j}, M, 2), M)$$$ and $$$A'_{t,i,j} = SSF(A_{t,i,j}, B'_{t,i,j}, 1)$$$. At any time, if a failure occurs in any cell, it shuts down, leaving the Collatz-Star with no power temporarily. These values $$$A$$$ and $$$B$$$ vary in time as follows: $$$A_{t+1,i,j} = CF(A_{t,i,j})$$$ and $$$B_{t+1,i,j} = CF(B_{t,i,j})$$$.
Vinicius Solo, an influent person in the fallen Galatic Republic, used his connections to find (at time $$$0$$$) all the numbers $$$A_{0,i,j}$$$ and $$$B_{0,i,j}$$$ and the fixed number $$$M$$$. He also became aware that, at a certain unknown time $$$T$$$, Lord Edsidius will order the Collatz-Star to fire against the poor U.n.Balloon planet. To prevent its destruction, Vinicius will need the help of his ally Daniel Skywalker, who can Mystic Focus on subgrids of the energy board grid.
Let's consider that Daniel tries to Mystic Focus on a subgrid $$$S$$$ in a period of time from $$$t_l$$$ to $$$t_r$$$ (inclusive), and let $$$Prob(S, t_l, t_r)$$$ be the probability that any cell in $$$S$$$ shuts down at any time between $$$t_l$$$ and $$$t_r$$$ (which means the Collatz-Star would lose power in this time interval). Daniel can explode the cells in $$$S$$$ using his ability, but only if the value of $$$Prob(S, t_l, t_r)$$$ mod $$$M$$$ is greater than $$$\lfloor \frac{M}{2} \rfloor$$$ (it can be proven $$$Prob(S, t_l, t_r)$$$ is always representable by a fraction $$$\frac{P}{Q}$$$ with $$$Q$$$ coprime to $$$M$$$). This explosion would provoke a chain reaction that would destroy the whole Collatz-Star!
However, saving U.n.Balloon will not be that easy. To protect the Collatz-Star, Darth Caetanus casted a spell which prevents Daniel from trying to Mystic Focus on subgrids $$$S$$$ not contained in a special unknown subgrid $$$K$$$. Also, if Daniel tries to Mystic Focus on some subgrid $$$S$$$, he will be forced to do so from $$$t_l = S_{x1} + S_{y1} + K_{x1} + K_{y1}$$$ to $$$t_r = T - (S_{x2} + S_{y2} + K_{x2} + K_{y2})$$$.
Vinicius and Daniel don't know what is the actual time $$$T$$$ or the special subgrid $$$K$$$, so they decided to prepare themselves. They will conduct $$$Q$$$ studies: in the study $$$i$$$, they will count, for chosen values of $$$T_i$$$ and $$$K_i$$$, the number of subgrids $$$S_i$$$ that Daniel would be able to Mystic Focus on and explode, causing the destruction of the Collatz-Star. Help them write a program that calculates this!
The first line of input contains the integers $$$H$$$, $$$W$$$ ($$$1 \leq H, W \leq 30$$$) and $$$M$$$ ($$$2 \lt M \leq 10^5$$$).
The next $$$H$$$ lines contain $$$W$$$ integers each: the values $$$A_{0,i,j}$$$ ($$$0 \lt A_{0,i,j} \leq 10^5$$$).
The other next $$$H$$$ lines also contain $$$W$$$ integers each: the values $$$B_{0,i,j}$$$ ($$$0 \lt B_{0,i,j} \leq 10^5$$$).
The next line of input contains the integer $$$Q$$$ ($$$1 \leq Q \leq 100$$$).
Finally, the next $$$Q$$$ lines contain five integers each: $$$T_i$$$ ($$$0 \leq T_i \leq 10^5$$$) and the $$$x1$$$, $$$y1$$$, $$$x2$$$ and $$$y2$$$ (in this order) related to $$$K_i$$$ ($$$1 \leq x1, x2 \leq H$$$, $$$1 \leq y1, y2 \leq W$$$).
Print $$$Q$$$ lines: in the $$$i$$$-th line, the number of subgrids $$$S_i$$$ that Daniel would be able to Mystic Focus on and explode, considering the given $$$T_i$$$, $$$K_i$$$ and associated $$$t_l$$$ and $$$t_r$$$ for each subgrid (as described by the statement).
3 3 912 31 4620 35 2891 54 1811 14 1610 15 7727 15 13411 1 1 2 230 1 3 2 30 1 1 3 38 1 1 1 1
1 0 0 1
3 3 355 83 6373 62 9595 97 2763 55 4195 47 6395 54 47430 2 3 3 320 3 3 3 324 1 2 3 236 2 1 3 2
0 0 4 4
Let's understand the first study of the first test case, with $$$T = 11$$$ and $$$K = (1, 1, 2, 2)$$$. The value of $$$M$$$ is 9.
With these values, most subgrids have $$$t_l \gt t_r$$$, and thus cannot be targeted by Daniel's Mystic Focus. The only possible ones are $$$S_1 = (1,1,1,1)$$$ ($$$t_l = 4$$$, $$$t_r = 5$$$), $$$S_2 = (1,1,2,1)$$$, and $$$S_3 = (1,1,1,2)$$$ (both with $$$t_l = t_r = 4$$$).
Starting with $$$S_1$$$:
At time $$$4$$$, we can verify that $$$A_{4,1,1} = 5$$$ and $$$B_{4,1,1} = 10$$$. Calculating $$$B'_{4,1,1} = NCP(SSF(10, 9, 2), 9)$$$, we have $$$NCP((8 \ \% \ 7)+2, 9) = NCP(3, 9) = 4$$$. It follows that $$$A'_{t,i,j} = SSF(5, 4, 1) = (4 \ \% \ 3) + 1 = 2$$$ and that $$$Prob(S_1, 4, 4)$$$ is $$$\frac{2}{4}$$$.
At time $$$5$$$, $$$A_{5,1,1} = 4$$$ and $$$B_{5,1,1} = 5$$$. Thus, we calculate $$$B'_{5,1,1} = NCP(SSF(5, 9, 2), 9)$$$ as $$$NCP((3 \ \% \ 7)+2, 9) = NCP(5, 9) = 5$$$ and $$$A'_{t,i,j} = SSF(4, 5, 1)$$$ as $$$(3 \ \% \ 4) + 1 = 4$$$. This results in $$$Prob(S_1, 5, 5) = \frac{4}{5}$$$.
We can use $$$Prob(S_1, t_l = 4, t_r = 5) = Prob(S_1, 4, 4) \cup Prob(S_1, 5, 5)$$$ to calculate the probability as $$$Prob(S_1, 4, 4) + Prob(S_1, 5, 5) - Prob(S_1, 4, 4) \cdot Prob(S_1, 5, 5)$$$. Applying the values, we get $$$Prob(S_1, 4, 5) = \frac{2}{4} + \frac{4}{5} - \frac{2}{4} \cdot \frac{4}{5} = \frac{18}{20}$$$. The value of this fraction mod $$$9$$$ is $$$(18 \cdot 20^{-1}) \ \% \ 9 = (18 \cdot 5) \ \% \ 9 = 0$$$. This is not greater than $$$\lfloor \frac{9}{2} \rfloor = 4$$$, so Daniel cannot use the Mystic Focus here.
Now with $$$S_2$$$:
Let's take $$$S^1_2 = (1,1,1,1)$$$ and $$$S^2_2 = (2,1,2,1)$$$. Then, $$$Prob(S_2, t_l = 4, t_r = 4) = Prob(S^1_2, 4, 4) \cup Prob(S^2_2, 4, 4)$$$. We have already calculated $$$Prob(S^1_2, 4, 4) = Prob(S_1, 4, 4) = \frac{2}{4}$$$.
We have $$$A_{4,2,1} = 1$$$ and $$$B_{4,2,1} = 1$$$. Thus, $$$B'_{4,2,1} = NCP(SSF(1, 9, 2), 9)$$$, which results in $$$NCP(((-1) \ \% \ 7)+2, 9) = NCP(-1+2, 9) = 1$$$, and $$$A'_{4,2,1} = SSF(1, 1, 1)$$$, which gives $$$(0 \ \% \ 0) + 1 = 0 + 1 = 1$$$. With this, we obtain $$$Prob(S^2_2, 4, 4) = \frac{1}{1}$$$. This results in $$$Prob(S_2, t_l = 4, t_r = 4) = \frac{2}{4} + \frac{1}{1} - \frac{2}{4} \cdot \frac{1}{1} = \frac{4}{4}$$$, which has a value of $$$1$$$ in mod $$$9$$$ (still not greater than $$$\lfloor \frac{9}{2} \rfloor = 4$$$).
Lastly, with $$$S_3$$$:
Similarly to $$$S_2$$$, let's take $$$S^1_3 = (1,1,1,1)$$$ and $$$S^2_3 = (1,2,1,2)$$$. $$$A_{4,1,2} = 71$$$, $$$B_{4,1,2} = 34$$$, $$$B'_{4,1,2} = NCP(SSF(34, 9, 2), 9) = NCP(6) = 7$$$ and $$$A'_{4,1,2} = SSF(71, 7, 1) = 5$$$. Then, $$$Prob(S^2_3, 4, 4) = \frac{5}{7}$$$ and $$$Prob(S_3, 4, 4) = \frac{2}{4} + \frac{5}{7} - \frac{2}{4} \cdot \frac{5}{7} = \frac{24}{28}$$$. This has a value of $$$6$$$ in mod $$$9$$$, so Daniel would be able to use the Mystic Focus here!
In total, we find that there is only one subgrid of $$$K$$$ (our $$$S_3$$$) in which Daniel can use his ability. Thus, the result for this study is $$$1$$$.