Hallucinogenic Carnations

No attempts yetTime limit1sMemory limit128 MB

Problem

A farmer has grown hallucinogenic carnations since time immemorial. The carnations grow on a rectangular field divided into equal square parcels. Each parcel grows a certain number of carnations and, remarkably, the very same parcel always grows the very same number of carnations year after year.

On the first day of summer each year the farmer starts the harvest. He draws a polygon on the field. Every vertex of the polygon has integer coordinates, and each side is parallel, perpendicular, or slanted at 4545, 135135, 225225, or 315315 degrees relative to the sides of the field. The polygon is simple: two of its sides share a point only when they are adjacent.

Using this polygon, the farmer harvests the carnations of every parcel at least half of whose area lies inside the polygon. Whatever is not harvested is happily eaten by all sorts of running, flying, and smelly creatures.

How many carnations does the farmer harvest in each successive year?

Input

The first line contains the number of tests dd (1d101 \le d \le 10).

The first line of each test contains the field width nn, the field length mm (1n,m10001 \le n, m \le 1000), and the number of observed years kk (1k100001 \le k \le 10000). Each of the next mm lines contains nn integers gi,jg_{i,j} (i=1mi = 1 \dots m, j=1nj = 1 \dots n, 0gi,j2160 \le g_{i,j} \le 2^{16}): the number of carnations growing on the parcel with coordinates (i,j)(i, j).

Coordinates work as follows. The field occupies 0xn0 \le x \le n and 0ym0 \le y \le m, and parcel (i,j)(i, j) is the unit square whose opposite corners are (j1, i1)(j-1,\ i-1) and (j, i)(j,\ i). Thus jj is the horizontal (xx) coordinate and ii is the vertical (yy) coordinate, and the first of the mm lines corresponds to i=1i = 1 (that is, 0y10 \le y \le 1).

The next kk lines describe the polygon drawn in each successive year. Each polygon starts with its number of vertices ll (3l1003 \le l \le 100), followed by the coordinates x,yx, y (0xn0 \le x \le n, 0ym0 \le y \le m) of its consecutive vertices.

Output

For each year of each test, print on its own line the number of carnations the farmer harvested.

Hint

The figure below illustrates the situation described above.