Ski Routes

No attempts yetTime limit10sMemory limit128 MB

Problem

A mountainous region is described by a grid of numbers with NN rows and MM columns, where each entry is the terrain height at that grid point. To serve skiers, KK ski lifts have been built between selected pairs of points. Every lift works in one direction only, carrying a skier from a lower point up to a higher point.

A ski route consists of a non-empty sequence of lift rides, during which the skier's height keeps increasing, followed by a sequence of descents that returns to the point where the route began. Consecutive lift rides must connect: each ride starts at the point where the previous one ended. Each descent is a single move to an orthogonally adjacent cell (one of the four directions), and every descent must go from a higher cell to a strictly lower one.

The resorts want to advertise the slogan: "We have XX ski routes." Given the list of lifts and the height of every point, compute XX modulo 109+710^9 + 7.

Input

The first line contains an integer ZZ (1Z101 \le Z \le 10), the number of test cases. The test cases follow.

The first line of each test case contains two space-separated integers NN and MM (1N,M1001 \le N, M \le 100). The next NN lines describe the height table row by row, from row 11 to row NN; each of these lines contains MM integers hh (0h1090 \le h \le 10^9), listed from column 11 to column MM.

The next line contains an integer KK (1K3001 \le K \le 300), the number of lifts. Each of the following KK lines contains four integers w0 c0 w1 c1w_0\ c_0\ w_1\ c_1, describing a lift from point (w0,c0)(w_0, c_0) to point (w1,c1)(w_1, c_1). Rows are numbered from 11 to NN and columns from 11 to MM. Every lift ends strictly higher than it starts. Several lifts may connect the same pair of points.

Output

For each test case, print on a separate line the number of ski routes modulo 109+710^9 + 7.

Hint

Here (r,c)(r, c) denotes the cell in row rr and column cc.

For the first sample, the possible routes are:

  • (1,1)(1,2)(1,1)(1,1) \to (1,2) \to (1,1)
  • (1,1)(1,2)(2,2)(1,2)(1,1)(1,1) \to (1,2) \to (2,2) \to (1,2) \to (1,1)
  • (1,1)(1,2)(2,2)(2,1)(1,1)(1,1) \to (1,2) \to (2,2) \to (2,1) \to (1,1)
  • (1,1)(2,1)(1,1)(1,1) \to (2,1) \to (1,1)
  • (1,2)(2,2)(1,2)(1,2) \to (2,2) \to (1,2)

The second sample shows that several lifts may connect the same two points.