Laser Pool

No attempts yetTime limit5sMemory limit256 MB

Problem

The people of Byteotia love a game called laser pool. The table is an n×mn \times m rectangle. Its rails are 14\tfrac{1}{4} wide and hold n+mn + m pairs of laser transmitters. When every transmitter is switched on, the table is covered by nn horizontal and mm vertical laser beams, arranged so that the ii-th horizontal beam (for 1in1 \le i \le n) meets the jj-th vertical beam (for 1jm1 \le j \le m) at the point (j12, i12)\left(j - \tfrac{1}{2},\ i - \tfrac{1}{2}\right). Each transmitter can be turned on or off independently.

The game uses a ball of diameter 12\tfrac{1}{2}. Whenever the ball touches at least one lit laser beam, a single hit signal is shown; touching several beams at the very same instant still counts as one signal.

At the start the ball is centered at (x12, y12)\left(x - \tfrac{1}{2},\ y - \tfrac{1}{2}\right). It is struck so that its initial velocity vector is (xv,yv)(x_v, y_v), and it then rolls without friction for tt units of time. Every collision with a rail is perfectly elastic. How many times is the hit signal shown, counting the initial moment of the ball's motion as well?

Input

The first line contains two integers nn and mm (3n,m1000003 \le n, m \le 100\,000), the dimensions of the table.

The second line is a string of nn characters, each either 00 or 11. Its ii-th character is the state of the ii-th horizontal transmitter, where 00 means off and 11 means on.

The third line is a string of mm characters describing the vertical transmitters in the same way.

The fourth line contains an integer kk (1k100001 \le k \le 10\,000), the number of queries. Each of the next kk lines contains five integers xx, yy, xvx_v, yvy_v, tt (1<x<m1 < x < m, 1<y<n1 < y < n, xv,yv{1,1}x_v, y_v \in \{-1, 1\}, 1t1091 \le t \le 10^9): the starting position of the ball, its velocity, and how long it rolls.

Output

Print exactly kk lines. The ii-th line must contain one integer: the number of times the hit signal is shown for the ii-th query.

Hint