Two Tables
Contest: Educational Codeforces Round 112 (Rated for Div. 2)
Difficulty rating: 1300
Tags: brute force

You have an axis-aligned rectangle room with width $$$W$$$ and height $$$H$$$, so the lower left corner is in point $$$(0, 0)$$$ and the upper right corner is in $$$(W, H)$$$.

There is a rectangular table standing in this room. The sides of the table are parallel to the walls, the lower left corner is in $$$(x_1, y_1)$$$, and the upper right corner in $$$(x_2, y_2)$$$.

You want to place another rectangular table in this room with width $$$w$$$ and height $$$h$$$ with the width of the table parallel to the width of the room.

The problem is that sometimes there is not enough space to place the second table without intersecting with the first one (there are no problems with tables touching, though).

You can't rotate any of the tables, but you can move the first table inside the room.

Example of how you may move the first table.

What is the minimum distance you should move the first table to free enough space for the second one?

Input format:
The first line contains the single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of the test cases.

The first line of each test case contains two integers $$$W$$$ and $$$H$$$ ($$$1 \le W, H \le 10^8$$$) — the width and the height of the room.

The second line contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$0 \le x_1 < x_2 \le W$$$; $$$0 \le y_1 < y_2 \le H$$$) — the coordinates of the corners of the first table.

The third line contains two integers $$$w$$$ and $$$h$$$ ($$$1 \le w \le W$$$; $$$1 \le h \le H$$$) — the width and the height of the second table.

Output format:
For each test case, print the minimum distance you should move the first table, or $$$-1$$$ if there is no way to free enough space for the second table.

Your answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
The configuration of the first test case is shown in the picture. But the movement of the first table is not optimal. One of the optimal movement, for example, is to move the table by $$$(0, -1)$$$, so the lower left corner will move from $$$(2, 1)$$$ to $$$(2, 0)$$$. Then you can place the second table at $$$(0, 3)-(4, 5)$$$.

In the second test case, there is no way to fit both tables in the room without intersecting.

In the third test case, you can move the first table by $$$(0, 2)$$$, so the lower left corner will move from $$$(0, 3)$$$ to $$$(0, 5)$$$.

Example input:
5
8 5
2 1 7 4
4 2
5 4
2 2 5 4
3 3
1 8
0 3 1 6
1 5
8 1
3 0 6 1
5 1
8 10
4 5 7 8
8 5

Example output:
1.000000000
-1
2.000000000
2.000000000
0.000000000

Editorial:
Firstly, let's notice the next property: if two axis-aligned rectangles don't intersect, then we can draw a vertical or horizontal line between them. In other words, either $$$\max(x_1, x_2) \le \min(x_3, x_4)$$$ or $$$\max(x_3, x_4) \le \min(x_1, x_2)$$$ if $$$x_1$$$ and $$$x_2$$$ are coordinates of the one rectangle and $$$x_3$$$ and $$$x_4$$$ of the other one (analogically, for $$$y$$$ coordinates).
Now, suppose you want to move the first table by $$$(dx, dy)$$$. Note that if in result they will be divided by vertical line then we can set $$$dy = 0$$$ — they still will be divided, but the total distance will decrease. Analogically, if divided by horizontal line, we can set $$$dx = 0$$$.
In other words, it's always optimal to move the table either horizontally or vertically.
Let's look at the case of horizontal move: at first, we need to check that both tables can fit in the room, or their total width $$$w + (x_2 - x_1) \le W$$$. If yes, then we calculate the movement distance $$$dx$$$ as follows: if we move the table right then there should be at least $$$w$$$ to the left of it, or $$$w \le x_1 + dx$$$ $$$\Leftrightarrow$$$ $$$dx \ge w - x_1$$$. Since we want to minimize $$$dx$$$ then we take $$$dx = \max(0, w - x_1)$$$.
If we want to move the table left, then there should be at least $$$w$$$ to the right, or $$$x_2 - dx \le W - w$$$ $$$\Leftrightarrow$$$ $$$dx \ge x_2 - (W - w)$$$, minimizing $$$dx$$$ means taking $$$dx = \max(0, x_2 - (W - w))$$$. So, the result is $$$\min(\max(0, w - x_1), \max(0, x_2 - (W - w)))$$$.
The vertical case can be handled in the same manner, if $$$h + (y_2 - y_1) \le H$$$ then the result is $$$\min(\max(0, h - y_1), \max(0, y_2 - (H - h)))$$$.
The answer is the minimum among all possible variants, or $$$-1$$$ if both cases are impossible.