Under construction

Still being built. Things may change

Big Table

Time limit4sMemory limit128 MB

Summary
Each row of a huge table repeats a short digit period; answer rectangle-sum queries without materializing the table.
Level

Medium6 of 10

Topics
Prefix sum, Math, Implementation, Array
Solved
No attempts yet

Problem

Mirko and Slavko are always playing some game. They got bored of every existing one, so they decided to invent their own. Mirko imagines a large table of single-digit numbers and writes it down, and Slavko has to answer questions of the form "What is the sum of the numbers in this rectangle?" as fast as he can.

So that Mirko does not have to write out every number of the imagined table, the two agreed on the following. Each row of the table is built by concatenating the row's base period many times and then discarding elements from the end until the row is exactly as wide as the table.

Input

The first line contains RR and SS (1≤R,S≤100 0001 \le R, S \le 100\,000), the number of rows and the number of columns of the table.

The next RR lines contain the base periods of the rows, from the top row down. Each period consists of at most min⁡(S,100)\min(S, 100) digits from 0 to 9, written one after another without spaces.

The next line contains QQ (1≤Q≤100 0001 \le Q \le 100\,000), the number of questions Slavko has to answer.

Each of the following QQ lines contains four integers r1,s1,r2,s2r_1, s_1, r_2, s_2 (1≤r1≤r2≤R1 \le r_1 \le r_2 \le R and 1≤s1≤s2≤S1 \le s_1 \le s_2 \le S) that describe a rectangle in which Slavko has to find the sum of the numbers. r1r_1 and s1s_1 give the top-left cell of the rectangle (row r1r_1, column s1s_1), and r2r_2 and s2s_2 give the bottom-right cell. Rows are numbered 1 to RR from top to bottom, and columns are numbered 1 to SS from left to right.

Output

Print QQ lines with Slavko's answers in the order of the questions, exactly one number per line.

Hint

The periods in the first sample input describe this table:

1111111111
0404040404
1231231231
9898989898

Slavko has to answer a single question, which asks for the sum of all numbers in the table. That sum is 134.

Examples2

  1. Example 1

    Input
    4 10
    1
    04
    123
    98
    1
    1 1 4 10
    
    Expected output
    134
    
  2. Example 2

    Input
    4 3
    5
    0
    330
    405
    2
    1 1 4 1
    1 1 3 1
    
    Expected output
    12
    8