Don't Break the Nile (Large)

Compute the maximum unit flow from the south edge to the north edge of a grid blocked by up to 1000 rectangles.

Hard8Shortest pathGraphGeometryNo attempts yetTime limit5sMemory limit512 MB

Problem

Aliens have landed. Their home planet has no flowing water at all, so they find the rivers of Earth interesting, and now they want to put up their buildings in some of those rivers. Your job is to check that the buildings do not block the rivers too much, because a blocked river causes serious trouble. Given the placement of the buildings, determine the maximum flow the river sustains.

The aliens prefer stretches of river that are straight and of uniform width, so you model the river as a rectangular grid. Each cell has integer coordinates (X,Y)(X, Y) with 0X<W0 \le X < W and 0Y<H0 \le Y < H. Each cell passes 1 unit of flow, and water moves between two cells that share an edge. Every cell on the south side of the river, meaning every cell with yy coordinate 00, receives 1 unit of incoming flow. All buildings are rectangles aligned with the grid, and a cell under a building passes no flow at all.

Under these rules, determine the maximum flow that reaches the north side of the river, meaning the cells with yy coordinate H1H-1.

Input

The first line contains the number of test cases, TT. TT test cases follow.

The first line of each test case contains three integers: the width of the river WW, the height of the river HH, and the number of buildings placed in the river BB. Each of the next BB lines contains four integers X0X_0, Y0Y_0, X1X_1, Y1Y_1. (X0,Y0)(X_0, Y_0) is the lower left corner of the building and (X1,Y1)(X_1, Y_1) is the upper right corner. Buildings do not overlap, although two buildings can share an edge.

Limits

  • 1T1001 \le T \le 100
  • 3W10003 \le W \le 1000
  • 3H1083 \le H \le 10^8
  • 0B10000 \le B \le 1000
  • 0X0X1<W0 \le X_0 \le X_1 < W
  • 0Y0Y1<H0 \le Y_0 \le Y_1 < H

Output

For each test case, print one line in the form Case #x: m, where xx is the test case number starting from 1 and mm is the maximum flow that passes through the river.

Hint

The two pictures below draw the two test cases of the first example.