Slate Modern (Large)

Fill a huge R by C grid with positive integers so adjacent cells differ by at most D, matching N fixed cells, maximizing the total sum or reporting impossibility.

Hard9GraphShortest pathMathImplementationNo attempts yetTime limit80sMemory limit512 MB

Problem

The Slate Modern gallery shows only grayscale paintings. Every painting in the gallery must be a grid with RR rows and CC columns, and each cell is painted with a positive integer brightness value. So that the art is not too startling, two cells that share an edge must have brightness values that differ by at most DD. Cells that touch only at a corner are not restricted.

Last night your artist friend Cody-Jamal picked NN different cells and filled each of them with a positive integer brightness value. You told him the gallery rules only today, and now he wants to know whether the remaining cells can all be filled with positive integer brightness values so that the painting obeys the rules. If that is possible, he wants the sum of all brightness values to be as large as possible, to save his black paint. The sum can be very large, so report it modulo the prime 109+710^9+7.

Input

The first line contains the number of test cases TT.

The first line of each test case contains four integers RR, CC, NN, and DD. Then NN lines follow. The ii-th of them contains three integers RiR_i, CiC_i, and BiB_i, which mean that the cell in row RiR_i and column CiC_i already has brightness value BiB_i. Rows and columns are numbered starting from 1.

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1. If the painting cannot be completed, yy is IMPOSSIBLE. Otherwise yy is the maximum possible sum of all brightness values modulo 109+710^9+7.

Constraints

  • 1T1001 \le T \le 100
  • 1R1091 \le R \le 10^9
  • 1C1091 \le C \le 10^9
  • 1N2001 \le N \le 200
  • 1D1091 \le D \le 10^9
  • 1RiR1 \le R_i \le R, 1CiC1 \le C_i \le C, and 1Bi1091 \le B_i \le 10^9 for all ii. The upper bound of 10910^9 applies only to the cells Cody-Jamal already painted. You may assign brightness values larger than 10910^9 to the other cells.
  • N<R×CN < R \times C, so at least one cell is empty.
  • RiRjR_i \ne R_j or CiCjC_i \ne C_j for all iji \ne j, so the given cells are all different.

Notes

In test case 1 of the first example, one optimal painting is shown below, and its sum is 40.

6 7 9
4 6 8

In test case 2 the best painting is 2000000000 1000000000, whose sum is 3000000000. Modulo 109+710^9+7 that is 999999986.

Test case 3 cannot be completed. Whatever value you choose for the cell in row 2, it differs too much from at least one of the two painted cells above and below it.

In test case 4 the two cells Cody-Jamal already painted have brightness values that are too far apart, so the painting cannot be continued.