Museum Heist: Area of the Shadowy Regions

No attempts yetTime limit1sMemory limit128 MB

Problem

By the year 3002, the notorious Godfather Unlucky Luchiano is planning a heist at the Rectilinia Museum. The museum's walls are impenetrable and every door is guarded, so his men cannot get in from the sides. Fortunately for him, the museum has no roof, so one can drop in from above. He decides to use an ancient device — a catapult — to fling his men over the walls so they land somewhere inside without passing any guard.

There is still one problem: a high-tech laser gun guards the museum. This gun can steer its beam along a rectilinear (axis-aligned) path rather than a straight line, and the beam always travels the shortest possible distance from the gun to its target. Anyone who lands on a spot the beam can reach is destroyed instantly. Again the Godfather is lucky: the museum contains walls and other obstacles that the beam cannot pass through. If his men land in the shadow of these obstacles — a spot no beam can reach — they survive and can disable the gun. Because the catapult is imprecise, the Godfather wants the total area of the shadowy (safe) regions.

Seen from above, the museum is a rectangle containing several obstacles. Each obstacle is a simple polygon whose sides are parallel to the rectangle's sides, and no two obstacle interiors overlap. The laser gun sits in the upper-right corner of the museum. A beam is a sequence of horizontal and vertical segments. When the gun picks a target, it chooses a valid path and fires. A valid path satisfies:

  • It consists only of horizontal and vertical segments.
  • It never enters the interior of an obstacle, though it may run tangent to obstacle sides. It is never tangent to two obstacle sides at the same point.
  • Traveling from the gun to the target, the beam never moves from left to right and never moves from bottom to top (directions are as seen from above).

Compute the total area of the shadowy regions — the parts of the museum that lie outside every obstacle yet no beam can reach. In the figure above, the point marked (×) lies in a shadowy region.

Input

The first line contains an integer tt (1t101 \le t \le 10), the number of test cases. Each test case is given as follows.

  • The first line contains two positive integers: the length and the width of the rectangle (its extent along the xx-axis and the yy-axis, respectively).
  • The second line contains an integer nn (0n500 \le n \le 50), the number of obstacles.
  • Each of the next nn lines describes one obstacle: an integer mm (4m504 \le m \le 50), the number of vertices, followed by 2m2m integers — the xx and yy coordinates of the vertices in clockwise order.

Every coordinate is a non-negative integer less than 1,000,000. The upper-left corner of the rectangle is the origin, the xx-axis points right, and the yy-axis points down.

Output

For each test case, print a single line containing one integer: the total area of the safe (shadowy) regions — the region inside the museum, outside every obstacle, that the laser gun cannot reach. Each answer is guaranteed to fit in a 32-bit integer.