Two Windows on the Screen
You're building a window manager that reports how much of the screen is covered by windows, for a compositor that only ever has at most two windows open at once. Each window is an axis-aligned rectangle, given by two opposite corners in pixel coordinates — but the corners for a given window are not guaranteed to come in any particular order (either corner may be the top-left, bottom-left, and so on).
For each query, you're given both windows. Report the total number of pixels
covered by at least one of them — the area of their union. Windows may touch,
overlap, nest inside one another, coincide exactly, or be degenerate (a window
whose two corners share an x or a y coordinate has zero area, like a line
or a single point drawn on the screen).
Input
The first line contains an integer q (1 ≤ q ≤ 10^5) — the number of
queries.
Each of the next q lines describes one query with eight integers:
x1 y1 x2 y2 x3 y3 x4 y4
(x1, y1) and (x2, y2) are two opposite corners of the first window, and
(x3, y3) and (x4, y4) are two opposite corners of the second window. Every
coordinate satisfies 0 ≤ coordinate ≤ 10^9. Within a window the two given
corners are not necessarily ordered (you may have x1 > x2 or y1 > y2, and
likewise for the second window).
Output
For each query, print a single integer on its own line: the area of the union of the two windows, in pixels.
Notes
If the two windows don't overlap in x or don't overlap in y (including
when they merely touch along an edge with zero-width overlap), the overlap
area is zero and the answer is simply the sum of the two windows' areas.