K-dimensional partial sum#692

K-dimensional partial sum

10 seconds
64 megabytes
Easy

Given a -dimensional array of numbers a_{i_1,i_2,\ldots,i_k}, where 1 \leq i_j \leq n_j for each from to . For given l_1, \ldots, l_k, r_1, \ldots, r_k, determine the following:

Input

The first line contains the integer (1 \leq k \leq 6).

The second line contains the dimensions of the array: n_j (1 \leq \prod n_j \leq 10^6).

Then, the array numbers are given in n_k rows, each not exceeding , describing the array.

The next line contains the integer (1 \leq q \leq 10^6) — the number of queries.

Each of the following lines contains a query: l_1, \ldots, l_k, r_1, \ldots, r_k (1 \leq l_j \leq r_j \leq n_j).

Output

Print the answer for each query on a separate line.

Examples

Example 1

Loading
Loading