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-----

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-----

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}$.


-----Examples-----

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
Output
1.000000000
-1
2.000000000
2.000000000
0.000000000


-----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)$.

Source: codeforces | Difficulty: EASY
Tags: Complete search


Solutions:
import time

def main():
	t = i_input()
	for tt in range(t):
		(W, H) = li_input()
		(x1, y1, x2, y2) = li_input()
		(x1, x2) = (min(x1, x2), max(x1, x2))
		(y1, y2) = (min(y1, y2), max(y1, y2))
		(w, h) = li_input()
		if w <= W - (x2 - x1):
			rx = max(x1, W - x2)
			mx = max(0, w - rx)
		else:
			mx = 10 ** 9
		if h <= H - (y2 - y1):
			ry = max(y1, H - y2)
			my = max(0, h - ry)
		else:
			my = 10 ** 9
		out = min(mx, my)
		print(out if out != 10 ** 9 else -1)

def i_input():
	return int(input())

def l_input():
	return input().split()

def li_input():
	return list(map(int, l_input()))

def il_input():
	return list(map(int, l_input()))
TT = time.time()
main()


for i in range(int(input())):
	(W, H) = list(map(int, input().split()))
	(x1, y1, x2, y2) = list(map(int, input().split()))
	(w, h) = list(map(int, input().split()))
	if w + (x2 - x1) > W and h + (y2 - y1) > H:
		print(-1)
	elif x1 >= w or W - x2 >= w or y1 >= h or (H - y2 >= h):
		print(0)
	else:
		print(min(w - x1 if w + (x2 - x1) <= W else 1000000000, w - W + x2 if w + (x2 - x1) <= W else 1000000000, h - y1 if h + (y2 - y1) <= H else 1000000000, h - H + y2 if h + (y2 - y1) <= H else 1000000000))


for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if w * h > W * H - (x2 - x1) * (y2 - y1) or (w + (x2 - x1) > W and h + (y2 - y1) > H):
		print(-1)
	else:
		maxH = max(y1, H - y2)
		maxW = max(x1, W - x2)
		if maxH == 0 and maxW == 0 or maxH >= h or maxW >= w:
			print(0)
		elif maxW == 0 or x1 + W - x2 < w:
			print(h - maxH)
		elif maxH == 0 or y1 + H - y2 < h:
			print(w - maxW)
		else:
			print(min(h - maxH, w - maxW))


from sys import stdin
input = stdin.readline
rn = lambda : int(input())
rns = lambda : map(int, input().split())
rl = lambda : list(map(int, input().split()))
rs = lambda : input().strip()
YN = lambda x: print('YES') if x else print('NO')
mod = 10 ** 9 + 7
for _ in range(rn()):
	(W, H) = rns()
	(a, b, c, d) = rns()
	(w, h) = rns()
	ans = float('inf')
	if a >= w or b >= h:
		ans = 0
	else:
		if w - a <= W - c:
			ans = min(ans, w - a)
		if h - b <= H - d:
			ans = min(ans, h - b)
	if a >= w or d <= H - h:
		ans = 0
	else:
		if w - a <= W - c:
			ans = min(ans, w - a)
		if b >= d - (H - h):
			ans = min(ans, d - (H - h))
	if W - w >= c or H - h >= d:
		ans = 0
	else:
		if a >= c - (W - w):
			ans = min(ans, c - (W - w))
		if b >= d - (H - h):
			ans = min(ans, d - (H - h))
	if W - w >= c or b >= h:
		ans = 0
	else:
		if a >= c - (W - w):
			ans = min(ans, c - (W - w))
		if h - b <= H - d:
			ans = min(ans, h - b)
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)


t = int(input())
for _ in range(t):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w1, h1) = map(int, input().split())
	a = max(x1, w - x2)
	b = max(y1, h - y2)
	if w1 <= a or h1 <= b:
		print(0)
	elif w - (x2 - x1) < w1 and h - (y2 - y1) < h1:
		print(-1)
	elif w - (x2 - x1) < w1:
		print(h1 - b)
	elif h - (y2 - y1) < h1:
		print(w1 - a)
	else:
		print(min(w1 - a, h1 - b))


def solve():
	for _ in range(int(input())):
		(W, H) = list(map(int, input().split()))
		(x1, y1, x2, y2) = list(map(int, input().split()))
		(w, h) = list(map(int, input().split()))
		ans = float('inf')
		if x2 - x1 + w <= W:
			ans = min(ans, max(0, w - x1))
			ans = min(ans, max(0, x2 - (W - w)))
		if y2 - y1 + h <= H:
			ans = min(ans, max(0, h - y1))
			ans = min(ans, max(0, y2 - (H - h)))
		if ans == float('inf'):
			print('-1')
		else:
			print(ans)
solve()


for _ in range(int(input())):
	(w, h) = map(int, input().split())
	l = [int(i) for i in input().split()]
	(x1, y1, x2, y2) = (l[0], l[1], l[2], l[3])
	mw = x2 - x1
	mh = y2 - y1
	(a, b) = map(int, input().split())
	wdis = w - x2 + x1
	hdis = h - y2 + y1
	if wdis < a and hdis < b:
		print(-1)
		continue
	if w - x2 >= a or x1 >= a or h - y2 >= b or (y1 >= b):
		print(0)
		continue
	res = []
	if mw + a <= w:
		res = [a - (w - x2), a - x1]
	if mh + b <= h:
		res += [b - (h - y2), b - y1]
	print(min(res))
	continue


def func(x, y):
	return round((x ** 2 + y ** 2) ** 0.5, 9)
for u in range(int(input())):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(x, y) = map(int, input().split())
	l = x1
	r = w - x2
	ans = 10 ** 10
	f = False
	if x <= l + r:
		f = True
		if x <= l or x <= r:
			ans = 0
		else:
			temp = min(x - l, x - r)
			ans = min(ans, temp)
	l = y1
	r = h - y2
	if y <= l + r:
		f = True
		if y <= l or y <= r:
			ans = 0
		else:
			temp = min(y - l, y - r)
			ans = min(ans, temp)
	if f:
		print(ans)
	else:
		print(-1)


tests = int(input())
for i in range(tests):
	space = list(map(int, input().split()))
	table1 = list(map(int, input().split()))
	table2 = list(map(int, input().split()))
	tb1szx = table1[2] - table1[0]
	tb1szy = table1[3] - table1[1]
	lowest = float('inf')
	if tb1szx + table2[0] <= space[0]:
		lowest = min(max(0, table2[0] - table1[0]), max(0, table1[2] - (space[0] - table2[0])))
	if tb1szy + table2[1] <= space[1]:
		lowest = min(lowest, max(0, table2[1] - table1[1]), max(0, table1[3] - (space[1] - table2[1])))
	print(lowest if lowest < float('inf') else -1)


t = int(input())
for _ in range(t):
	[W, H] = [int(c) for c in input().split()]
	[ablx, ably, atrx, atry] = [int(c) for c in input().split()]
	[w, h] = [int(c) for c in input().split()]
	if atrx - ablx + w > W and atry - ably + h > H:
		print(-1)
		continue
	ans = float('inf')
	if atrx - ablx + w <= W:
		ans = min(ans, max(0, w - ablx))
		ans = min(ans, max(0, w - W + atrx))
	if atry - ably + h <= H:
		ans = min(ans, max(0, h - ably))
		ans = min(ans, max(0, h - H + atry))
	print(int(ans))


import sys, math, io, os, time, itertools, collections
mod = 10 ** 9 + 7
sys.setrecursionlimit(10000)
i = sys.stdin.readline
p = sys.stdout.write
global start, end

def isin(l, x):
	left = 0
	right = len(l) - 1
	if x < l[0]:
		return -1
	while left <= right:
		mid = left + (right - left) // 2
		if l[mid] == x:
			return mid
		elif l[mid] < x:
			ans = mid
			left = mid + 1
		else:
			right = mid - 1
	return ans

def ispal(l):
	n = len(l)
	for i in range(n // 2 + 1):
		if l[i] != l[n - i - 1]:
			return False
	return True

def ccarray(l):
	d = {l[k]: k for k in range(len(l))}
	m = sorted(d)
	return [d[m[k]] for k in range(len(l))]

def is_prime(n):
	if n <= 3:
		return n > 1
	if n % 2 == 0 or n % 3 == 0:
		return False
	k = 5
	while k ** 2 <= n:
		if n % k == 0 or n % (k + 2) == 0:
			return False
		k += 6
	return True

def sieve(n):
	prime = [True for k in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for k in range(p * p, n + 1, p):
				prime[k] = False
		p += 1

def main():
	(W, H) = [int(k) for k in i().split()]
	(x1, y1, x2, y2) = [int(k) for k in i().split()]
	(w, h) = [int(k) for k in i().split()]
	k = y2 - y1
	g = x2 - x1
	(t1, t2, t3, t4) = (max(0, w - x1), max(0, x2 + w - W), max(0, h - y1), max(0, y2 + h - H))
	if h + k > H and w + g > W:
		ans = -1
	elif h + k > H and w + g <= W:
		ans = min(t1, t2)
	elif h + k <= H and w + g > W:
		ans = min(t3, t4)
	else:
		ans = min(t1, t2, t3, t4)
	p(str(ans) + '\n')
t = 1
t = int(i())
start = time.perf_counter()
for _ in range(t):
	main()
end = time.perf_counter()


import sys
from math import sqrt, ceil, floor, gcd
from collections import Counter
input = lambda : sys.stdin.readline()

def int_arr():
	return list(map(int, input().split()))

def str_arr():
	return list(map(str, input().split()))

def get_str():
	return map(str, input().split())

def get_int():
	return map(int, input().split())

def get_flo():
	return map(float, input().split())

def lcm(a, b):
	return a * b // gcd(a, b)
mod = 1000000007

def solve(X, Y, lx, ly, rx, ry, x, y):
	leftside = X - rx
	rightside = lx
	upside = Y - ry
	downside = ly
	c = float('inf')
	if y <= Y and abs(rx - lx) + x <= X:
		if leftside < x:
			c = min(c, rightside - (rightside - abs(x - leftside)))
		else:
			c = 0
	if y <= Y and abs(rx - lx) + x <= X:
		if rightside < x:
			c = min(c, leftside - (leftside - abs(x - rightside)))
		else:
			c = 0
	if x <= X and abs(ry - ly) + y <= Y:
		if upside < y:
			c = min(c, downside - (downside - abs(y - upside)))
		else:
			c = 0
	if x <= X and abs(ry - ly) + y <= Y:
		if downside < y:
			c = min(c, upside - (upside - abs(y - downside)))
		else:
			c = 0
	print(f'{c:.9f}' if c != float('inf') else -1)
for _ in range(int(input())):
	(X, Y) = get_int()
	(lx, ly, rx, ry) = get_int()
	(x, y) = get_int()
	solve(X, Y, lx, ly, rx, ry, x, y)


for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	right = W - x2
	left = x1
	up = H - y2
	down = y1
	hor = max(left, right)
	ver = max(up, down)
	if w + x2 - x1 > W and h + y2 - y1 > H:
		print(-1)
	elif w + x2 - x1 <= W and h + y2 - y1 <= H:
		if hor >= w or ver >= h:
			print(0)
		else:
			print(min(w - hor, h - ver))
	elif w + x2 - x1 <= W:
		print(max(w - hor, 0))
	else:
		print(max(h - ver, 0))


from math import inf

def solve(W, H, x1, y1, x2, y2, w, h):
	check1 = W >= x2 - x1 + w
	check2 = H >= y2 - y1 + h
	if check1 or check2:
		ans = inf
		if check1:
			ans = min(ans, min(max(0, w - x1), max(0, x2 - W + w)))
		if check2:
			ans = min(ans, min(max(0, h - y1), max(0, y2 - H + h)))
		if ans == inf:
			return -1
		return float(ans)
	return -1.0
for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	print(solve(W, H, x1, y1, x2, y2, w, h))


n = int(input())

def main(x1, y1, x2, y2, w1, h1, w, h):
	h2 = y2 - y1
	w2 = x2 - x1
	if w2 + w1 <= w and h1 + h2 <= h:
		if w - x2 >= w1 or x1 >= w1:
			wo = 0
		else:
			wo = min(w1 + w2 - x2, w1 - (w - x2))
		if h - y2 >= h1 or y1 >= h1:
			ho = 0
		else:
			ho = min(h1 + h2 - y2, h1 - (h - y2))
		return min(wo, ho)
	elif w2 + w1 <= w:
		if w - x2 >= w1 or x1 >= w1:
			return 0
		else:
			return min(w1 + w2 - x2, w1 - (w - x2))
	elif h1 + h2 <= h:
		if h - y2 >= h1 or y1 >= h1:
			return 0
		else:
			return min(h1 + h2 - y2, h1 - (h - y2))
	else:
		return -1
for i in range(n):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(width, height) = map(int, input().split())
	print(main(x1, y1, x2, y2, width, height, w, h))


import math
c = int(input())
for _ in range(c):
	(W, H) = map(int, input().strip().split(' '))
	(x1, y1, x2, y2) = map(int, input().strip().split(' '))
	(w, h) = map(int, input().strip().split(' '))
	by_x = max(w - max(W - x2, x1), 0)
	by_y = max(h - max(H - y2, y1), 0)
	by_x = by_x if min(W - x2, x1) >= by_x else math.inf
	by_y = by_y if min(H - y2, y1) >= by_y else math.inf
	res = min(by_x, by_y)
	if res == math.inf:
		print(-1)
	else:
		print(res)


def solve(w, h, x1, y1, x2, y2, ww, hh):
	if abs(x2 - x1) + ww > w and abs(y2 - y1) + hh > h:
		print(-1)
		return None
	x_shift = max(w, h)
	y_shift = max(w, h)
	if x1 + w - x2 >= ww:
		if ww > max(x1, w - x2):
			x_shift = ww - max(x1, w - x2)
		else:
			print(0)
			return None
	if y1 + h - y2 >= hh:
		if hh > max(y1, h - y2):
			y_shift = hh - max(y1, h - y2)
		else:
			print(0)
			return None
	print(min(x_shift, y_shift))
	return None
for _ in range(int(input())):
	solve(*map(int, input().split()), *map(int, input().split()), *map(int, input().split()))


import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
	(w, h) = [int(c) for c in input().split()]
	(x1, y1, x2, y2) = [int(c) for c in input().split()]
	(nw, nh) = [int(c) for c in input().split()]
	(ow, oh) = (x2 - x1, y2 - y1)
	if ow + nw > w and oh + nh > h:
		ans = -1
	else:
		ans = float('inf')
		if ow + nw <= w:
			ans = min(ans, max(nw - w + x2, 0))
			ans = min(ans, max(0, nw - x1))
		if oh + nh <= h:
			ans = min(ans, max(0, nh - h + y2))
			ans = min(ans, max(0, nh - y1))
	print(ans)


for _ in range(int(input())):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w1, h1) = map(int, input().split())
	(la, ra, da, ua) = (x1, w - x2, y1, h - y2)
	a = []
	if w1 <= la:
		a.append(0)
	elif x2 - x1 + w1 <= w:
		a.append(w1 - la)
	if w1 <= ra:
		a.append(0)
	elif x2 - x1 + w1 <= w:
		a.append(w1 - ra)
	if h1 <= da:
		a.append(0)
	elif y2 - y1 + h1 <= h:
		a.append(h1 - da)
	if h1 <= ua:
		a.append(0)
	elif y2 - y1 + h1 <= h:
		a.append(h1 - ua)
	if len(a) != 0:
		print(min(a))
	else:
		print(-1)


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def get_int():
	return int(sys.stdin.readline().strip())

def get_list_strings():
	return list(map(str, sys.stdin.readline().strip().split()))

def solve(W, H, x1, y1, x2, y2, w, h):
	topH = H - y2
	botH = y1
	leftW = x1
	rightW = W - x2
	if h <= topH and w <= W:
		print(0.0)
		return
	if w <= leftW and h <= H:
		print(0.0)
		return
	if w <= rightW and h <= H:
		print(0.0)
		return
	if h <= botH and w <= W:
		print(0.0)
		return
	dist = float('inf')
	req = w - rightW
	if leftW >= req:
		dist = min(dist, req)
	req = w - leftW
	if rightW >= req:
		dist = min(dist, req)
	req = h - topH
	if botH >= req:
		dist = min(dist, req)
	req = h - botH
	if topH >= req:
		dist = min(dist, req)
	if dist != float('inf'):
		print(dist)
		return
	else:
		print(-1)
		return
T = get_int()
while T:
	(W, H) = get_ints()
	(x1, y1, x2, y2) = get_ints()
	(w, h) = get_ints()
	solve(W, H, x1, y1, x2, y2, w, h)
	T -= 1


tt = int(input())
for _ in range(tt):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	res = 0
	l = x1
	r = W - x2
	d = y1
	u = H - y2
	if w > max(l, r) and h > max(u, d):
		if w > l + r and h > u + d:
			res = -1
		else:
			h_shift = w - max(l, r) if l + r >= w > max(l, r) else W + H
			v_shift = h - max(u, d) if u + d >= h > max(u, d) else W + H
			res = min(h_shift, v_shift)
	print(res)


t = int(input())
for case in range(t):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	w1 = x2 - x1
	h1 = y2 - y1
	(w2, h2) = map(int, input().split())
	if h1 + h2 > h and w1 + w2 > w:
		print(-1)
		continue
	best = 10000000000000
	if w2 >= x1 and h2 >= y1:
		if w1 + w2 <= w:
			best = min(best, w2 - x1)
		if h1 + h2 <= h:
			best = min(best, h2 - y1)
	else:
		best = 0
	if w - w2 <= x2 and h2 >= y1:
		if w1 + w2 <= w:
			best = min(best, x2 - (w - w2))
		if h1 + h2 <= h:
			best = min(best, h2 - y1)
	else:
		best = 0
	if w - w2 <= x2 and h - h2 <= y2:
		if w1 + w2 <= w:
			best = min(best, x2 - (w - w2))
		if h1 + h2 <= h:
			best = min(best, y2 - (h - h2))
	else:
		best = 0
	if w2 >= x1 and h - h2 <= y2:
		if w1 + w2 <= w:
			best = min(best, w2 - x1)
		if h1 + h2 <= h:
			best = min(best, y2 - (h - h2))
	else:
		best = 0
	print(float(best))


import sys
input = sys.stdin.readline

def mp():
	return map(int, input().split())

def lmp():
	return list(map(int, input().split()))

def mps(A):
	return [tuple(map(int, input().split())) for _ in range(A)]
import math
import bisect
from copy import deepcopy as dc
from itertools import accumulate
from collections import Counter, defaultdict, deque

def ceil(U, V):
	return (U + V - 1) // V

def modf1(N, MOD):
	return (N - 1) % MOD + 1
inf = int(1e+20)
mod = int(1000000000.0 + 7)
t = int(input())
for _ in range(t):
	(W, H) = mp()
	(x1, y1, x2, y2) = mp()
	(w, h) = mp()
	(ax, bx) = (x1, W - x2)
	(ay, by) = (y1, H - y2)
	ans = inf
	if max(ax, bx) >= w or max(ay, by) >= h:
		print(0)
		continue
	else:
		if ax + bx >= w:
			ans = min(ans, w - ax, w - bx)
		if ay + by >= h:
			ans = min(ans, h - ay, h - by)
	if ans == inf:
		print(-1)
	else:
		print(ans)


t = int(input())
for _ in range(t):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	width = x2 - x1
	height = y2 - y1
	ver_flag = 0
	hor_flag = 0
	(x, y) = map(int, input().split())
	if y + height > h:
		ver_flag = 1
	else:
		dist1 = y - y1
		if dist1 < 0:
			dist1 = 0
		dist2 = y2 - (h - y)
		if dist2 < 0:
			dist2 = 0
		ver = min(dist1, dist2)
	if x + width > w:
		hor_flag = 1
	else:
		hor1 = x - x1
		if hor1 < 0:
			hor1 = 0
		hor2 = x2 - (w - x)
		if hor2 < 0:
			hor2 = 0
		hor = min(hor1, hor2)
	if ver_flag == 0 and hor_flag == 0:
		print(min(hor, ver))
	elif ver_flag == 1 and hor_flag == 0:
		print(hor)
	elif ver_flag == 0 and hor_flag == 1:
		print(ver)
	else:
		print(-1)


from bisect import insort, bisect_right, bisect_left
from sys import stdout, stdin, setrecursionlimit
from heapq import heappush, heappop, heapify
from io import BytesIO, IOBase
from collections import *
from itertools import *
from random import *
from string import *
from queue import *
from math import *
from re import *
from os import *

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = read(self._fd, max(fstat(self._fd).st_size, 8192))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = read(self._fd, max(fstat(self._fd).st_size, 8192))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))
(graph, mod, szzz) = ({}, 10 ** 9 + 7, lambda : sorted(zzz()))

def getStr():
	return input()

def getInt():
	return int(input())

def listStr():
	return list(input())

def getStrs():
	return input().split()

def isInt(s):
	return '0' <= s[0] <= '9'

def input():
	return stdin.readline().strip()

def zzz():
	return [int(i) for i in input().split()]

def output(answer, end='\n'):
	stdout.write(str(answer) + end)

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def getPrimes(N=10 ** 5):
	SN = int(sqrt(N))
	sieve = [i for i in range(N + 1)]
	sieve[1] = 0
	for i in sieve:
		if i > SN:
			break
		if i == 0:
			continue
		for j in range(2 * i, N + 1, i):
			sieve[j] = 0
	prime = [i for i in range(N + 1) if sieve[i] != 0]
	return prime

def primeFactor(n, prime=getPrimes()):
	lst = []
	mx = int(sqrt(n)) + 1
	for i in prime:
		if i > mx:
			break
		while n % i == 0:
			lst.append(i)
			n //= i
	if n > 1:
		lst.append(n)
	return lst
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
daysInMounth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
num = getInt()
for _ in range(num):
	(W, H) = zzz()
	(x1, y1, x2, y2) = zzz()
	(w, h) = zzz()
	available_x = W + x1 - x2
	available_y = H + y1 - y2
	z = False
	ans = float('inf')
	if available_x >= w:
		current_available = max(x1 - 0, W - x2)
		t1 = float(max(0, w - current_available))
		ans = min(ans, t1)
		z = True
	if available_y >= h:
		current_available = max(y1 - 0, H - y2)
		t2 = float(max(0, h - current_available))
		ans = min(ans, t2)
		z = True
	if z:
		print(ans)
	else:
		print(-1)


t = int(input())
for i in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if x2 - x1 + w > W and y2 - y1 + h > H:
		print(-1)
	else:
		r1 = h - y1
		r2 = w - x1
		r3 = y2 - (H - h)
		r4 = x2 - (W - w)
		if x2 - x1 + w > W:
			ans = min(r1, r3)
		elif y2 - y1 + h > H:
			ans = min(r2, r4)
		else:
			l = [r1, r2, r3, r4]
			ans = min(l)
		if ans < 0:
			print(0)
		else:
			print(ans)


import sys
input = sys.stdin.readline

def solve1(W, H, x1, y1, x2, y2, w, h):
	if x1 < w and y1 < h:
		r = 1 << 60
		if x2 + w - x1 <= W:
			r = min(r, (w - x1) ** 2)
		if y2 + h - y1 <= H:
			r = min(r, (h - y1) ** 2)
		if x2 + w - x1 <= W and y2 + h - y1 <= H:
			r = min(r, (w - x1) ** 2 + (h - y1) ** 2)
		return r
	else:
		return 0

def solve():
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	r = 1 << 60
	for i in range(4):
		r = min(r, solve1(W, H, x1, y1, x2, y2, w, h))
		(W, H, x1, y1, x2, y2, w, h) = (H, W, y1, W - x2, y2, W - x1, h, w)
	if r == 1 << 60:
		print(-1)
	else:
		print(r ** 0.5)
for i in range(int(input())):
	solve()


import sys
input = sys.stdin.readline
inf = 10000000000.0
for _ in range(int(input())):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(tw, th) = map(int, input().split())
	ans = []
	if w >= x2 - x1 + tw:
		ans.append(max(0, tw - x1))
		ans.append(max(0, tw - (w - x2)))
	if h >= y2 - y1 + th:
		ans.append(max(0, th - y1))
		ans.append(max(0, th - (h - y2)))
	print(min(ans) if ans else -1)


t = int(input())
while t > 0:
	t -= 1
	s1 = input().split()
	W = int(s1[0])
	H = int(s1[1])
	s2 = input().split()
	x1 = int(s2[0])
	y1 = int(s2[1])
	x2 = int(s2[2])
	y2 = int(s2[3])
	s3 = input().split()
	w = int(s3[0])
	h = int(s3[1])
	w_s = W - (x2 - x1)
	h_s = H - (y2 - y1)
	ans_w = W + H
	ans_h = W + H
	if w_s >= w:
		ans_w = min(w - x1, w - (W - x2))
	if h_s >= h:
		ans_h = min(h - y1, h - (H - y2))
	ans = min(ans_w, ans_h)
	if ans < 0:
		ans = 0
	if w_s < w and h_s < h:
		ans = -1
	print(ans)


for i in range(int(input())):
	dimensions = list(map(int, input().split(' ')))
	W = dimensions[0]
	H = dimensions[1]
	first_table_dimensions = list(map(int, input().split(' ')))
	x1 = first_table_dimensions[0]
	y1 = first_table_dimensions[1]
	x2 = first_table_dimensions[2]
	y2 = first_table_dimensions[3]
	second_table_dimensions = list(map(int, input().split(' ')))
	w = second_table_dimensions[0]
	h = second_table_dimensions[1]
	horizontal_change = -1
	if x2 - x1 + w <= W:
		left_space = x1
		right_space = W - x2
		maximum_space = max(left_space, right_space)
		if maximum_space >= w:
			horizontal_change = 0
		else:
			horizontal_change = w - maximum_space
	vertical_change = -1
	if y2 - y1 + h <= H:
		bottom_space = y1
		top_space = H - y2
		maximum_space = max(bottom_space, top_space)
		if maximum_space >= h:
			vertical_change = 0
		else:
			vertical_change = h - maximum_space
	if vertical_change == -1 and horizontal_change == -1:
		print('-1')
	elif vertical_change == -1:
		print(horizontal_change)
	elif horizontal_change == -1:
		print(vertical_change)
	else:
		print(min(vertical_change, horizontal_change))


def solve(w, h, x, y, x1, y1, x2, y2):
	wid = y2 - y1
	leng = x2 - x1
	if wid > h:
		return -1
	if leng > w:
		return -1
	if y + wid > h:
		if leng + x > w:
			return -1
		else:
			lft = w - x
			rgt = x
			if x1 >= rgt or x2 <= lft:
				return 0
			return min(x2 - lft, rgt - x1)
	if x + leng > w:
		if y + wid > h:
			return -1
		else:
			upr = y
			lwr = h - y
			if y1 >= upr or y2 <= lwr:
				return 0
			return min(y2 - lwr, upr - y1)
	lft = w - x
	rgt = x
	upr = y
	lwr = h - y
	if x1 >= rgt or x2 <= lft:
		return 0
	if y1 >= upr or y2 <= lwr:
		return 0
	return min(min(x2 - lft, rgt - x1), min(y2 - lwr, upr - y1))
t = int(input())
for i in range(t):
	(w, h) = map(int, input().split(' '))
	(x1, y1, x2, y2) = map(int, input().split(' '))
	(x, y) = map(int, input().split(' '))
	print(solve(w, h, x, y, x1, y1, x2, y2))


t = int(input())
for i in range(t):
	(W, H) = (int(c) for c in input().split())
	(x1, y1, x2, y2) = (int(c) for c in input().split())
	(w, h) = (int(c) for c in input().split())
	w1 = w - x1
	w2 = w + x2 - W
	h1 = h - y1
	h2 = h + y2 - H
	if x2 - x1 + w > W:
		if y2 - y1 + h > H:
			print(-1)
		else:
			x = min(h1, h2)
			if x < 0:
				print(0)
			else:
				print(x)
	elif y2 - y1 + h > H:
		x = min(w1, w2)
		if x < 0:
			print(0)
		else:
			print(x)
	else:
		x = min(w1, w2, h1, h2)
		if x < 0:
			print(0)
		else:
			print(x)


from math import *
from collections import defaultdict as dt
from sys import stdin
inp = lambda : stdin.readline().strip()
I = lambda : int(inp())
M = lambda : map(int, inp().split())
L = lambda : list(M())
mod = 1000000007
inf = 100000000000000000000
ss = 'abcdefghijklmnopqrstuvwxyz'

def solve():
	(W, H) = M()
	(x1, y1, x2, y2) = M()
	(w, h) = M()
	p = w - x1
	q = h - y1
	r = w - W + x2
	s = h - H + y2
	e = inf
	if x2 - x1 + w > W and y2 - y1 + h > H:
		print(-1)
		return
	if p <= 0 or q <= 0 or r <= 0 or (s <= 0):
		print(str(0) + '.000000000')
		return
	if x2 - x1 + w <= W:
		e = min(e, p)
	if y2 - y1 + h <= H:
		e = min(e, q)
	if x2 - x1 + w <= W:
		e = min(e, r)
	if y2 - y1 + h <= H:
		e = min(e, s)
	print(str(e) + '.000000000')
tt = 1
tt = I()
for _ in range(tt):
	solve()


def TwoTables():
	t = int(input())
	for i in range(t):
		(w, h) = [int(x) for x in input().split()]
		(x_1, y_1, x_2, y_2) = [int(x) for x in input().split()]
		(w_green, h_green) = [int(x) for x in input().split()]
		w_blue = x_2 - x_1
		h_blue = y_2 - y_1
		if w_green > w or w_blue > w or h_green > h or (h_blue > h) or (w_blue + w_green > w and h_blue + h_green > h):
			print(-1)
		else:
			hor_space = max(x_1, w - x_2)
			vert_space = max(y_1, h - y_2)
			hor_steps = max(w_green - hor_space, 0)
			vert_steps = max(h_green - vert_space, 0)
			is_there_space_to_move_blue_hor = w_green + w_blue <= w
			is_there_space_to_move_blue_vert = h_green + h_blue <= h
			if is_there_space_to_move_blue_hor and (hor_steps <= vert_steps or not is_there_space_to_move_blue_vert):
				print(hor_steps)
			else:
				print(vert_steps)
	return
TwoTables()


for _ in range(int(input())):
	(W, H) = [int(x) for x in input().split()]
	(x1, y1, x2, y2) = [int(x) for x in input().split()]
	(w, h) = [int(x) for x in input().split()]
	(wo, ho) = (x2 - x1, y2 - y1)
	if wo + w > W and ho + h > H or (w > W or h > H):
		ans = -1
	else:
		chutiya1 = chutiya2 = 100000000
		if wo + w <= W:
			(mrityu1, mrityu2) = (max(0, w - x1), max(0, w - (W - x2)))
			chutiya1 = min(mrityu1, mrityu2)
		if ho + h <= H:
			mrityu3 = max(0, h - y1)
			mrityu4 = max(0, h - (H - y2))
			chutiya2 = min(mrityu3, mrityu4)
		if chutiya1 == 100000000:
			ans = chutiya2
		elif chutiya2 == 100000000:
			ans = chutiya1
		else:
			ans = min(chutiya1, chutiya2)
	print(ans)


def tot(W, x1, x2, w):
	if x2 - x1 + w > W:
		return 100000000000000
	return min(max(0, w - x1), max(0, x2 - (W - w)))
for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	ret = min(tot(W, x1, x2, w), tot(H, y1, y2, h))
	print(-1 if ret == 100000000000000 else ret)


def solution(W, H, x1, y1, x2, y2, w2, h2):
	(w1, h1) = (x2 - x1, y2 - y1)
	if w1 + w2 > W and h1 + h2 > H:
		return -1
	if w <= x1 or w <= W - x2 or h <= y1 or (h <= H - y2):
		return 0
	dx = W - x2 - x1
	dy = H - y2 - y1
	if dx > 0:
		x1 += dx
		x2 == dx
	if dy > 0:
		y1 += dy
		y2 += dy
	if w2 + w1 > W:
		return h2 - y1
	if h2 + h1 > H:
		return w2 - x1
	return min(w2 - x1, h2 - y1)
TestCases = int(input())
for _ in range(TestCases):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	print(f'{solution(W, H, x1, y1, x2, y2, w, h)}')


num = int(input())
outputs = []
for i in range(num):
	possible = []
	(room_x, room_y) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(second_x, second_y) = map(int, input().split())
	if x2 - x1 + second_x <= room_x:
		possible.append(max(0, second_x - x1))
		possible.append(max(0, x2 - (room_x - second_x)))
	if y2 - y1 + second_y <= room_y:
		possible.append(max(0, second_y - y1))
		possible.append(max(0, y2 - (room_y - second_y)))
	if len(possible) == 0:
		outputs.append(-1)
	else:
		outputs.append(min(possible))
for i in outputs:
	print(i)


import sys
from bisect import bisect_left as lb
from collections import deque
from queue import PriorityQueue as pq
from math import *
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
inv = lambda x: pow(x, mod - 2, mod)
mod = 10 ** 9 + 7
for _ in range(ii()):
	(w, h) = il()
	(x1, y1, x2, y2) = il()
	(w1, h1) = il()
	ans = float('inf')
	if w1 + (x2 - x1) > w and h1 + (y2 - y1) > h:
		print(-1)
		continue
	ans = float('inf')
	if w1 + (x2 - x1) <= w:
		if x1 >= w1 or x2 <= w - w1:
			ans = 0
		if h1 + (y2 - y1) <= h:
			ans = min(ans, abs(h1 - y1), abs(h - h1 - y2))
		ans = min(ans, abs(w1 - x1), abs(w - w1 - x2))
	if h1 + (y2 - y1) <= h:
		if y1 >= h1 or y2 <= h - h1:
			ans = 0
		if w1 + (x2 - x1) <= w:
			ans = min(ans, abs(w1 - x1), abs(w - w1 - x2))
		ans = min(ans, abs(h1 - y1), abs(h - h1 - y2))
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import ceil

def prod(a, mod=10 ** 9 + 7):
	ans = 1
	for each in a:
		ans = ans * each % mod
	return ans

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
from collections import deque
from math import inf
for _ in range(int(input()) if True else 1):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	(wt, ht) = (x2 - x1, y2 - y1)
	if wt + w > W and ht + h > H:
		print(-1)
		continue
	ans = inf
	if wt + w <= W:
		if x2 <= W - w:
			ans = 0
		else:
			ans = min(ans, abs(x2 - (W - w)))
		if x1 >= w:
			ans = 0
		else:
			ans = min(ans, abs(x1 - w))
	if ht + h <= H:
		if y2 <= H - h:
			ans = 0
		else:
			ans = min(ans, abs(y2 - (H - h)))
		if y1 >= h:
			ans = 0
		else:
			ans = min(ans, abs(y1 - h))
	print(ans)


mini = 100000
tests = int(input())
mod = 10 ** 9 + 7
while tests:
	tests -= 1
	arr = list(map(int, input().split()))
	(W, H) = (arr[0], arr[1])
	arr = list(map(int, input().split()))
	(a, b, c, d) = (arr[0], arr[1], arr[2], arr[3])
	arr = list(map(int, input().split()))
	(w, h) = (arr[0], arr[1])
	aud = b + H - d
	arl = a + W - c
	left = a
	right = W - c
	down = b
	up = H - d
	if h <= down or h <= up or w <= left or (w <= right) or (h > down and (w <= left or w <= right)) or (h > up and (w <= left or w <= right)):
		print(0)
	elif h <= aud or w <= arl:
		(i, j) = (1000000000000000, 1000000000000000)
		if aud > 0:
			i = min(h - down if h - down <= up else 1000000000000000, h - up if h - up <= down else 1000000000000000)
		if arl > 0:
			j = min(w - left if w - left <= right else 1000000000000000, w - right if w - right <= left else 1000000000000000)
		print(min(i, j))
	else:
		print(-1)


t = int(input())
while t > 0:
	t -= 1
	s = input().split()
	w = int(s[0])
	h = int(s[1])
	s = input().split()
	x1 = int(s[0])
	y1 = int(s[1])
	x2 = int(s[2])
	y2 = int(s[3])
	s = input().split()
	wi = int(s[0])
	hi = int(s[1])
	wj = w - (x2 - x1)
	hj = h - (y2 - y1)
	ans = max(w, h)
	if wj >= wi:
		ans = min(wi - x1, x2 - w + wi)
	if hj >= hi:
		ans = min(ans, hi - y1)
		ans = min(ans, y2 - h + hi)
	if ans < 0:
		ans = 0
	if wj < wi and hj < hi:
		ans = -1
	print(ans)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, ceil
from collections import Counter, deque
from io import BytesIO, IOBase
sys.setrecursionlimit(100000000)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None
		self.BUFSIZE = 8192

	def read(self):
		while True:
			a = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			if not a:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(a), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			a = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			self.newlines = a.count(b'\n') + (not a)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(a), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
inf = float('inf')
ninf = float('-inf')
abc = 'abcdefghijklmnopqrstuvwxyz'
inp = lambda : int(input())
st = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
int_arr = lambda : list(map(int, input().strip().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().strip().split())
get_int = lambda : map(int, input().strip().split())
get_float = lambda : map(float, input().strip().split())
mod = 1000000007
for _ in range(inp()):
	(W, H) = get_int()
	(x1, y1, x2, y2) = get_int()
	(w, h) = get_int()
	(b1, h1) = (x2 - x1, y2 - y1)
	(mn_w, mn_h) = (inf, inf)
	if W - b1 >= w:
		if x1 >= w or x2 <= W - w:
			mn_w = 0
		else:
			(mn_l, mn_r) = (abs(w - x1), abs(W - w - x2))
			mn_w = min(mn_r, mn_l)
	if H - h1 >= h:
		if y1 >= h or y2 <= H - h:
			mn_h = 0
		else:
			(mn_u, mn_d) = (abs(h - y1), abs(H - h - y2))
			mn_h = min(mn_u, mn_d)
	if mn_h == inf and mn_w == inf:
		print(-1)
	else:
		print(f'{min(mn_h, mn_w):.9f}')


t = int(input())
for _ in range(t):
	(w, h) = map(int, input().split())
	(x, y, X, Y) = map(int, input().split())
	(W, H) = map(int, input().split())
	(a, b) = (10 ** 8, 10 ** 8)
	if X - x <= w - W:
		a = max(W - max(x, w - X), 0)
	if Y - y <= h - H:
		b = max(H - max(y, h - Y), 0)
	if min(a, b) != 10 ** 8:
		print(min(a, b))
	else:
		print(-1)


for _ in range(int(input())):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(ww, hh) = map(int, input().split())
	if w - ww < x2 - x1:
		weight = -1
	else:
		weight = min(ww - x1, x2 - (w - ww))
		if weight <= 0:
			print(0)
			continue
	if h - hh < y2 - y1:
		height = -1
	else:
		height = min(hh - y1, y2 - (h - hh))
		if height <= 0:
			print(0)
			continue
	if weight == -1 and height == -1:
		print(-1)
	elif weight == -1:
		print(height)
	elif height == -1:
		print(weight)
	else:
		print(min(weight, height))


t = int(input())
for _ in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	q1 = x1
	q2 = W - x2
	a1 = y1
	a2 = H - y2
	if w <= q1 or w <= q2:
		print(0)
	elif h <= a1 or h <= a2:
		print(0)
	else:
		(q1, q2) = (max(q1, q2), min(q1, q2))
		(a1, a2) = (max(a1, a2), min(a1, a2))
		if q1 + q2 < w and a1 + a2 < h:
			print(-1)
		elif q1 + q2 < w:
			print(h - a1)
		elif a1 + a2 < h:
			print(w - q1)
		else:
			s1 = w - q1
			s2 = h - a1
			print(min(s1, s2))


INF = 1001001001

def main():
	(RoomX, RoomY) = map(int, input().split())
	(X1, Y1, X2, Y2) = map(int, input().split())
	(X, Y) = map(int, input().split())
	Ans = INF
	if X2 - X1 + X <= RoomX:
		Ans = min(Ans, max(0, X - X1))
		Ans = min(Ans, max(0, X - RoomX + X2))
	if Y2 - Y1 + Y <= RoomY:
		Ans = min(Ans, max(0, Y - Y1))
		Ans = min(Ans, max(0, Y - RoomY + Y2))
	print(Ans if Ans != INF else -1)
T = int(input())
for _ in range(T):
	main()


t = int(input())
for i in range(t):
	(W, H) = map(int, input().split())
	(a, b, c, d) = list(map(int, input().split()))
	(w, h) = map(int, input().split())
	tableWidth = c - a
	tableheight = d - b
	if a >= w or b >= h or W - c >= w or (H - d >= h):
		print(0)
	elif tableWidth + w > W and tableheight + h > H:
		print(-1)
	else:
		mini = W + H
		if tableWidth + w <= W:
			vala = w - max(a, W - c)
			mini = min(mini, vala)
		if tableheight + h <= H:
			valb = h - max(b, H - d)
			mini = min(mini, valb)
		print(mini)


for _ in range(int(input())):
	inf = 10 ** 9
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	ans = min(inf, max(min(w - x1, x2 - W + w), 0) if w + x2 - x1 <= W else inf, max(min(h - y1, y2 - H + h), 0) if h + y2 - y1 <= H else inf)
	print(-1) if ans == inf else print(ans)


t = int(input())
for i in range(0, t):
	minimum = 10 ** 8
	(box_x, box_y) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(tableW, tableH) = map(int, input().split())
	if box_x - (x2 - x1) >= tableW and box_y >= tableH or (box_y - (y2 - y1) >= tableH and box_x >= tableW):
		if box_x - (x2 - x1) >= tableW and box_y >= tableH:
			minimum = min(minimum, tableW - x1, x2 - (box_x - tableW))
		if box_y - (y2 - y1) >= tableH and box_x >= tableW:
			minimum = min(minimum, tableH - y1, y2 - (box_y - tableH))
		if minimum <= 0:
			print('0.000000000')
		else:
			print('%.9f' % minimum)
	else:
		print('-1')


tc = int(input())
for _ in range(tc):
	(w, h) = list(map(int, input().split()))
	(x1, y1, x2, y2) = list(map(int, input().split()))
	(w2, h2) = list(map(int, input().split()))
	(w1, h1) = (x2 - x1, y2 - y1)
	if w1 + w2 > w and h1 + h2 > h:
		print(-1)
	elif w1 + w2 > w:
		print(max(min(h2 - y1, y2 - (h - h2)), 0))
	elif h1 + h2 > h:
		print(max(min(w2 - x1, x2 - (w - w2)), 0))
	else:
		print(max(min(h2 - y1, y2 - (h - h2), w2 - x1, x2 - (w - w2)), 0))


a = int(input())
for i in range(a):
	(p1, p2) = map(int, input().split())
	(k1, k2, k3, k4) = map(int, input().split())
	(z1, z2) = map(int, input().split())
	s1 = 1000000000000
	s2 = 1000000000000
	if k3 - k1 + z1 <= p1:
		s1 = max(z1 - max(k1, p1 - k3), 0)
	if k4 - k2 + z2 <= p2:
		s2 = max(z2 - max(k2, p2 - k4), 0)
	if min(s1, s2) == 1000000000000:
		print(-1)
	else:
		print(min(s1, s2))


import sys
import math

def solve_one():
	(W, H) = map(int, sys.stdin.readline().strip().split())
	(x1, y1, x2, y2) = map(int, sys.stdin.readline().strip().split())
	(w, h) = map(int, sys.stdin.readline().strip().split())
	best_move = 1000000000
	if w + (x2 - x1) > W and h + (y2 - y1) > H:
		return -1
	else:
		if w + (x2 - x1) <= W:
			best_move = min(best_move, max(w - x1, 0))
			best_move = min(best_move, max(w - (W - x2), 0))
		if h + (y2 - y1) <= H:
			best_move = min(best_move, max(h - y1, 0))
			best_move = min(best_move, max(h - (H - y2), 0))
	return best_move
number_of_data = int(sys.stdin.readline())
for _ in range(number_of_data):
	print(solve_one())


t = int(input())
for i in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	trans = 0
	dx1 = x1
	dx2 = W - x2
	dy1 = y1
	dy2 = H - y2
	w1 = x2 - x1
	h1 = y2 - y1
	if w1 + w > W and h1 + h > H:
		trans = -1
	elif h <= H and w <= max(dx1, dx2) or (w <= W and h <= max(dy1, dy2)):
		trans = 0
	else:
		(trans1, trans2) = (max(H, W), max(H, W))
		if h <= H and w1 + w <= W:
			trans1 = min(w - dx1, w - dx2)
		if w <= W and h1 + h <= H:
			trans2 = min(h - dy1, h - dy2)
		trans = min(trans1, trans2)
	print(trans)


def readline():
	return map(int, input().split())

def solve():
	(W, H) = readline()
	(x1, y1, x2, y2) = readline()
	(w, h) = readline()
	if x2 - x1 + w <= W:
		yield (w - x1)
		yield (w - (W - x2))
	if y2 - y1 + h <= H:
		yield (h - y1)
		yield (h - (H - y2))
t = int(input())
for __ in range(t):
	ans = min(solve(), default=None)
	print(-1 if ans is None else max(ans, 0))


from math import inf

def solve(W, H, x1, y1, x2, y2, w, h):
	check1 = W >= x2 - x1 + w
	check2 = H >= y2 - y1 + h
	if check1 or check2:
		(answ, ansh) = (inf, inf)
		if check1:
			left = max(0, w - x1)
			right = max(0, w - (W - x2))
			answ = min(answ, left, right)
		if check2:
			down = max(0, h - y1)
			up = max(0, h - (H - y2))
			ansh = min(ansh, up, down)
		ans = min(ansh, answ)
		if ans == inf:
			return -1
		return ans
	return -1.0
for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	print(solve(W, H, x1, y1, x2, y2, w, h))


import sys
input = sys.stdin.readline
t = int(input())
for test in range(t):
	(wr, hr) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(wt, ht) = map(int, input().split())
	wot = abs(x1 - x2)
	hot = abs(y1 - y2)
	bestdist = wr + hr
	if wot + wt > wr and hot + ht > hr:
		print(-1)
	else:
		if not wot + wt > wr:
			bestdist = min(wt - x1, bestdist)
			bestdist = min(x2 - (wr - wt), bestdist)
		if not hot + ht > hr:
			bestdist = min(ht - y1, bestdist)
			bestdist = min(y2 - (hr - ht), bestdist)
		print(max(0, bestdist))


cases = input()
for c in range(int(cases)):
	(rw, rh) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(tw, th) = map(int, input().split())
	west = x1
	east = rw - x2
	north = rh - y2
	south = y1
	westside = tw - west
	eastside = tw - east
	northside = th - north
	southside = th - south
	if rw - (x2 - x1) < tw and rh - (y2 - y1) < th:
		print('-1')
	elif west >= rw or east >= rw or north >= rh or (south >= rh):
		print('0')
	else:
		if northside > rh - (north + (y2 - y1)):
			northside = 100000000
		if southside > rh - (south + (y2 - y1)):
			southside = 100000000
		if westside > rw - (west + (x2 - x1)):
			westside = 100000000
		if eastside > rw - (east + (x2 - x1)):
			eastside = 100000000
		print(max(0, min(northside, southside, westside, eastside)))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def nck(n, k):
	res = 1
	for i in range(1, k + 1):
		res = res * (n - i + 1) // i
	return res
from collections import Counter, defaultdict
import math
import heapq
import bisect
for tt in range(int(input())):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(x, y) = map(int, input().split())
	if x <= x1 or w - x2 >= x or y <= h - y2 or (y <= y1):
		print(0)
	else:
		answer = 10 ** 15
		a = x1 + w - x2
		b = h - y2 + y1
		if x <= a:
			answer = min(max(0, x - max(x1, w - x2)), answer)
		if y <= b:
			answer = min(answer, max(0, y - max(y1, h - y2)))
		if answer == 10 ** 15:
			print(-1)
		else:
			print('%.7f' % answer)


from sys import stdin
input = stdin.buffer.readline

def func():
	ans = 100000000.0
	if w + (x2 - x1) <= W:
		ans = min(ans, max(0, w - x1))
		ans = min(ans, max(0, x2 - (W - w)))
	if h + (y2 - y1) <= H:
		ans = min(ans, max(0, h - y1))
		ans = min(ans, max(0, y2 - (H - h)))
	print(ans if ans != 100000000.0 else -1)
for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	func()


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	(w_room, h_room) = [int(x) for x in input().split()]
	(x1, y1, x2, y2) = [int(x) for x in input().split()]
	(w, h) = [int(x) for x in input().split()]
	d_left = x1
	d_right = w_room - x2
	d_down = y1
	d_up = h_room - y2
	ans = float('inf')
	if h + (y2 - y1) <= h_room:
		up = max(0, h - d_down)
		down = max(0, h - d_up)
		ans = min((ans, up, down))
	if w + (x2 - x1) <= w_room:
		right = max(0, w - d_left)
		left = max(0, w - d_right)
		ans = min((ans, right, left))
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)


import sys
p = sys.stdin.readline
for test in range(int(input().strip())):
	(W, H) = map(int, input().strip().split())
	(x1, y1, x2, y2) = map(int, input().strip().split())
	(w, h) = map(int, input().strip().split())
	l = x1
	r = W - x2
	d = y1
	u = H - y2
	m = 1000000000
	a = b = m
	if l + r >= w:
		if max(l, r) >= w:
			a = 0
		elif l > r:
			a = w - l
		else:
			a = w - r
	if d + u >= h:
		if max(d, u) >= h:
			b = 0
		elif d > u:
			b = h - d
		else:
			b = h - u
	if a == b == m:
		print(-1)
	else:
		print(min(a, b))


for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	ans = 999999999999999
	if x2 - x1 + w <= W:
		ans = min(ans, max(0, w - x1))
		ans = min(ans, max(0, x2 - (W - w)))
	if y2 - y1 + h <= H:
		ans = min(ans, max(0, h - y1))
		ans = min(ans, max(0, y2 - (H - h)))
	if ans == 999999999999999:
		print(-1)
	else:
		print(float(ans))


def solve(W, H, x1, y1, x2, y2, w, h):
	if x2 - x1 + w > W and y2 - y1 + h > H:
		return -1
	dist_x = max(x1, W - x2)
	dist_y = max(y1, H - y2)
	if dist_x >= w or dist_y >= h:
		return 0
	move_x = 100000000.0
	move_y = 100000000.0
	if x2 - x1 + w <= W:
		move_x = w - dist_x
	if y2 - y1 + h <= H:
		move_y = h - dist_y
	return min(move_x, move_y)
for t in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	print(solve(W, H, x1, y1, x2, y2, w, h))


from collections import defaultdict, Counter
from sys import stdin, stdout, setrecursionlimit
from math import *
from bisect import bisect_left
from statistics import mode
from functools import reduce
import operator as op
I = lambda : stdin.readline().rstrip('\r\n')
ari = lambda : list(map(int, I().split()))
ars = lambda : list(map(str, I().split()))
arf = lambda : list(map(float, I().split()))
mpi = lambda : map(int, I().split())
mps = lambda : map(str, I().split())
mpf = lambda : map(float, I().split())
mod = 10 ** 9 + 7
setrecursionlimit(10 ** 8)
inf = float('inf')

def power(A, B, p):
	res = 1
	A = A % p
	if A == 0:
		return 0
	while B > 0:
		if B & 1 == 1:
			res = res * A % p
		B = B >> 1
		A = A * A % p
	return res

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	for i in range(5, ceil(sqrt(n)) + 1, 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def nearest2(num):
	return int(pow(2, int(log(num, 2))))

def comb(n, r):
	r = min(r, n - r)
	if r < 0:
		return 0
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def func():
	if w > W or h > H:
		return -1
	ans = []
	if x1 + W - x2 >= w:
		ans.append(max(w - x1, 0))
		ans.append(max(w - (W - x2), 0))
	if y1 + H - y2 >= h:
		ans.append(max(h - y1, 0))
		ans.append(max(h - (H - y2), 0))
	if not ans:
		return -1
	return float(min(ans))
for t in range(int(I())):
	(W, H) = map(int, I().split())
	(x1, y1, x2, y2) = map(int, I().split())
	(w, h) = map(int, I().split())
	print(func())


from collections import *
from math import *
TT = int(input())
for y in range(TT):
	(n, m) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if x2 - x1 + w <= n:
		if y2 - y1 + h <= m:
			rh = max(m - y2, y1)
			r1 = max(h - rh, 0)
			rw = max(n - x2, x1)
			r2 = max(w - rw, 0)
			print(min(r1, r2))
		else:
			rw = max(n - x2, x1)
			r2 = max(w - rw, 0)
			print(r2)
	elif y2 - y1 + h <= m:
		rh = max(m - y2, y1)
		r1 = max(h - rh, 0)
		print(r1)
	else:
		print('-1')


import sys
from math import *
import builtins
import string
import bisect
alphabets = list(string.ascii_lowercase)
ALPHABETS = list(string.ascii_uppercase)

def print(x, end='\n'):
	sys.stdout.write(str(x) + end)

def get_int():
	return int(input())

def get_list_ints():
	return list(map(int, input().split()))

def get_char_list():
	s = input()
	return list(s[:len(s)])

def get_tuple_ints():
	return tuple(map(int, input().split()))

def print_iterable(p):
	print(' '.join(map(str, p)))

def floorSearch(arr, low, high, x):
	if low > high:
		return -1
	if x >= arr[high]:
		return high
	mid = int((low + high) / 2)
	if arr[mid] == x:
		return mid
	if mid > 0 and arr[mid - 1] <= x < arr[mid]:
		return mid - 1
	if x < arr[mid]:
		return floorSearch(arr, low, mid - 1, x)
	return floorSearch(arr, mid + 1, high, x)

def binary_search(arr, x):
	low = 0
	high = len(arr) - 1
	mid = 0
	while low <= high:
		mid = (high + low) // 2
		if arr[mid] < x:
			low = mid + 1
		elif arr[mid] > x:
			high = mid - 1
		else:
			return mid
	return high

def main():
	p = get_int()
	for q in range(p):
		(a, b) = get_list_ints()
		(x1, y1, x2, y2) = get_list_ints()
		(w, h) = get_list_ints()
		x = x2 - x1
		y = y2 - y1
		c = a - x
		d = b - y
		if w > c and h > d:
			print(-1)
		else:
			ans = 1000000000
			if w + x2 - x1 <= a:
				ans = min(ans, min(w - x1, x2 - (a - w)))
			if h + y2 - y1 <= b:
				ans = min(ans, min(h - y1, y2 - (b - h)))
			ans = max(0, ans)
			print(ans)
	pass
main()


import sys
input = lambda : sys.stdin.readline().strip()

def solve():
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	(w1, h1) = (x2 - x1, y2 - y1)
	if w1 + w > W and h1 + h > H:
		return -1

	def helper(x, y):
		if x >= w or y >= h:
			return 0
		d = float('inf')
		if w1 + w <= W:
			d = min(d, w - x)
		if h1 + h <= H:
			d = min(d, h - y)
		return d
	return min((helper(x, y) for (x, y) in ((x1, y1), (W - x2, H - y2), (W - x2, y1), (x1, H - y2))))
for _ in range(int(input())):
	print(solve())


t = int(input())
for i in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if x2 - x1 + w > W and y2 - y1 + h > H:
		print(-1)
	elif x1 >= w or W - x2 >= w or y1 >= h or (H - y2 >= h):
		print(0)
	else:
		maxw = max(x1, W - x2)
		maxh = max(y1, H - y2)
		sh = w - maxw
		vi = h - maxh
		if w + x2 - x1 <= W and h + y2 - y1 > H:
			print(sh)
		elif w + x2 - x1 > W and h + y2 - y1 <= H:
			print(vi)
		else:
			print(min(vi, sh))


for _ in range(int(input())):
	(W, H) = list(map(int, input().split()))
	(x1, y1, x2, y2) = list(map(int, input().split()))
	(w, h) = list(map(int, input().split()))
	L = x1
	R = W - x2
	U = H - y2
	D = y1
	d = []
	if w <= L or w <= R or h <= U or (h <= D):
		print('0.000000000')
	else:
		if h <= D + U and w <= W:
			req = h - max(D, U)
			d.append(req)
		if w <= L + R and h <= H:
			d.append(w - max(L, R))
		if len(d) == 0:
			print(-1)
		else:
			print('{0:.9f}'.format(min(d)))


import sys
import bisect
from bisect import bisect_left as lb
from bisect import bisect_right as rb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
sflo = lambda : list(map(float, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
mod1 = 998244353
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs2(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)
ans = 0
b = []
vis = []
tot = 0
time = []
time1 = []
adj = []
mx = -1
eps = 1e-07

def dfs(a, b, p, c):
	if a == b:
		return c
	for (i, j) in adj[a]:
		if i == p:
			continue
		temp = dfs(i, b, a, c + j)
		if dfs(i, b, a):
			mx = max(i, mx)
			return 1
adj = []
ans = []

def dfs(root, par, v):
	global adj, ans, abe
	tot = v
	for (child, val) in adj[root]:
		if par == child:
			continue
		tot |= dfs(child, root, val)
	ans[root] = tot
	return tot
abe = 0

def traverse(root, par):
	global adj, ans, abe
	for (child, val) in adj[root]:
		if par == child:
			continue
		if ans[child] == 0:
			abe += 1
		else:
			traverse(child, root)

def hnbhai(tc):
	(W, H) = sd()
	(x1, y1, x2, y2) = sd()
	(w, h) = sd()
	(x3, y3) = (0, 0)
	(x4, y4) = (w, h)
	if w + (x2 - x1) > W and h + (y2 - y1) > H:
		print(-1)
		return
	ans = float('inf')
	if w + (x2 - x1) <= W:
		if x1 >= w or x2 <= W - w:
			ans = 0
		if h + (y2 - y1) <= H:
			ans = min(ans, abs(h - y1), abs(H - h - y2))
		ans = min(ans, abs(w - x1), abs(W - w - x2))
	if h + (y2 - y1) <= H:
		if y1 >= h or y2 <= H - h:
			ans = 0
		if w + (x2 - x1) <= W:
			ans = min(ans, abs(w - x1), abs(W - w - x2))
		ans = min(ans, abs(h - y1), abs(H - h - y2))
	print(ans)
for _ in range(sb()):
	hnbhai(_ + 1)


import sys
import string
from decimal import Decimal
from math import log

def inp():
	return int(input())

def inplist():
	return list(map(int, input().split()))

def inpstrlist():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
t = inp()
for _ in range(t):
	(W, H) = invr()
	(x1, y1, x2, y2) = invr()
	(w, h) = invr()
	ans = float('inf')
	x_dist = max(0, w - x1)
	y_dist = max(0, h - y1)
	if x_dist + x2 <= W:
		ans = min(ans, x_dist)
	if y_dist + y2 <= H:
		ans = min(ans, y_dist)
	x_dist = max(0, x2 - (W - w))
	y_dist = max(0, h - y1)
	if x1 - x_dist >= 0:
		ans = min(ans, x_dist)
	if y2 + y_dist <= H:
		ans = min(ans, y_dist)
	x_dist = max(0, w - x1)
	y_dist = max(0, y2 - (H - h))
	if x2 + x_dist <= W:
		ans = min(ans, x_dist)
	if y1 - y_dist >= 0:
		ans = min(ans, y_dist)
	x_dist = max(0, x2 - (W - w))
	y_dist = max(0, y2 - (H - h))
	if x1 - x_dist >= 0:
		ans = min(ans, x_dist)
	if y1 - y_dist >= 0:
		ans = min(ans, y_dist)
	if ans == float('inf'):
		print(-1)
	else:
		print(ans * 1.0)


import sys
import math
import collections
from os import path
from collections import defaultdict
mod = 10 ** 9 + 7
sys.setrecursionlimit(10 ** 6)
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
test = int(sys.stdin.readline().rstrip())
while test > 0:
	test -= 1
	(W, H) = map(int, sys.stdin.readline().rstrip().split())
	(x1, y1, x2, y2) = map(int, sys.stdin.readline().rstrip().split())
	(w, h) = map(int, sys.stdin.readline().rstrip().split())
	h1 = y1
	h2 = H - y2
	w1 = x1
	w2 = W - x2
	d1 = h - y1
	d2 = w - x1
	d3 = h - h2
	d4 = w - w2
	if h2 >= h and w <= W or (h1 >= h and w <= W) or (w1 >= w and h <= H) or (w2 >= w and h <= H):
		print(0)
	else:
		if not 0 <= d1 <= h2:
			d1 = mod
		if not 0 <= d2 <= w2:
			d2 = mod
		if not 0 <= d3 <= h1:
			d3 = mod
		if not 0 <= d4 <= w1:
			d4 = mod
		ans = min(d1, d2, d3, d4)
		if ans == mod:
			print(-1)
		else:
			print(ans)


t = int(input())
for _ in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if w + (x2 - x1) > W and h + (y2 - y1) > H:
		print(-1)
	elif x1 >= w or W - x2 >= w:
		print(0)
	elif y1 >= h or H - y2 >= h:
		print(0)
	else:
		d = W + H
		ld = x1
		rd = W - x2
		ud = H - y2
		lod = y1
		if ld + rd >= w:
			d = min(w - ld, w - rd)
		if ud + lod >= h:
			d = min(d, h - ud, h - lod)
		print(d)


e = lambda : map(int, input().split())
for _ in range(int(input())):
	(W, H) = e()
	(A, B, C, D) = e()
	(w, h) = e()
	print(max(0, min(l)) if (l := ((w - A, w - W + C) * (w + C - A <= W) + (h - B, h - H + D) * (h + D - B <= H))) else -1)


import math
for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	available_x = W + x1 - x2
	available_y = H + y1 - y2
	z = False
	ans = 1000000000000.0
	if available_x >= w:
		current_available = max(x1 - 0, W - x2)
		t1 = float(max(0, w - current_available))
		ans = min(ans, t1)
		z = True
	if available_y >= h:
		current_available = max(y1 - 0, H - y2)
		t2 = float(max(0, h - current_available))
		ans = min(ans, t2)
		z = True
	if z:
		print(ans)
	else:
		print(-1)


t = int(input())
ans = []
for i in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	w1 = x1
	h1 = y1
	w2 = W - x2
	h2 = H - y2
	if h1 + h2 < h and w1 + w2 < w:
		ans.append(-1)
	else:
		mw = max(w1, w2)
		mh = max(h1, h2)
		if h <= mh:
			ch = 0
		elif h == H or h1 + h2 < h:
			ch = 1000000000
		elif h > mh:
			ch = h - mh
		if w <= mw:
			cw = 0
		elif w == W or w1 + w2 < w:
			cw = 1000000000
		elif w > mw:
			cw = w - mw
		ans.append(float(min(ch, cw)))
print(*ans, sep='\n')


import sys

def fun(W, H, w, h, a, b, c, d):
	ans1 = None
	if b + (H - d) >= h:
		ans1 = float('inf')
		temp = max(b, H - d)
		if temp >= h:
			return 0
		else:
			ans1 = h - temp
	if a + (W - c) >= w:
		temp = max(a, W - c)
		if temp >= w:
			return 0
		else:
			if ans1:
				return min(ans1, w - temp)
			return w - temp
	if ans1:
		return ans1
	return -1
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(W, H) = map(int, input().split())
	(a, b, c, d) = map(int, input().split())
	(w, h) = map(int, input().split())
	print(fun(W, H, w, h, a, b, c, d))


import sys
cin = int(sys.stdin.readline().strip())
for testcase in range(cin):
	cin1 = list(map(int, sys.stdin.readline().strip().split()))
	cin2 = list(map(int, sys.stdin.readline().strip().split()))
	cin3 = list(map(int, sys.stdin.readline().strip().split()))
	(wo, ho) = cin1
	(w1, h1) = (cin2[2] - cin2[0], cin2[3] - cin2[1])
	(w2, h2) = cin3
	if w1 + w2 > wo:
		if h1 + h2 > ho:
			print(-1)
		else:
			td = ho - cin2[3]
			bd = cin2[1]
			if td >= h2 or bd >= h2:
				print(0)
			else:
				print(min(h2 - td, h2 - bd))
	elif h1 + h2 > ho:
		if w1 + w2 > wo:
			print(-1)
		else:
			rd = wo - cin2[2]
			ld = cin2[0]
			if rd >= w2 or ld >= w2:
				print(0)
			else:
				print(min(w2 - rd, w2 - ld))
	else:
		td = ho - cin2[3]
		bd = cin2[1]
		rd = wo - cin2[2]
		ld = cin2[0]
		if rd >= w2 or ld >= w2 or td >= h2 or (bd >= h2):
			print(0)
		else:
			print(min(h2 - td, h2 - bd, w2 - rd, w2 - ld))


def min_movement(W, H, ax1, ay1, ax2, ay2, w, h):
	widthLeft = ax1
	widthRight = W - ax2
	lengthDown = ay1
	lengthUp = H - ay2
	if w + abs(ax2 - ax1) <= W:
		minWidth = min(max(w - widthLeft, 0), max(w - widthRight, 0))
	else:
		minWidth = 99999999
	if h + abs(ay2 - ay1) <= H:
		minLength = min(max(h - lengthDown, 0), max(h - lengthUp, 0))
	else:
		minLength = 99999999
	if w + abs(ax2 - ax1) > W and h + abs(ay2 - ay1) > H:
		return int(-1)
	else:
		return int(min(minLength, minWidth))
numCases = int(input())
currentCase = 1
while currentCase <= numCases:
	(wW, hH) = map(int, input().split())
	(tx1, ty1, tx2, ty2) = map(int, input().split())
	(width, height) = map(int, input().split())
	print(min_movement(wW, hH, tx1, ty1, tx2, ty2, width, height))
	currentCase += 1


from math import sqrt
for _ in range(int(input())):
	(W, L) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, l) = map(int, input().split())
	a = x2 - x1
	b = y2 - y1
	if a + w > W and b + l > L:
		print(-1)
	elif x1 >= w or y1 >= l or W - x2 >= w or (L - y2 >= l):
		print(0)
	else:
		ans = [10 ** 9 for i in range(4)]
		if w + a <= W:
			ans[0] = abs(x1 - w)
			ans[1] = abs(x2 - (W - w))
		if b + l <= L:
			ans[2] = abs(y1 - l)
			ans[3] = abs(y2 - (L - l))
		print(min(ans))


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	ans = float('inf')
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w1, h1) = map(int, input().split())
	(w, h) = (x2 - x1, y2 - y1)
	if w + w1 <= W:
		dist = w1 - x1
		ans = min(ans, dist)
		dist = x2 - (W - w1)
		ans = min(ans, dist)
	if h + h1 <= H:
		dist = y2 - (H - h1)
		ans = min(ans, dist)
		dist = h1 - y1
		ans = min(ans, dist)
	ans = max(ans, 0)
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)


t = int(input())
for o in range(t):
	(W, H) = (int(k) for k in input().split())
	(x1, y1, x2, y2) = (int(x) for x in input().split())
	(w, h) = (int(p) for p in input().split())
	d1 = H - y2
	d2 = x1
	d3 = y1
	d4 = W - x2
	if d1 >= h or d3 >= h:
		print(0)
	elif d2 >= w or d4 >= w:
		print(0)
	elif d1 + d3 >= h and d2 + d4 < w:
		print(h - max(d1, d3))
	elif d1 + d3 < h and d2 + d4 >= w:
		print(w - max(d2, d4))
	elif d1 + d3 >= h and d2 + d4 >= w:
		print(min(h - max(d1, d3), w - max(d2, d4)))
	else:
		print(-1)


t = int(input())

def solve():
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if w + (x2 - x1) > W and h + (y2 - y1) > H:
		print(-1)
		return
	corners = []
	corners.append([w, h])
	corners.append([W - w, h])
	corners.append([W - w, H - h])
	corners.append([w, H - h])
	cors = []
	cors.append([x1, y1])
	cors.append([x2, y1])
	cors.append([x2, y2])
	cors.append([x1, y2])
	ans = []
	width_space = w + (x2 - x1) <= W
	hor_space = h + (y2 - y1) <= H
	d1 = max(corners[0][0] - cors[0][0], 0)
	d2 = max(corners[0][1] - cors[0][1], 0)
	temp = []
	if width_space:
		temp.append(d1)
	if hor_space:
		temp.append(d2)
	ans.append(min(temp))
	d1 = max(0, cors[1][0] - corners[1][0])
	d2 = max(0, corners[1][1] - cors[1][1])
	temp = []
	if width_space:
		temp.append(d1)
	if hor_space:
		temp.append(d2)
	ans.append(min(temp))
	d1 = max(0, cors[2][0] - corners[2][0])
	d2 = max(0, cors[2][1] - corners[2][1])
	temp = []
	if width_space:
		temp.append(d1)
	if hor_space:
		temp.append(d2)
	ans.append(min(temp))
	d1 = max(0, corners[3][0] - cors[3][0])
	d2 = max(0, cors[3][1] - corners[3][1])
	temp = []
	if width_space:
		temp.append(d1)
	if hor_space:
		temp.append(d2)
	ans.append(min(temp))
	print(min(ans))
for i in range(t):
	solve()


e = lambda : map(int, input().split())
(t,) = e()
while t:
	t -= 1
	(W, H) = e()
	(A, B, C, D) = e()
	(w, h) = e()
	print(max(0, min(l)) if (l := ((w - A, w - W + C) * (w + C - A <= W) + (h - B, h - H + D) * (h + D - B <= H))) else -1)


import sys
input = sys.stdin.readline
t = int(input())
while t > 0:
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	d = []
	if w + (x2 - x1) <= W:
		d.append(max(0, w - max(x1, W - x2)))
	if h + (y2 - y1) <= H:
		d.append(max(0, h - max(y1, H - y2)))
	if len(d):
		print('%.9f' % min(d))
	else:
		print(-1)
	t -= 1


for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	w2 = x2 - x1
	h2 = y2 - y1
	(w1, h1) = map(int, input().split())
	if w2 + w1 <= W or h1 + h2 <= H:
		v1 = v2 = v3 = v4 = float('inf')
		if w2 + w1 <= W:
			v1 = max(0, w1 - x1)
			v3 = max(0, w1 - W + x2)
		if h1 + h2 <= H:
			v2 = max(0, h1 - y1)
			v4 = max(0, h1 - H + y2)
		print(min(v1, v2, v3, v4))
	else:
		print('-1')


for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	w1 = x2 - x1
	h1 = y2 - y1
	ans = 0
	(w, h) = map(int, input().split())
	s1 = False
	s2 = False
	ls = x1
	rs = W - x2
	bs = y1
	ts = H - y2
	if w1 + w <= W:
		s1 = True
	if h1 + h <= H:
		s2 = True
	if w > W or h > H:
		print(-1)
	elif s1 or s2:
		if ls >= w or bs >= h or rs >= w or (ts >= h):
			ans = 0
			ans = round(ans, 6)
			print(ans)
		elif s1 and s2:
			a1 = w - ls
			a2 = h - ts
			a3 = w - rs
			a4 = h - bs
			ans = min(a1, a2, a3, a4)
			ans = round(ans, 6)
			print(ans)
		elif s1:
			a1 = w - ls
			a2 = w - rs
			ans = min(a1, a2)
			ans = round(ans, 6)
			print(ans)
		elif s2:
			a1 = h - ts
			a2 = h - bs
			ans = min(a1, a2)
			ans = round(ans, 6)
			print(ans)
	else:
		print(-1)


import heapq
from collections import *
import math

def solve():
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w1, h1) = map(int, input().split())
	if h - (y2 - y1) < h1 and w - (x2 - x1) < w1:
		print(-1)
		return
	if h - y2 >= h1 or y1 >= h1:
		print(0.0)
		return
	if w - x2 >= w1 or x1 >= w1:
		print(0.0)
		return
	if h1 == h:
		print(float(min(abs(x1 - w1), abs(w - x2 - w1))))
		return
	if w1 == w:
		print(float(min(abs(h1 - y1), abs(h - y2 - h1))))
		return
	if h - (y2 - y1) < h1:
		print(float(min(abs(x1 - w1), abs(w - x2 - w1))))
		return
	if w - (x2 - x1) < w1:
		print(float(min(abs(h1 - y1), abs(h - y2 - h1))))
		return
	print(min(float(min(abs(h1 - y1), abs(h - y2 - h1))), float(min(abs(x1 - w1), abs(w - x2 - w1)))))
	return
t = int(input())
for _ in range(t):
	solve()


import sys
import math
input = sys.stdin.readline

def s():
	(w, h) = map(int, input().split())
	l = [int(i) for i in input().split()]
	(x1, y1, x2, y2) = (l[0], l[1], l[2], l[3])
	mw = x2 - x1
	mh = y2 - y1
	(a, b) = map(int, input().split())
	wdis = w - x2 + x1
	hdis = h - y2 + y1
	if wdis < a and hdis < b:
		return -1
	if w - x2 >= a or x1 >= a or h - y2 >= b or (y1 >= b):
		return 0
	res = []
	if mw + a <= w:
		res = [a - (w - x2), a - x1]
	if mh + b <= h:
		res += [b - (h - y2), b - y1]
	return min(res)
for _ in range(int(input())):
	print(s())


for i in range(int(input())):
	x = [int(x) for x in input().split()]
	(W, H) = (x[0], x[1])
	x = [int(x) for x in input().split()]
	(x1, y1, x2, y2) = (x[0], x[1], x[2], x[3])
	x = [int(x) for x in input().split()]
	(w, h) = (x[0], x[1])
	if (w <= W - x2 or w <= x1) or (h <= H - y2 or h <= y1):
		print(0)
	elif x2 - x1 + w > W and y2 - y1 + h > H:
		print(-1)
	else:
		if W - x2 + x1 < w:
			a = 1000000000.0
		elif w <= W - x2 or w <= x1:
			a = 0
		elif x2 + w - W < w - x1:
			a = x2 + w - W
		else:
			a = w - x1
		if H - y2 + y1 < h:
			b = 1000000000.0
		elif h <= H - y2 or h <= y1:
			b = 0
		elif y2 + h - H < h - y1:
			b = y2 + h - H
		else:
			b = h - y1
		if min(a, b) == 1000000000.0:
			print(-1)
		else:
			print(min(a, b))


import math

def solve():
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	gap1 = x1
	gap2 = W - x2
	gap3 = y1
	gap4 = H - y2
	if gap3 + gap4 < h and gap1 + gap2 < w:
		return -1
	if gap1 >= w:
		return 0
	if gap2 >= w:
		return 0
	if gap3 >= h:
		return 0
	if gap4 >= h:
		return 0
	movex = 10 ** 10
	movey = 10 ** 10
	if gap1 + gap2 >= w:
		movex = w - max(gap1, gap2)
	if gap3 + gap4 >= h:
		movey = h - max(gap3, gap4)
	return min(movex, movey) * 1.0
t = int(input())
for i in range(t):
	print(solve())


def main():
	for _ in range(int(input())):
		(W, H) = map(int, input().split())
		(x1, y1, x2, y2) = map(int, input().split())
		(w, h) = map(int, input().split())
		distance = float('inf')
		if w <= W and h <= H:
			if x2 - x1 + w <= W:
				distance = min(distance, max(0, w - x1), max(0, x2 - (W - w)))
			if y2 - y1 + h <= H:
				distance = min(distance, max(0, h - y1), max(0, y2 - (H - h)))
		print(distance if distance != float('inf') else -1)
main()


t = int(input())
for _ in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if y2 - y1 + h > H and x2 - x1 + w > W:
		print(-1)
	else:
		d1 = H - y2
		d2 = y1
		row = int(1000000000.0)
		col = int(1000000000.0)
		if y2 - y1 + h <= H:
			tmp1 = max(0, h - d1)
			tmp2 = max(0, h - d2)
			col = min(tmp1, tmp2)
		d1 = W - x2
		d2 = x1
		if x2 - x1 + w <= W:
			tmp1 = max(0, w - d1)
			tmp2 = max(0, w - d2)
			row = min(tmp1, tmp2)
		m = min(row, col)
		print(float(m))


t = int(input())
for i in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	lx = w - x1
	ly = h - y1
	rx = w - (W - x2)
	ry = h - (H - y2)
	if w + (x2 - x1) > W and h + (y2 - y1) > H:
		print(-1)
	elif lx <= 0 or rx <= 0 or ly <= 0 or (ry <= 0):
		print(0)
	elif w + (x2 - x1) > W:
		print(min(ly, ry))
	elif h + (y2 - y1) > H:
		print(min(lx, rx))
	else:
		print(min(lx, rx, ly, ry))


def solve():
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w1, h1) = (abs(x1 - x2), abs(y1 - y2))
	(w2, h2) = map(int, input().split())
	if w1 + w2 > W and h1 + h2 > H:
		print(-1)
		return
	if w2 <= x1 or w2 <= W - x2 or h2 <= y1 or (h2 <= H - y2):
		print(0)
		return
	ans = W + H
	if w1 + w2 <= W:
		ans = min(ans, w2 - x1, w2 - W + x2)
	if h1 + h2 <= H:
		ans = min(ans, h2 - y1, h2 - H + y2)
	print(ans)
for _ in range(int(input())):
	solve()


t = int(input())
for i in range(t):
	(m, n) = map(int, input().split(' '))
	(a, b, c, d) = map(int, input().split(' '))
	width = max(a, m - c)
	high = max(b, n - d)
	width_limit = m - c + a
	high_limit = n - d + b
	(x, y) = map(int, input().split(' '))
	answer1 = -1
	answer2 = -1
	if x <= width_limit:
		answer1 = x - width
		if answer1 < 0:
			answer1 = 0
	if y <= high_limit:
		answer2 = y - high
		if answer2 < 0:
			answer2 = 0
	if answer1 == -1:
		answer = answer2
	elif answer2 == -1:
		answer = answer1
	else:
		answer = min(answer1, answer2)
	if x > width_limit and y > high_limit:
		answer = -1
	print(answer)


def gcd(a, b):
	if a % b == 0:
		return b
	else:
		return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)
testcases = 1
testcases = int(input())
for _ in range(testcases):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = [int(x) for x in input().split()]
	(w, h) = map(int, input().split())
	options = [y1, W - x2, H - y2, x1]
	res = []
	if h + y2 - y1 <= H:
		res.append(max(0, h - y1))
		res.append(max(0, h - (H - y2)))
	if w + x2 - x1 <= W:
		res.append(max(0, w - x1))
		res.append(max(0, w - (W - x2)))
	if len(res) == 0:
		print(-1)
	else:
		print(min(res))


for i in range(int(input())):
	(w_r, h_r) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w_t2, h_t2) = map(int, input().split())
	if x2 - x1 + w_t2 > w_r and y2 - y1 + h_t2 > h_r:
		print(-1)
	else:
		max_avail_x = max(w_r - x2, x1 - 0)
		min_avail_x = min(w_r - x2, x1 - 0)
		max_avail_y = max(h_r - y2, y1 - 0)
		min_avail_y = min(h_r - y2, y1 - 0)
		move_x = w_t2 - max_avail_x
		move_y = h_t2 - max_avail_y
		if max_avail_x > w_t2 or max_avail_y > h_t2:
			print(0)
		elif move_x > min_avail_x:
			print(move_y)
		elif move_y > min_avail_y:
			print(move_x)
		else:
			print(min(move_x, move_y))


def solve():
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	cost = 10000000000
	hneed = y1
	wneed = x1
	if wneed >= w and hneed >= h:
		cost = min(cost, 0)
	else:
		spacex = W - x2
		if spacex >= w - wneed:
			cost = min(cost, w - wneed)
			cost = max(cost, 0)
		spacey = H - y2
		if spacey >= h - hneed:
			cost = min(h - hneed, cost)
			cost = max(cost, 0)
	hneed = H - y2
	wneed = x1
	if wneed >= w and hneed >= h:
		cost = min(cost, 0)
	else:
		spacex = W - x2
		if spacex >= w - wneed:
			cost = min(cost, w - wneed)
			cost = max(cost, 0)
		spacey = y1
		if spacey >= h - hneed:
			cost = min(h - hneed, cost)
			cost = max(cost, 0)
	hneed = y1
	wneed = W - x2
	if wneed >= w and hneed >= h:
		cost = 0
	else:
		spacex = x1
		if spacex >= w - wneed:
			cost = min(cost, w - wneed)
			cost = max(cost, 0)
		spacey = H - y2
		if spacey >= h - hneed:
			cost = min(h - hneed, cost)
			cost = max(cost, 0)
	hneed = H - y2
	wneed = W - x2
	if wneed >= w and hneed >= h:
		cost = 0
	else:
		spacex = x1
		if spacex >= w - wneed:
			cost = min(cost, w - wneed)
			cost = max(cost, 0)
		spacey = y1
		if spacey >= h - hneed:
			cost = min(h - hneed, cost)
			cost = max(cost, 0)
	if cost == 10000000000:
		print(-1)
	else:
		print(float(cost))
t = int(input())
for i in range(t):
	solve()


t = int(input())
while t:
	t -= 1
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if h <= max(H - y2, y1) or w <= max(x1, W - x2):
		print(format(0, '.9f'))
	elif h + (y2 - y1) > H:
		if w + (x2 - x1) > W:
			print(-1)
		else:
			print(format(w - max(x1, W - x2), '.9f'))
	elif w + (x2 - x1) <= W:
		print(format(min(w - max(x1, W - x2), h - max(H - y2, y1)), '.9f'))
	else:
		print(format(h - max(H - y2, y1), '.9f'))


def minimal_move(room, tb1, tb2):
	W = room[0]
	H = room[1]
	w = tb2[0]
	h = tb2[1]
	x1 = tb1[0]
	y1 = tb1[1]
	x2 = tb1[2]
	y2 = tb1[3]
	ww = x2 - x1
	hh = y2 - y1
	ans = 10 ** 9
	if ww + w > W and hh + h > H:
		return -1
	if ww + w <= W:
		ans = min(w - x1, x2 - (W - w))
	if hh + h <= H:
		ans = min(y2 - (H - h), h - y1, ans)
	if ans < 0:
		ans = 0
	return ans
num = int(input().strip())
for i in range(num):
	room = list(map(int, input().strip().split()))
	tb1 = list(map(int, input().strip().split()))
	tb2 = list(map(int, input().strip().split()))
	print(minimal_move(room, tb1, tb2))


import sys
import math
mod = 10 ** 9 + 7
LI = lambda : [int(k) for k in input().split()]
input = lambda : sys.stdin.readline().rstrip()
IN = lambda : int(input())
S = lambda : input()
r = range
for t in r(IN()):
	(W, H) = LI()
	(x1, y1, x2, y2) = LI()
	(w, h) = LI()
	ans = 10 ** 20
	if x1 >= w or W - x2 >= w:
		print(0)
		continue
	if y1 >= h or H - y2 >= h:
		print(0)
		continue
	if w + (x2 - x1) <= W:
		left = max(x1, W - x2)
		ans = min(ans, w - left)
	if h + (y2 - y1) <= H:
		left = max(y1, H - y2)
		ans = min(ans, h - left)
	print(ans if ans < 10 ** 20 else -1)


t = int(input())
for _ in range(t):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w2, h2) = map(int, input().split())
	ans = 50000000
	ans2 = 50000000
	c = 0
	if w - (x2 - x1) >= w2:
		if x1 > w - x2:
			if x1 >= w2:
				print(0)
				continue
			else:
				ans = abs(x1 - w2)
		elif w - x2 >= w2:
			print(0)
			continue
		else:
			ans = abs(w2 - (w - x2))
	else:
		c += 1
	if h - (y2 - y1) >= h2:
		if y1 > h - y2:
			if h2 <= y1:
				print(0)
				continue
			else:
				ans2 = abs(y1 - h2)
		elif h - y2 >= h2:
			print(0)
			continue
		else:
			ans2 = abs(h2 - (h - y2))
	elif c == 1:
		print(-1)
		continue
	print(min(ans, ans2))


t = int(input())
for i in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if abs(y2 - y1) + h <= H and abs(x2 - x1) + w > W:
		ans = max(0, h - y1)
		ans = min(ans, max(0, y2 - (H - h)))
		print(ans)
	elif abs(y2 - y1) + h > H and abs(x2 - x1) + w <= W:
		ans = max(0, w - x1)
		ans = min(ans, max(0, x2 - W + w))
		print(ans)
	elif abs(y2 - y1) + h <= H and abs(x2 - x1) + w <= W:
		ans = max(0, h - y1)
		ans = min(ans, max(0, y2 - (H - h)))
		ans = min(ans, max(0, w - x1))
		ans = min(ans, max(0, x2 - W + w))
		print(ans)
	else:
		print(-1)


import bisect
import heapq
import math
import collections
import sys
import copy
from functools import reduce
import decimal
from io import BytesIO, IOBase
import os
sys.setrecursionlimit(10 ** 9)
decimal.getcontext().rounding = decimal.ROUND_HALF_UP
graphDict = collections.defaultdict
queue = collections.deque

class Graphs:

	def __init__(self):
		self.graph = graphDict(list)

	def add_edge(self, u, v):
		self.graph[u].append(v)
		self.graph[v].append(u)

	def dfs_utility(self, nodes, visited_nodes):
		visited_nodes.add(nodes)
		print(nodes, end='')
		for neighbour in self.graph[nodes]:
			if neighbour not in visited_nodes:
				self.dfs_utility(neighbour, visited_nodes)

	def dfs(self, node):
		Visited = set()
		self.dfs_utility(node, Visited)

	def bfs(self, node):
		visited = set()
		if node not in visited:
			queue.append(node)
			visited.add(node)
		while queue:
			parent = queue.popleft()
			print(parent)
			for item in self.graph[parent]:
				if item not in visited:
					queue.append(item)
					visited.add(item)

class Tree:

	def __init__(self, data):
		self.data = data
		self.left = None
		self.right = None

def inorder(node):
	if node:
		inorder(node.left)
		print(node.data)
		inorder(node.right)

def leaf_node_sum(root):
	if root is None:
		return 0
	if root.left is None and root.right is None:
		return root.data
	return leaf_node_sum(root.left) + leaf_node_sum(root.right)

def hight(root):
	if root is None:
		return -1
	if root.left is None and root.right is None:
		return 0
	return max(hight(root.left), hight(root.right)) + 1

def rounding(n):
	return int(decimal.Decimal(f'{n}').to_integral_value())

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def inp():
	return sys.stdin.readline().strip()

def map_inp(v_type):
	return map(v_type, inp().split())

def list_inp(v_type):
	return list(map_inp(v_type))
for _ in range(int(inp())):
	(W, H) = map_inp(int)
	(x1, y1, x2, y2) = map_inp(int)
	(w2, h2) = map_inp(int)
	(w1, h1) = (abs(x2 - x1), abs(y2 - y1))
	if w1 + w2 > W and h1 + h2 > H:
		print(-1)
	elif x1 >= w2 or abs(W - x2) >= w2 or y1 >= h2 or (abs(H - y2) >= h2):
		print(0)
	elif w1 + w2 > W:
		print(min(abs(h2 - y1), abs(h2 - abs(H - y2))))
	elif h1 + h2 > H:
		print(min(abs(w2 - x1), abs(w2 - abs(W - x2))))
	else:
		print(min(abs(h2 - y1), abs(h2 - abs(H - y2)), abs(w2 - x1), abs(w2 - abs(W - x2))))


t = int(input())
for i in range(t):
	(W, H) = map(float, input().split())
	(x1, y1, x2, y2) = map(float, input().split())
	(w, h) = map(float, input().split())
	xn = max(W - x2, x1)
	yn = max(H - y2, y1)
	maxdy = min(H - y2, y1)
	maxdx = min(W - x2, x1)
	if xn >= w:
		dx = 0
	else:
		dx = w - xn
	if yn >= h:
		dy = 0
	else:
		dy = h - yn
	if dy > maxdy and dx > maxdx:
		print(-1)
	elif dy > maxdy:
		print(dx)
	elif dx > maxdx:
		print(dy)
	else:
		print(min(dx, dy))


testCases = int(input())
for a in range(testCases):
	(wRoom, hRoom) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(wTable, hTable) = map(int, input().split())
	if x2 - x1 + wTable > wRoom and y2 - y1 + hTable > hRoom:
		print(-1)
	else:
		maxX = max([wRoom - x2, x1])
		minX = min([wRoom - x2, x1])
		maxY = max([hRoom - y2, y1])
		minY = min([hRoom - y2, y1])
		moveX = wTable - maxX
		moveY = hTable - maxY
		if maxX > wTable or maxY > hTable:
			print(0)
		elif moveX > minX:
			print(moveY)
		elif moveY > minY:
			print(moveX)
		else:
			print(min([moveX, moveY]))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import defaultdict as dd
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdmap = lambda : map(int, stdstr().split())
stdarr = lambda : list(map(int, stdstr().split()))
mod = 1000000007
for _ in range(stdint()):
	(W, H) = stdmap()
	(x1, y1, x2, y2) = stdmap()
	(w, h) = stdmap()
	ha = abs(y2 - y1)
	wa = abs(x2 - x1)
	if wa + w > W and ha + h > H:
		print(-1)
	elif x1 > w or y1 > h:
		print(0)
	elif y1 > h or x2 < W - w:
		print(0)
	elif x2 < W - w or y2 < H - h:
		print(0)
	elif x1 > w or y2 < H - h:
		print(0)
	else:
		if wa + w <= W:
			f = w - x1
			s = x2 - (W - w)
			t = x2 - (W - w)
			fo = w - x1
			wm = min(f, s, t, fo)
		else:
			wm = float('inf')
		if ha + h <= H:
			f = h - y1
			s = h - y1
			t = y2 - (H - h)
			fo = y2 - (H - h)
			hm = min(f, s, t, fo)
		else:
			hm = float('inf')
		print(min(wm, hm))


for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	rl = [x1, y2]
	rr = [x2, y2]
	ld = [x1, y1]
	rd = [x2, y1]
	ans = 10 ** 18
	if w - rl[0] + rd[0] <= W and ld[1] - (rl[1] - (H - h)) >= 0:
		ans = min(ans, min(w - rl[0], rl[1] - (H - h)))
	elif w - rl[0] + rd[0] > W and ld[1] - (rl[1] - (H - h)) >= 0:
		ans = min(ans, rl[1] - (H - h))
	elif w - rl[0] + rd[0] <= W and ld[1] - (rl[1] - (H - h)) < 0:
		ans = min(ans, w - rl[0])
	if rl[0] - (rr[0] - (W - w)) >= 0 and ld[1] - (rr[1] - (H - h)) >= 0:
		ans = min(ans, min(rr[0] - (W - w), rr[1] - (H - h)))
	elif rl[0] - (rr[0] - (W - w)) < 0 and ld[1] - (rr[1] - (H - h)) >= 0:
		ans = min(ans, rr[1] - (H - h))
	elif rl[0] - (rr[0] - (W - w)) >= 0 and ld[1] - (rr[1] - (H - h)) < 0:
		ans = min(ans, rr[0] - (W - w))
	if w - ld[0] + rd[0] <= W and h - ld[1] + rr[1] <= H:
		ans = min(ans, min(w - ld[0], h - ld[1]))
	elif w - ld[0] + rd[0] > W and h - ld[1] + rr[1] <= H:
		ans = min(ans, h - ld[1])
	elif w - ld[0] + rd[0] <= W and h - ld[1] + rr[1] > H:
		ans = min(ans, w - ld[0])
	if rl[0] - (rd[0] - (W - w)) >= 0 and rr[1] + (h - rd[1]) <= H:
		ans = min(ans, min(rd[0] - (W - w), h - rd[1]))
	elif rl[0] - (rd[0] - (W - w)) < 0 and rr[1] + (h - rd[1]) <= H:
		ans = min(ans, h - rd[1])
	elif rl[0] - (rd[0] - (W - w)) >= 0 and rr[1] + (h - rd[1]) > H:
		ans = min(ans, rd[0] - (W - w))
	if ans == 10 ** 18:
		print(-1)
	elif ans < 0:
		print(0)
	else:
		print(ans)


const = 10 ** 8 + 1

def dist(a1, a2, m):
	if a2 < a1:
		return 0
	elif a1 <= a2 and a2 - a1 <= m:
		return a2 - a1
	else:
		return const
T = int(input())
for testcase in range(1, T + 1):
	(W, H) = list(map(int, input().split()))
	(x1, y1, x2, y2) = list(map(int, input().split()))
	(w, h) = list(map(int, input().split()))
	if x2 - x1 + w > W and y2 - y1 + h > H:
		print(-1)
	else:
		d = [dist(H - h, y2, y1), dist(y1, h, H - y2), dist(W - w, x2, x1), dist(x1, w, W - x2)]
		print(min(d))


t = int(input())
while t:
	t -= 1
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if w > W:
		print(-1)
	elif H - (y2 - y1) < h:
		if h <= H and W - (x2 - x1) >= w:
			print(float(min(max(w - x1, 0), max(0, w - (W - x2)))))
		else:
			print(-1)
	elif W - (x2 - x1) >= w:
		print(float(min(min(max(w - x1, 0), max(0, w - (W - x2))), min(max(0, h - y1), max(0, h - (H - y2))))))
	else:
		print(float(min(max(0, h - y1), max(0, h - (H - y2)))))


for i in range(int(input())):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w1, h1) = map(int, input().split())
	if w1 + x2 - x1 > w and h1 + y2 - y1 > h:
		print(-1)
		continue
	ans = 10 ** 10
	if w1 + x2 - x1 <= w:
		ans = min(ans, w1 - x1, x2 - (w - w1))
	if h1 + y2 - y1 <= h:
		ans = min(ans, h1 - y1, y2 - (h - h1))
	ans = max(ans, 0)
	print(ans)


t = int(input())
for i in range(t):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w1, h1) = map(int, input().split())
	c = 0
	lst = []
	allow = h - y2
	if allow >= h1:
		dif = 0
	else:
		dif = h1 - allow
	if y1 >= dif:
		c += 1
		lst.append(dif)
	allow = y1
	if allow >= h1:
		dif = 0
	else:
		dif = h1 - allow
	if h - y2 >= dif:
		c += 1
		lst.append(dif)
	allow = x1
	if allow >= w1:
		dif = 0
	else:
		dif = w1 - allow
	if w - x2 >= dif:
		c += 1
		lst.append(dif)
	allow = w - x2
	if allow >= w1:
		dif = 0
	else:
		dif = w1 - allow
	if x1 >= dif:
		c += 1
		lst.append(dif)
	if c > 0:
		print(min(lst))
	else:
		print(-1)


import math, sys
input = sys.stdin.readline
for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	ans = []
	if w + (x2 - x1) <= W:
		if w <= x1 or x2 + w <= W:
			ans.append(0)
		else:
			ans.append(min(w - x1, x2 + w - W))
	if h + (y2 - y1) <= H:
		if h <= y1 or y2 + h <= H:
			ans.append(0)
		else:
			ans.append(min(h - y1, y2 + h - H))
	if ans:
		print(min(ans))
	else:
		print(-1)


from math import sqrt

def intersect(x1, y1, x2, y2, x3, y3, x4, y4):
	if x4 <= x1 or x2 <= x3:
		return False
	if y1 >= y4 or y2 <= y3:
		return False
	return True

def check(W, H, x1, y1, x2, y2, x3, y3, x4, y4):
	if not intersect(x1, y1, x2, y2, x3, y3, x4, y4):
		return 0
	(Dx, Dy) = ([0], [0])
	for sign in [1, -1]:
		for xf in [x1, x2]:
			for xs in [x3, x4]:
				Dx.append(sign * (xf - xs))
		for yf in [y1, y2]:
			for ys in [y3, y4]:
				Dy.append(sign * (yf - ys))
	r = 10 ** 20
	for dx in Dx:
		for dy in Dy:
			x1m = x1 + dx
			x2m = x2 + dx
			y1m = y1 + dy
			y2m = y2 + dy
			if 0 <= x1m <= W and 0 <= x2m <= W and (0 <= y1m <= H) and (0 <= y2m <= H) and (not intersect(x1m, y1m, x2m, y2m, x3, y3, x4, y4)):
				r = min(r, dx ** 2 + dy ** 2)
	return r
T = int(input())
for _ in range(T):
	(W, H) = map(int, input().split(' '))
	(x1, y1, x2, y2) = map(int, input().split(' '))
	(w, h) = map(int, input().split(' '))
	r1 = check(W, H, x1, y1, x2, y2, 0, 0, w, h)
	r2 = check(W, H, x1, y1, x2, y2, 0, H - h, w, H)
	r3 = check(W, H, x1, y1, x2, y2, W - w, 0, W, h)
	r4 = check(W, H, x1, y1, x2, y2, W - w, H - h, W, H)
	r = min(r1, r2, r3, r4)
	if r == 10 ** 20:
		print(-1)
	else:
		print(sqrt(r))


def solve(W, x1, x2, w):
	if w + (x2 - x1) > W:
		return 10000000000
	return min(max(0, w - x1), max(0, w - W + x2))
for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	answer = min(solve(W, x1, x2, w), solve(H, y1, y2, h))
	print(-1 if answer == 10000000000 else answer)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	ans = pow(10, 18)
	if w <= n - (x2 - x1):
		val = min(w - x1, w - (n - x2))
		val = max(val, 0)
		ans = min(ans, val)
	if h <= m - (y2 - y1):
		val = min(h - y1, h - (m - y2))
		val = max(val, 0)
		ans = min(ans, val)
	if ans == pow(10, 18):
		print(-1)
	else:
		print(ans)


for _ in range(int(input())):
	(W, H) = input().split()
	W = int(W)
	H = int(H)
	(x1, y1, x2, y2) = input().split()
	x1 = int(x1)
	y1 = int(y1)
	x2 = int(x2)
	y2 = int(y2)
	(w, h) = input().split()
	w = int(w)
	h = int(h)
	if W - (x2 - x1) < w and H - (y2 - y1) < h:
		print(-1)
	else:
		left = x1
		right = W - x2
		top = H - y2
		bottom = y1
		res = 1000000007
		if top + bottom >= h:
			res = min(res, max(0, h - max(top, bottom)))
		if left + right >= w:
			res = min(res, max(0, w - max(left, right)))
		print(float(res))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(b, l) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	ans = 10 ** 18
	if x2 - x1 + w <= b:
		ans = min(ans, max(0, w - x1))
		ans = min(ans, max(0, x2 - (b - w)))
	if y2 - y1 + h <= l:
		ans = min(ans, max(0, h - y1))
		ans = min(ans, max(0, y2 - (l - h)))
	if ans == 10 ** 18:
		ans = -1
	print(ans)


import math

def f(W, H, x1, y1, x2, y2, w, h):
	if x2 - x1 + w > W and y2 - y1 + h > H:
		return -1
	if w <= x1 or w <= W - x2:
		return 0.0
	if h <= y1 or h <= H - y2:
		return 0.0
	horizontal = 0
	vertical = 0
	if H - y2 < y1:
		vertical += h - y1
	else:
		vertical = h - (H - y2)
	if h + (y2 - y1) > H:
		vertical = 999999999999999999999
	if x1 < W - x2:
		horizontal += w - (W - x2)
	else:
		horizontal += w - x1
	if w + (x2 - x1) > W:
		horizontal = 999999999999999999999
	if min([horizontal, vertical]) == 999999999999999999999:
		return -1
	return min([horizontal, vertical])
for t in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	print(f(W, H, x1, y1, x2, y2, w, h))


from sys import stdin, stdout
from math import sqrt
nmbr = lambda : int(input())
lst = lambda : list(map(int, input().split()))
for _ in range(nmbr()):
	(xx, yy) = lst()
	(x1, y1, x2, y2) = lst()
	(x, y) = lst()
	height = abs(y2 - y1)
	length = abs(x2 - x1)
	dx = dy = -1
	if height + y <= yy:
		upper = abs(yy - y2)
		lower = abs(y1 - 0)
		if lower >= upper:
			dy = max(0, y - lower)
		else:
			dy = max(0, y - upper)
	if length + x <= xx:
		upper = abs(xx - x2)
		lower = abs(x1 - 0)
		if lower >= upper:
			dx = max(0, x - lower)
		else:
			dx = max(0, x - upper)
	if dx == -1 and dy == -1:
		print(-1)
		continue
	elif dy != -1 and dx != -1:
		ans = min(sqrt(dx * dx), sqrt(dy * dy))
	elif dx != -1:
		ans = sqrt(dx * dx)
	elif dy != -1:
		ans = sqrt(dy * dy)
	else:
		print(-1)
		continue
	print('{0:.10f}'.format(ans))


def solver():
	if w1 + w > W and h1 + h > H:
		return -1
	movex = max(0, w - max(W - x2, x1))
	movey = max(0, h - max(H - y2, y1))
	if w1 + w > W:
		return float(movey)
	if h1 + h > H:
		return float(movex)
	return float(min(movex, movey))
T = int(input())
lst = [10, 8, 6]
for t in range(T):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w1, h1) = (x2 - x1, y2 - y1)
	(w, h) = map(int, input().split())
	print(solver())


T = int(input())
for t in range(T):
	(w, h) = map(int, input().split())
	(x, y, xx, yy) = map(int, input().split())
	(ww, hh) = map(int, input().split())
	ans = 1123456789
	if ww + xx - x <= w:
		ans = min(ans, max(0, ww - x), max(0, ww - (w - xx)))
	if hh + yy - y <= h:
		ans = min(ans, max(0, hh - (h - yy)), max(0, hh - y))
	if ans == 1123456789:
		print(-1)
	else:
		print(ans)


R = lambda : map(int, input().split())
(t,) = R()
while t:
	t -= 1
	(W, H) = R()
	(u, v, x, y) = R()
	(w, h) = R()
	print(max(0, min(l)) if (l := ([w - u, w - W + x] * (w + x - u <= W) + [h - v, h - H + y] * (h + y - v <= H))) else -1)


import os, sys
from io import BytesIO, IOBase

def main():
	inf = float('inf')
	for _ in range(int(input())):
		(W, H) = map(int, input().split())
		(x1, y1, x2, y2) = map(int, input().split())
		(w, h) = map(int, input().split())
		(dx, dy) = (inf, inf)
		if x2 - x1 + w <= W:
			dx = max(0, min(w - W + x2, w - x1))
		if y2 - y1 + h <= H:
			dy = max(0, min(h - H + y2, h - y1))
		if dx == inf and dy == inf:
			print(-1)
			continue
		print(min(dx, dy))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


T = int(input())
for _ in range(T):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	ans = 10 ** 8
	if x2 - x1 + w <= W:
		ans = min(ans, max(0, w - x1))
		ans = min(ans, max(0, x2 - (W - w)))
	if y2 - y1 + h <= H:
		ans = min(ans, max(0, h - y1))
		ans = min(ans, max(0, y2 - (H - h)))
	if ans == 10 ** 8:
		ans = -1
	print(ans)


test = int(input())
while test:
	test -= 1
	(width, height) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(sw, sh) = map(int, input().split())
	lowerv = y1
	lowerh = x1
	topv = height - y2
	toph = width - x2
	(h1, h2) = (10 ** 11, 10 ** 11)
	flag = 0
	if lowerv + topv >= sh:
		flag = 1
		if sh <= max(lowerv, topv):
			h1 = 0
		else:
			h1 = sh - max(lowerv, topv)
	if lowerh + toph >= sw:
		flag = 1
		if sw <= max(lowerh, toph):
			h2 = 0
		else:
			h2 = sw - max(lowerh, toph)
	if flag:
		print(min(h1, h2))
	else:
		print(-1)


for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	w1 = abs(x1 - x2)
	h1 = abs(y1 - y2)
	leftw = x1
	rightw = W - x2
	toph = H - y2
	both = y1
	if leftw >= w or rightw >= w:
		print(0)
	elif toph >= h or both >= h:
		print(0)
	else:
		ans = float('inf')
		if leftw > rightw:
			move = w - leftw
			if x2 + move <= W:
				ans = min(ans, move)
		else:
			move = w - rightw
			if x1 - move >= 0:
				ans = min(ans, move)
		if both > toph:
			move = h - both
			if y2 + move <= H:
				ans = min(ans, move)
		else:
			move = h - toph
			if y1 - move >= 0:
				ans = min(ans, move)
		if ans == float('inf'):
			print(-1)
		else:
			print(float(ans))


def solve(W, H, x1, y1, x2, y2, w, h):
	maxw = max(x1, W - x2)
	maxh = max(y1, H - y2)
	if w <= maxw or h <= maxh:
		return 0
	if w + (x2 - x1) <= W or h + (y2 - y1) <= H:
		best = 10 ** 9
		if w + (x2 - x1) <= W:
			best = w - maxw
		h_move = h - maxh
		if h + (y2 - y1) <= H and h_move <= best:
			best = h_move
		return best
	else:
		return -1

def solve2(W, H, x1, y1, x2, y2, w, h):
	best = 10 ** 9
	dist = [best]
	dist = max(0, w - x1)
	dist = max(0, x2 - (W - h))
	dist = max(0, h - y1)
	dist = max(0, y2 - (H - h))
import os
import io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input().decode().strip())
for t in range(T):
	(W, H) = [int(x) for x in input().decode().strip().split(' ')]
	(x1, y1, x2, y2) = [int(x) for x in input().decode().strip().split(' ')]
	(w, h) = [int(x) for x in input().decode().strip().split(' ')]
	res1 = solve(W, H, x1, y1, x2, y2, w, h)
	print(res1)


import sys, random, bisect
from collections import deque, defaultdict
from heapq import heapify, heappop, heappush
from itertools import permutations
from math import gcd, log
input = lambda : sys.stdin.buffer.readline()
mi = lambda : map(int, input().split())
li = lambda : list(mi())
for _ in range(int(input())):
	(W, H) = mi()
	(x0, y0, x1, y1) = mi()
	(w, h) = mi()
	res = 10 ** 9
	if y1 + h <= H:
		res = 0
	elif h + (y1 - y0) <= H:
		res = min(res, h - (H - y1))
	if 0 <= y0 - h:
		res = 0
	elif h + (y1 - y0) <= H:
		res = min(res, h - y0)
	if 0 <= x0 - w:
		res = 0
	elif w + (x1 - x0) <= W:
		res = min(res, w - x0)
	if x1 + w <= W:
		res = 0
	elif w + (x1 - x0) <= W:
		res = min(res, x1 + w - W)
	if res == 10 ** 9:
		print(-1)
	else:
		print(res)


t = int(input())
for i in range(t):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w1, h1) = map(int, input().split())
	mi = 2 ** 0.5 * 10 ** 8
	if w - x2 + x1 < w1 and h - y2 + y1 < h1:
		print(-1)
		continue
	if x1 >= w1:
		print(0)
	elif w - x2 >= w1:
		print(0)
	elif h1 <= y1:
		print(0)
	elif h - y2 >= h1:
		print(0)
	else:
		a = y2 - y1
		b = x2 - x1
		mi = 2 ** 0.5 * 10 ** 8
		if h1 > y1 and w1 > x1:
			if h1 + a <= h:
				mi = min(mi, h1 - y1)
			if w1 + b <= w:
				mi = min(mi, w1 - x1)
		if x2 > w - w1 and y2 > h - h1:
			if h1 + a <= h:
				mi = min(mi, y2 + h1 - h)
			if w1 + b <= w:
				mi = min(mi, x2 + w1 - w)
		if y2 > h - h1 and w1 > x1:
			if h1 + a <= h:
				mi = min(mi, y2 + h1 - h)
			if w1 + b <= w:
				mi = min(mi, w1 - x1)
		if x2 > w - w1 and h1 > y1:
			if h1 + a <= h:
				mi = min(mi, h1 - y1)
			if w1 + b <= w:
				mi = min(mi, x2 + w1 - w)
		print(round(mi, 7))


for testcase in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	(a1, a2, b1, b2, c1, c2, d1, d2) = (0, 0, 0, 0, 0, 0, 0, 0)
	if y2 - (H - h) > 0:
		if abs(y2 - (H - h)) > y1:
			a1 = -1
		else:
			a1 = abs(y2 - (H - h))
	if w - x1 > 0:
		if abs(w - x1) > W - x2:
			a2 = -1
		else:
			a2 = abs(w - x1)
	if y2 - (H - h) > 0:
		if abs(y2 - (H - h)) > y1:
			b1 = -1
		else:
			b1 = abs(y2 - (H - h))
	if x2 - (W - w) > 0:
		if abs(x2 - (W - w)) > x1:
			b2 = -1
		else:
			b2 = abs(x2 - (W - w))
	if h - y1 > 0:
		if abs(h - y1) > H - y2:
			c1 = -1
		else:
			c1 = abs(h - y1)
	if w - x1 > 0:
		if abs(w - x1) > W - x2:
			c2 = -1
		else:
			c2 = abs(w - x1)
	if h - y1 > 0:
		if abs(h - y1) > H - y2:
			d1 = -1
		else:
			d1 = abs(h - y1)
	if x2 - (W - w) > 0:
		if abs(x2 - (W - w)) > x1:
			d2 = -1
		else:
			d2 = abs(x2 - (W - w))
	arr = [a1, a2, b1, b2, c1, c2, d1, d2]
	if 0 in arr:
		print(0)
	elif arr.count(-1) == len(arr):
		print(-1)
	else:
		print(min([i for i in arr if i > 0]))


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(a, b) = map(int, input().split())
	if a + (x2 - x1) > n and b + (y2 - y1) > m:
		print(-1)
	else:
		ans = 10 ** 40
		if a + (x2 - x1) <= n:
			ans = min(ans, max(0, a - x1), max(0, x2 - (n - a)))
		if b + (y2 - y1) <= m:
			ans = min(ans, max(0, b - y1), max(0, y2 - (m - b)))
		print(ans)


for _ in range(int(input())):
	(W, H) = (int(i) for i in map(int, input().split()))
	(x1, y1, x2, y2) = (int(i) for i in map(int, input().split()))
	(w, h) = (int(i) for i in map(int, input().split()))
	if w + (x2 - x1) > W and h + (y2 - y1) > H:
		print('-1')
		continue
	if y2 + h <= H or h <= y1 or x2 + w <= W or (w <= x1):
		print('0')
		continue
	A = w + x2 - x1 <= W
	B = h + y2 - y1 <= H
	if A and B:
		print(min(abs(w - x1), abs(x2 - (W - w)), abs(h - y1), abs(y2 - (H - h))))
	elif A:
		print(min(abs(w - x1), abs(x2 - (W - w))))
	elif B:
		print(min(abs(h - y1), abs(y2 - (H - h))))


t = int(input())
for _ in range(t):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(xx, yy) = map(int, input().split())
	ans = 10 ** 8
	if w >= x2 - x1 + xx:
		ans = min(ans, max(0, xx - x1), max(0, x2 - (w - xx)))
	if h >= y2 - y1 + yy:
		ans = min(ans, max(0, yy - y1), max(0, y2 - (h - yy)))
	print(ans if ans < 10 ** 8 else -1)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w1, h1) = map(int, input().split())
	ans = 10 ** 10
	if abs(x1 - x2) + w1 <= w:
		ans = min(ans, max(0, min(x2 - (w - w1), w1 - x1)))
	if abs(y1 - y2) + h1 <= h:
		ans = min(ans, max(0, min(y2 - (h - h1), h1 - y1)))
	if ans == 10 ** 10:
		print(-1)
	else:
		print(ans)


def tableCheck(zoom, table1, table2):
	(roomW, roomH) = map(int, zoom.split())
	(x1, y1, x2, y2) = map(int, table1.split())
	(tableW, tableH) = map(int, table2.split())
	origTableW = x2 - x1
	origTableH = y2 - y1
	if origTableW + tableW > roomW and origTableH + tableH > roomH:
		return -1
	elif origTableW + tableW > roomW:
		if max(roomH - y2, y1) < tableH:
			return tableH - max(roomH - y2, y1)
		return 0
	elif origTableH + tableH > roomH:
		if max(roomW - x2, x1) < tableW:
			return tableW - max(roomW - x2, x1)
		return 0
	else:
		if max(roomH - y2, y1) < tableH:
			moveH = tableH - max(roomH - y2, y1)
		else:
			moveH = 0
		if max(roomW - x2, x1) < tableW:
			moveW = tableW - max(roomW - x2, x1)
		else:
			moveW = 0
		return min(moveH, moveW)
caseNum = input()
for i in range(int(caseNum)):
	room = input()
	fTable = input()
	sTable = input()
	print(tableCheck(room, fTable, sTable))


for x in range(int(input())):
	answer = True
	room = [int(x) for x in input().split()]
	table1 = [int(x) for x in input().split()]
	table2 = [int(x) for x in input().split()]
	movements = []
	if table1[2] - table1[0] + table2[0] <= room[0]:
		if table1[0] >= table2[0] or room[0] - table1[2] >= table2[0]:
			movements.append(0)
		else:
			movements.append(table2[0] - table1[0])
			movements.append(table2[0] - (room[0] - table1[2]))
		answer = False
	if table1[3] - table1[1] + table2[1] <= room[1]:
		if table1[1] >= table2[1] or room[1] - table1[3] >= table2[1]:
			movements.append(0)
		else:
			movements.append(table2[1] - table1[1])
			movements.append(table2[1] - (room[1] - table1[3]))
		answer = False
	if answer:
		print(-1)
	else:
		print(min(movements))


cases = int(input())
for z in range(cases):
	(W, H) = input().split(' ')
	W = int(W)
	H = int(H)
	(x1, y1, x2, y2) = input().split()
	x1 = int(x1)
	x2 = int(x2)
	y1 = int(y1)
	y2 = int(y2)
	(w, h) = input().split()
	w = int(w)
	h = int(h)
	table1_height = y2 - y1
	table1_width = x2 - x1
	if table1_height + h > H:
		if table1_width + w > W:
			print('-1')
		else:
			print(min(max(w - x1, 0), max(w - (W - x2), 0)))
	elif table1_width + w > W:
		if table1_height + h > H:
			print('-1')
		else:
			print(min(max(h - y1, 0), max(h - (H - y2), 0)))
	else:
		print(min(min(max(w - x1, 0), max(w - (W - x2), 0)), min(max(h - y1, 0), max(h - (H - y2), 0))))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	(W, H) = ma()
	(x1, y1, x2, y2) = ma()
	(w, h) = ma()
	fl = 0
	mo = 1000000000000000
	if w + x2 - x1 <= W:
		fl = 1
		mo = min(mo, max(0, w - max(x1, W - x2)))
	if y2 - y1 + h <= H:
		fl = 1
		mo = min(mo, max(0, h - max(y1, H - y2)))
	if fl == 0:
		mo = -1
	print(mo)


import sys
input = iter(sys.stdin.read().splitlines()).__next__

def solve1d(L, a, b, l):
	best = 1000000000000000
	if l <= a:
		return 0
	else:
		best = min(best, l - a)
	if l <= L - b:
		return 0
	else:
		best = min(best, l - (L - b))
	return best

def solve(W, H, x1, y1, x2, y2, w, h):
	wf = x2 - x1
	hf = y2 - y1
	if hf + h > H and wf + w > W:
		return -1
	best = 1000000000000000
	if hf + h <= H:
		best = min(best, solve1d(H, y1, y2, h))
	if wf + w <= W:
		best = min(best, solve1d(W, x1, x2, w))
	return best
TC = int(input())
for tc in range(TC):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	res = solve(W, H, x1, y1, x2, y2, w, h)
	print(res)


from math import sqrt
N = int(input())
for testcase in range(N):
	temp = input().split()
	rx = int(temp[0])
	ry = int(temp[1])
	temp = input().split()
	x1 = int(temp[0])
	y1 = int(temp[1])
	x2 = int(temp[2])
	y2 = int(temp[3])
	temp = input().split()
	cx = int(temp[0])
	cy = int(temp[1])
	x = cy - y1
	y = ry - y2
	if x <= 0:
		k1 = 0
	elif y >= x:
		k1 = x
	else:
		k1 = 10000000000.0
	x = y2 - ry + cy
	y = y1
	if x <= 0:
		k2 = 0
	elif y >= x:
		k2 = x
	else:
		k2 = 10000000000.0
	x = cx - x1
	y = rx - x2
	if x <= 0:
		k3 = 0
	elif y >= x:
		k3 = x
	else:
		k3 = 10000000000.0
	x = x2 - (rx - cx)
	y = x1
	if x <= 0:
		k4 = 0
	elif y >= x:
		k4 = x
	else:
		k4 = 10000000000.0
	res = min(k1, k2, k3, k4)
	if res != 10000000000.0:
		print(res)
	else:
		print(-1)


for i in range(int(input())):
	(W, H) = iter(map(int, input().split()))
	(x1, y1, x2, y2) = iter(map(int, input().split()))
	(w, h) = iter(map(int, input().split()))
	x = 1000000000.0
	y = 1000000000.0
	if x2 - x1 + w > W and y2 - y1 + h > H:
		print(-1)
		continue
	if x2 - x1 + w <= W:
		if x1 > w or W - x2 > w:
			print(0)
			continue
		x = min(abs(W - w - x2), w - x1)
	if y2 - y1 + h <= H:
		if y1 > h or H - y2 > h:
			print(0)
			continue
		y = min(h - y1, h - H + y2)
	print(min(x, y))


import sys
input = sys.stdin.readline

class Rect:

	def __init__(self, bl_x, bl_y, tr_x, tr_y):
		self.bl_x = bl_x
		self.bl_y = bl_y
		self.tr_x = tr_x
		self.tr_y = tr_y
		self.tl_x = bl_x
		self.tl_y = tr_y
		self.br_x = tr_x
		self.br_y = bl_y
import math

def minMovement(table, W, H, w, h):
	u = h - table.bl_y
	r = w - table.bl_x
	l = table.tr_x - (W - w)
	d = table.tr_y - (H - h)
	if u <= 0 or r <= 0 or l <= 0 or (d <= 0):
		return 0
	if table.tr_y + u > H:
		u = math.inf
	if table.tr_x + r > W:
		r = math.inf
	if table.bl_x - l < 0:
		l = math.inf
	if table.bl_y - d < 0:
		d = math.inf
	ans = min(u, r, l, d)
	return -1 if ans == math.inf else ans
n = int(input())
for _ in range(n):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	table = Rect(x1, y1, x2, y2)
	print(minMovement(table, W, H, w, h))


T = int(input())
for _ in range(T):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if w + (x2 - x1) > W and h + (y2 - y1) > H:
		print(-1)
	elif w + (x2 - x1) > W:
		print(min(max(0, h - y1), max(0, y2 - (H - h))))
	elif h + (y2 - y1) > H:
		print(min(max(0, w - x1), max(0, x2 - (W - w))))
	else:
		ans = min(max(0, w - x1), max(0, h - y1))
		ans = min(ans, max(0, w - x1), max(0, y2 - (H - h)))
		ans = min(ans, max(0, x2 - (W - w)), max(0, y2 - (H - h)))
		ans = min(ans, max(0, x2 - (W - w)), max(0, h - y1))
		print(ans)


t = int(input())
for _ in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if h + (y2 - y1) > H:
		if w + (x2 - x1) > W:
			print(-1)
		else:
			print('%.9f' % max(0, min(w - x1, w - W + x2)))
	elif w + (x2 - x1) > W:
		print('%.9f' % max(0, min(h - y1, h - H + y2)))
	else:
		v1 = max(0, min(w - x1, w - W + x2))
		v2 = max(0, min(h - y1, h - H + y2))
		print('%.9f' % min(v1, v2))


for _ in range(int(input())):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w2, h2) = map(int, input().split())
	if x1 + w - x2 < w2 and y1 + h - y2 < h2:
		print(-1)
	else:
		x_space = max(x1, w - x2)
		y_space = max(y1, h - y2)
		(x_mov, y_mov) = (10 ** 8, 10 ** 8)
		if w2 <= x_space:
			x_mov = 0
		elif w2 + (x2 - x1) <= w:
			x_mov = abs(w2 - x_space)
		if h2 <= y_space:
			y_mov = 0
		elif h2 + (y2 - y1) <= h:
			y_mov = abs(h2 - y_space)
		print(min(x_mov, y_mov))


import time
import os, sys
from datetime import datetime
from math import floor, sqrt, gcd, factorial, ceil
from collections import Counter, defaultdict
import bisect
from itertools import chain
from collections import deque
from sys import maxsize as INT_MAX
(ONLINE_JUDGE, INF, mod) = (False, float('inf'), int(1000000000.0) + 7)
if os.path.exists('D:\\Contest'):
	ONLINE_JUDGE = True
	sys.stdin = open('inp.txt', 'r')
	sys.stdout = open('out.txt', 'w')

def readint():
	return int(sys.stdin.readline())

def readstr():
	return sys.stdin.readline()

def readlst():
	return list(map(int, sys.stdin.readline().strip().split()))

def readmul():
	return map(int, sys.stdin.readline().strip().split())

def mulfloat():
	return map(float, sys.stdin.readline().strip().split())

def flush():
	return sys.stdout.flush()

def power_two(x):
	return 1 << x

def lcm(a, b):
	return a * b // gcd(a, b)

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
			return n - leftGreater

def lower_bound(arr, n, val):
	(l, r) = (-1, n)
	while r > l + 1:
		m = int(l + r >> 1)
		if arr[m] < val:
			l = m
		else:
			r = m
	return r

def upper_bound(arr, n, val):
	(l, r) = (-1, n)
	while r > l + 1:
		m = int(l + r >> 1)
		if arr[m] <= val:
			l = m
		else:
			r = m
	return l

def binpow(a, n, mod):
	res = 1
	while n:
		if n & 1:
			res = res * a % mod
			n -= 1
		a = a * a % mod
		n = n >> 1
	return res

def printmat(l):
	for i in range(0, len(l)):
		print(*l[i], sep=' ')

def is_perfect_square(num):
	temp = num ** 0.5
	return temp // 1 == temp

def john_3_16():
	(W, H) = readmul()
	(x1, y1, x2, y2) = readmul()
	(w, h) = readmul()
	(wt, ht) = (x2 - x1, y2 - y1)
	ans = []
	if x2 - x1 + w > W and y2 - y1 + h > H:
		print(-1)
		return
	ans = INT_MAX
	if h + (y2 - y1) <= H:
		ans = min(ans, y2 - (H - h), h - y1)
	if w + (x2 - x1) <= W:
		ans = min(ans, x2 - (W - w), w - x1)
	ans = max(ans, 0)
	print('%.6f' % ans)
	return

def main():
	tc = readint()
	start = time.time()
	while tc:
		john_3_16()
		tc -= 1
	if ONLINE_JUDGE:
		print(f'{(time.time() - start) * 1000}ms')
main()


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	if x1 > x2:
		(x1, x2) = (x2, x1)
	if y1 > y2:
		(y1, y2) = (y2, y1)
	(ww, hh) = map(int, input().split())
	if x2 - x1 + ww > w and y2 - y1 + hh > h:
		print(-1)
		continue
	x = float('inf')
	if x2 - x1 + ww <= w:
		if x1 <= ww:
			x = min(x, ww - x1)
		else:
			x = 0
		if w - x2 <= ww:
			x = min(x, ww - w + x2)
		else:
			x = 0
	y = float('inf')
	if y2 - y1 + hh <= h:
		if y1 <= hh:
			y = min(y, hh - y1)
		else:
			y = 0
		if h - y2 <= hh:
			y = min(y, hh - h + y2)
		else:
			y = 0
	print(min(x, y))


import math
t = int(input())
for tc in range(t):
	(rw, rh) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if w + (x2 - x1) > rw and h + (y2 - y1) > rh:
		print(-1)
	elif w + (x2 - x1) > rw:
		ymov = max(min(h - y1, y2 - (rh - h)), 0)
		print(ymov)
	elif h + (y2 - y1) > rh:
		xmov = max(min(w - x1, x2 - (rw - w)), 0)
		print(xmov)
	else:
		xmov = max(min(w - x1, x2 - (rw - w)), 0)
		ymov = max(min(h - y1, y2 - (rh - h)), 0)
		print(min(xmov, ymov))


for _ in range(int(input())):
	(w, h) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w1, h1) = map(int, input().split())
	yb = y1
	yu = h - y2
	xf = x1
	xl = w - x2
	a = max(xf, xl)
	b = max(yb, yu)
	c = min(xf, xl)
	d = min(yb, yu)
	xfi = 100000000
	yfi = 100000000
	if w1 <= a or h1 <= b:
		print(0)
	else:
		p = w1 - a
		q = h1 - b
		if c >= p:
			xfi = p
		if d >= q:
			yfi = q
		t = min(xfi, yfi)
		if t == 100000000:
			print(-1)
		else:
			print(t)


t = int(input())
for _ in range(t):
	(w, h) = [int(i) for i in input().split()]
	(dx1, dy1, x2, y2) = [int(i) for i in input().split()]
	dx2 = w - x2
	dy2 = h - y2
	zt = 10 ** 8 + 1
	(ax, ay) = [int(i) for i in input().split()]
	if ax <= dx1 + dx2:
		zt = min(zt, max(ax - max(dx1, dx2), 0))
	if ay <= dy1 + dy2:
		zt = min(zt, max(ay - max(dy1, dy2), 0))
	if zt == 10 ** 8 + 1:
		print(-1)
	else:
		print(zt)


import math
for _ in range(int(input().strip())):
	(Wg, Hg) = map(int, input().strip().split())
	(x1, y1, x2, y2) = map(int, input().strip().split())
	(wn, hn) = map(int, input().strip().split())
	(a, b) = (100000000.0, 100000000.0)
	ht1 = y2 - y1
	wt1 = x2 - x1
	hl = Hg - hn
	wl = Wg - wn
	if wt1 <= wl:
		a = max(wn - max(x1, Wg - x2), 0)
	if ht1 <= hl:
		b = max(hn - max(y1, Hg - y2), 0)
	if min(a, b) != 100000000.0:
		print(min(a, b))
	else:
		print(-1)


import sys
input = sys.stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())

def solve():
	(W, H) = R()
	(x1, y1, x2, y2) = R()
	(w, h) = R()
	if w + (x2 - x1) > W and h + y2 - y1 > H:
		print(-1)
		return
	ans = 10 ** 18
	if w <= x1 or w <= W - x2 or h <= y1 or (h <= H - y2):
		ans = 0
	else:
		if w + (x2 - x1) <= W:
			ans = min(ans, w - x1, w - (W - x2))
		if h + (y2 - y1) <= H:
			ans = min(ans, h - y1, h - (H - y2))
	print(ans * 1.0)
for _ in range(I()):
	solve()


import os
import sys
from math import ceil, floor, sqrt, gcd, factorial
from io import BytesIO, IOBase
from collections import Counter
BUFSIZE = 8192

def main():
	for _ in range(int(input())):
		(W, H) = map(int, input().split())
		(x1, y1, x2, y2) = map(int, input().split())
		width = abs(x2 - x1)
		height = abs(y2 - y1)
		(w, h) = map(int, input().split())
		ans = 0
		if W - width < w and H - height < h:
			print(-1)
		else:
			down = h - (H - y2)
			up = h - y1
			left = w - (W - x2)
			right = w - x1
			values = []
			if H - height >= h:
				if H - y2 >= h:
					values.append(0)
				else:
					values.append(down)
				if y1 >= h:
					values.append(0)
				else:
					values.append(up)
			if W - width >= w:
				if W - x2 >= w:
					values.append(0)
				else:
					values.append(left)
				if x1 >= w:
					values.append(0)
				else:
					values.append(right)
			ans = min(values)
			print('%.6f' % ans)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	(w, h) = map(int, input().split())
	(w1, h1, w2, h2) = map(int, input().split())
	(w3, h3) = map(int, input().split())
	(s1, s2) = (float('inf'), float('inf'))
	if w3 + w2 - w1 <= w:
		s1 = max(w3 - max(w1, w - w2), 0)
	if h3 + h2 - h1 <= h:
		s2 = max(h3 - max(h1, h - h2), 0)
	s = min(s1, s2)
	if s == float('inf'):
		print(-1)
	else:
		print(s)


import sys
read = lambda : sys.stdin.readline()
for _ in range(int(read())):
	(W, H) = map(int, read().split())
	(x1, y1, x2, y2) = map(int, read().split())
	(w, h) = map(int, read().split())
	if w > W or h > H:
		print(-1)
		continue
	if w > W - (x2 - x1) and h > H - (y2 - y1):
		print(-1)
		continue
	if w > W - (x2 - x1):
		ans = max(0, h - max(H - y2, y1))
	elif h > H - (y2 - y1):
		ans = max(0, w - max(W - x2, x1))
	else:
		ans = max(0, h - max(H - y2, y1))
		ans = min(ans, max(0, w - max(W - x2, x1)))
	print(ans)


def solve():
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	a = H - y2
	b = W - x2
	c = y1
	d = x1
	if w > b + d and h > a + c:
		return -1
	(width, height) = (float('inf'), float('inf'))
	if w <= b + d:
		width = min(max(0, w - x1), max(0, W - b - (W - w)))
	if h <= a + c:
		height = min(max(0, h - y1), max(0, H - a - (H - h)))
	return min(width, height)
for _ in range(int(input())):
	print(solve())


import os
import sys
import math
from io import BytesIO, IOBase
import io
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
from collections import Counter
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree1:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
MOD = 10 ** 9 + 7

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(11)]
prime[0] = prime[1] = False

def SieveOfEratosthenes(n=10):
	p = 2
	c = 0
	while p <= n:
		if prime[p] == True:
			c += 1
			for i in range(p, n + 1, p):
				prime[i] = False
		p += 1

class DSU:

	def __init__(self, R, C):
		self.par = range(R * C + 1)
		self.rnk = [0] * (R * C + 1)
		self.sz = [1] * (R * C + 1)

	def find(self, x):
		if self.par[x] != x:
			self.par[x] = self.find(self.par[x])
		return self.par[x]

	def union(self, x, y):
		(xr, yr) = (self.find(x), self.find(y))
		if xr == yr:
			return
		if self.rnk[xr] < self.rnk[yr]:
			(xr, yr) = (yr, xr)
		if self.rnk[xr] == self.rnk[yr]:
			self.rnk[xr] += 1
		self.par[yr] = xr
		self.sz[xr] += self.sz[yr]

	def size(self, x):
		return self.sz[self.find(x)]

	def top(self):
		return self.size(len(self.sz) - 1) - 1

class LazySegTree:

	def __init__(self, _op, _e, _mapping, _composition, _id, v):

		def set(p, x):
			assert 0 <= p < _n
			p += _size
			for i in range(_log, 0, -1):
				_push(p >> i)
			_d[p] = x
			for i in range(1, _log + 1):
				_update(p >> i)

		def get(p):
			assert 0 <= p < _n
			p += _size
			for i in range(_log, 0, -1):
				_push(p >> i)
			return _d[p]

		def prod(l, r):
			assert 0 <= l <= r <= _n
			if l == r:
				return _e
			l += _size
			r += _size
			for i in range(_log, 0, -1):
				if l >> i << i != l:
					_push(l >> i)
				if r >> i << i != r:
					_push(r >> i)
			sml = _e
			smr = _e
			while l < r:
				if l & 1:
					sml = _op(sml, _d[l])
					l += 1
				if r & 1:
					r -= 1
					smr = _op(_d[r], smr)
				l >>= 1
				r >>= 1
			return _op(sml, smr)

		def apply(l, r, f):
			assert 0 <= l <= r <= _n
			if l == r:
				return
			l += _size
			r += _size
			for i in range(_log, 0, -1):
				if l >> i << i != l:
					_push(l >> i)
				if r >> i << i != r:
					_push(r - 1 >> i)
			l2 = l
			r2 = r
			while l < r:
				if l & 1:
					_all_apply(l, f)
					l += 1
				if r & 1:
					r -= 1
					_all_apply(r, f)
				l >>= 1
				r >>= 1
			l = l2
			r = r2
			for i in range(1, _log + 1):
				if l >> i << i != l:
					_update(l >> i)
				if r >> i << i != r:
					_update(r - 1 >> i)

		def _update(k):
			_d[k] = _op(_d[2 * k], _d[2 * k + 1])

		def _all_apply(k, f):
			_d[k] = _mapping(f, _d[k])
			if k < _size:
				_lz[k] = _composition(f, _lz[k])

		def _push(k):
			_all_apply(2 * k, _lz[k])
			_all_apply(2 * k + 1, _lz[k])
			_lz[k] = _id
		_n = len(v)
		_log = _n.bit_length()
		_size = 1 << _log
		_d = [_e] * (2 * _size)
		_lz = [_id] * _size
		for i in range(_n):
			_d[_size + i] = v[i]
		for i in range(_size - 1, 0, -1):
			_update(i)
		self.set = set
		self.get = get
		self.prod = prod
		self.apply = apply
MIL = 1 << 20

def makeNode(total, count):
	return total * MIL + count

def getTotal(node):
	return math.floor(node / MIL)

def getCount(node):
	return node - getTotal(node) * MIL
nodeIdentity = makeNode(0.0, 0.0)

def nodeOp(node1, node2):
	return node1 + node2
	return makeNode(getTotal(node1) + getTotal(node2), getCount(node1) + getCount(node2))
identityMapping = -1

def mapping(tag, node):
	if tag == identityMapping:
		return node
	count = getCount(node)
	return makeNode(tag * count, count)

def composition(mapping1, mapping2):
	return mapping1 if mapping1 != identityMapping else mapping2

def memodict(f):

	class memodict(dict):

		def __missing__(self, key):
			ret = self[key] = f(key)
			return ret
	return memodict().__getitem__

def pollard_rho(n):
	if n & 1 == 0:
		return 2
	if n % 3 == 0:
		return 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			prev = p
			p = p * p % n
			if p == 1:
				return math.gcd(prev - 1, n)
			if p == n - 1:
				break
		else:
			for i in range(2, n):
				(x, y) = (i, (i * i + 1) % n)
				f = math.gcd(abs(x - y), n)
				while f == 1:
					(x, y) = ((x * x + 1) % n, (y * y + 1) % n)
					y = (y * y + 1) % n
					f = math.gcd(abs(x - y), n)
				if f != n:
					return f
	return n

@memodict
def prime_factors(n):
	if n <= 1:
		return Counter()
	f = pollard_rho(n)
	return Counter([n]) if f == n else prime_factors(f) + prime_factors(n // f)

def distinct_factors(n):
	factors = [1]
	for (p, exp) in prime_factors(n).items():
		factors += [p ** i * factor for factor in factors for i in range(1, exp + 1)]
	return factors

def all_factors(n):
	(small, large) = ([], [])
	for i in range(1, int(n ** 0.5) + 1, 2 if n & 1 else 1):
		if not n % i:
			small.append(i)
			large.append(n // i)
	if small[-1] == large[-1]:
		large.pop()
	large.reverse()
	small.extend(large)
	return small

def binarySearch(arr, n, key):
	left = 0
	right = n - 1
	mid = 0
	res = n
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] > key:
			res = mid
			right = mid - 1
		else:
			left = mid + 1
	return res

def binarySearch1(arr, n, key):
	left = 0
	right = n - 1
	mid = 0
	res = -1
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] > key:
			right = mid - 1
		else:
			res = mid
			left = mid + 1
	return res
t = 1
t = int(input())
for _ in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w2, h2) = map(int, input().split())
	w1 = x2 - x1
	h1 = y2 - y1
	ch = 0
	ans = max(W, H)
	if h1 + h2 <= H:
		ch = 1
		ans = min(ans, y2 - (H - h2))
		ans = min(ans, h2 - y1)
	if w1 + w2 <= W:
		ch = 1
		ans = min(ans, w2 - x1)
		ans = min(ans, x2 - (W - w2))
	if ch:
		print(max(0, ans))
	else:
		print(-1)


read = lambda : map(int, input().split())
t = int(input())
for _ in range(t):
	(W, H) = read()
	(x1, y1, x2, y2) = read()
	(w, h) = read()
	if x2 - x1 + w > W and y2 - y1 + h > H:
		print(-1)
	else:
		res = float('inf')
		if x2 - x1 + w <= W:
			res = min(res, max(0, w - x1))
			res = min(res, max(0, x2 - (W - w)))
		if y2 - y1 + h <= H:
			res = min(res, max(0, h - y1))
			res = min(res, max(0, y2 - (H - h)))
		print('%.6f' % res)


t = int(input())
for i in range(t):
	(W, H) = map(int, input().split())
	(a, b, c, d) = map(int, input().split())
	(w, h) = map(int, input().split())
	if abs(a - c) + w > W and abs(b - d) + h > H:
		print(-1)
	elif abs(a - c) + w > W and abs(b - d) + h <= H:
		if H - d > b:
			if H - d >= h:
				print(0)
			else:
				print(h - H + d)
		elif b >= h:
			print(0)
		else:
			print(h - b)
	elif abs(a - c) + w <= W and abs(b - d) + h > H:
		if W - c > a:
			if W - c >= w:
				print(0)
			else:
				print(w - W + c)
		elif a >= w:
			print(0)
		else:
			print(w - a)
	else:
		if H - d > b:
			if H - d >= h:
				ht = 0
			else:
				ht = h - H + d
		elif b >= h:
			ht = 0
		else:
			ht = h - b
		if W - c > a:
			if W - c >= w:
				wd = 0
			else:
				wd = w - W + c
		elif a >= w:
			wd = 0
		else:
			wd = w - a
		print(min([ht, wd]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if w <= max(x1 - 0, W - x2) or h <= max(y1 - 0, H - y2):
		print(0)
	elif w + x2 - x1 > W and h + y2 - y1 > H:
		print(-1)
	else:
		a = b = 1000000000.0
		if w + x2 - x1 <= W:
			a = min(w - x1, w - (W - x2))
		if h + y2 - y1 <= H:
			b = min(h - y1, h - (H - y2))
		print(min(a, b))


import sys
import math
testcases = int(input())
while testcases > 0:
	(W, H) = map(int, sys.stdin.readline().split())
	(x1, y1, x2, y2) = map(int, sys.stdin.readline().split())
	(w, h) = map(int, sys.stdin.readline().split())
	a = max(H - y2, y1)
	b = max(W - x2, x1)
	arr1 = []
	if w + (x2 - x1) <= W:
		c = max(w - b, 0)
		arr1.append(c)
	if h + (y2 - y1) <= H:
		d = max(h - a, 0)
		arr1.append(d)
	if len(arr1) != 0:
		print('%.9f' % min(arr1))
	else:
		print(-1)
	testcases -= 1


n = int(input())
for i in range(n):
	d = []
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	x3 = 0
	y3 = 0
	x4 = w
	y4 = h
	if x1 + max(0, w - x1) < x2 + max(0, w - x1) <= W:
		d.append(max(0, w - x1))
	if x2 - max(0, x2 - W + w) > x1 - max(0, x2 - W + w) >= 0:
		d.append(max(0, x2 - W + w))
	if y1 + max(0, h - y1) < y2 + max(0, h - y1) <= H:
		d.append(max(0, h - y1))
	if y2 - max(0, y2 - H + h) > y1 - max(0, y2 - H + h) >= 0:
		d.append(max(0, y2 - H + h))
	if d == []:
		print(-1)
	else:
		print(min(d))


import sys
import math
import random
import heapq
input = sys.stdin.readline

def pow(a, n, m):
	if n == 0:
		return 1
	x = pow(a, n // 2, m)
	if n % 2 == 0:
		return x * x % m
	else:
		return x * x * a % m

def gcd(a, b):
	while b != 0:
		(a, b) = (b, a % b)
	return a

def bs(arr, le, ri):
	l = 0
	arr.sort()
	r = len(arr) - 1
	ans = 10000000
	while l <= r:
		m = (l + r) // 2
		if arr[m] >= le and arr[m] <= ri:
			ans = arr[m]
			r = m - 1
		elif arr[m] < le:
			l = m + 1
		else:
			r = m - 1
	return ans

def fun(s1, s2, r1, r2, c, s, ans):
	if c >= 10:
		return
	elif s1 > s2:
		if s1 - s2 > r2:
			ans[0] = min(ans[0], c)
			return
	elif s2 > s1:
		if s2 - s1 > r1:
			ans[0] = min(ans[0], c)
			return
	if c % 2 == 0:
		r1 -= 1
		if s[c] == '1':
			s1 += 1
			fun(s1, s2, r1, r2, c + 1, s, ans)
		elif s[c] == '0':
			fun(s1, s2, r1, r2, c + 1, s, ans)
		else:
			fun(s1, s2, r1, r2, c + 1, s, ans)
			s1 += 1
			fun(s1, s2, r1, r2, c + 1, s, ans)
	else:
		r2 -= 1
		if s[c] == '1':
			s2 += 1
			fun(s1, s2, r1, r2, c + 1, s, ans)
		elif s[c] == '0':
			fun(s1, s2, r1, r2, c + 1, s, ans)
		else:
			fun(s1, s2, r1, r2, c + 1, s, ans)
			s2 += 1
			fun(s1, s2, r1, r2, c + 1, s, ans)
t = int(input())
while t > 0:
	(wr, hr) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if x2 + w <= wr or y2 + h <= hr or w <= x1 or (h <= y1):
		print(0)
	elif x2 - x1 + w > wr and y2 - y1 + h > hr:
		print(-1)
	else:
		ans = wr * hr
		if x2 - x1 + w <= wr:
			ans = min(ans, w - x1, x2 - (wr - w))
		if y2 - y1 + h <= hr:
			ans = min(ans, h - y1, y2 - (hr - h))
		print(ans)
	t -= 1


import sys
import math
input = sys.stdin.readline
test = input()

def cal():
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	minW = min(W - x2, x1)
	maxW = max(W - x2, x1)
	minH = min(H - y2, y1)
	maxH = max(H - y2, y1)
	if maxW + minW < w and maxH + minH < h:
		ans = -1
	elif maxW + minW >= w and maxH + minH < h:
		ans = w - maxW
		if ans < 0:
			ans = 0
	elif maxW + minW < w and maxH + minH >= h:
		ans = h - maxH
		if ans < 0:
			ans = 0
	elif maxW + minW >= w and maxH + minH >= h:
		ans = min(h - maxH, w - maxW)
		if ans < 0:
			ans = 0
	print(ans)
for j in range(int(test)):
	cal()


from sys import stdin, stdout, setrecursionlimit as srl
from threading import stack_size
from collections import deque, defaultdict as dd
from math import gcd, ceil, sqrt
from bisect import bisect_left as lb
from bisect import bisect_right as rb
input = stdin.readline
flush = stdout.flush
tc = 1
tc = int(input())
for _ in range(tc):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	w1 = x2 - x1
	h1 = y2 - y1
	(w2, h2) = map(int, input().split())

	def dis(a, b):
		f = float('inf')
		if w1 + w2 <= W:
			f = min(f, abs(a[0] - b[0]))
		if h1 + h2 <= H:
			f = min(f, abs(a[1] - b[1]))
		return f
	if h1 + h2 > H and w1 + w2 > W:
		print(-1)
		continue
	ans = float('inf')
	x = w2
	y = H - h2
	if x1 > x or y2 < y:
		print(0)
		continue
	else:
		ans = min(ans, dis([x1, y2], [x, y]))
	x = W - w2
	y = H - h2
	if x2 < x or y2 < y:
		print(0)
		continue
	else:
		ans = min(ans, dis([x2, y2], [x, y]))
	x = w2
	y = h2
	if x1 > x or y1 > y:
		print(0)
		continue
	else:
		ans = min(ans, dis([x1, y1], [x, y]))
	x = W - w2
	y = h2
	if x2 < x or y1 > y:
		print(0)
		continue
	else:
		ans = min(ans, dis([x2, y1], [x, y]))
	if ans == float('inf'):
		ans = -1
	print(ans)


import sys

def test():
	(w, h) = map(int, input().split())
	(a, b, c, d) = map(int, input().split())
	(x, y) = map(int, input().split())
	check = False
	p = a + (w - c)
	q = b + (h - d)
	ans = 0
	if x <= a or x <= w - c:
		print(ans)
	elif y <= b or y <= h - d:
		print(ans)
	else:
		k = sys.maxsize
		m = sys.maxsize
		if x <= p:
			k = min(x - a, x - w + c)
			check = True
		if y <= q:
			m = min(y - b, y - h + d)
			check = True
		if check:
			print(min(k, m))
		else:
			print(-1)
t = int(input())
while t:
	test()
	t -= 1


t = int(input())
for _ in range(t):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	if x2 - x1 + w <= W or y2 - y1 + h <= H:
		ans1 = float('inf')
		ans2 = float('inf')
		if x2 - x1 + w <= W:
			ans1 = min(max(0, w - x1), max(0, x2 - W + w))
		if y2 - y1 + h <= H:
			ans2 = min(max(0, h - y1), max(0, y2 - H + h))
		ans = min(ans1, ans2)
		print('%.9f' % ans)
	else:
		print(-1)


import sys

def rall():
	return sys.stdin.readlines()

def rl():
	return sys.stdin.readline().strip()

def rl_types(types):
	str_list = [x for x in sys.stdin.readline().strip().split(' ')]
	return [types[i](str_list[i]) for i in range(len(str_list))]

def pr(something):
	sys.stdout.write(str(something) + '\n')

def pra(array):
	sys.stdout.write(' '.join([str(x) for x in array]) + '\n')

def solve(array):
	return array
NT = int(rl())
for _ in range(NT):
	(rw, rh) = [int(x) for x in rl().split(' ')]
	(x1, y1, x2, y2) = [int(x) for x in rl().split(' ')]
	(t2w, t2h) = [int(x) for x in rl().split(' ')]
	(t1w, t1h) = (x2 - x1, y2 - y1)
	if rw - t1w < t2w and rh - t1h < t2h:
		d = -1
	elif x1 > t2w or rw - x2 > t2w or y1 > t2h or (rh - y2 > t2h):
		d = 0.0
	else:
		opts = []
		if t1w + t2w <= rw:
			opts.extend([t2w - x1, x2 - (rw - t2w)])
		if t1h + t2h <= rh:
			opts.extend([t2h - y1, y2 - (rh - t2h)])
		d = 1.0 * min(opts)
	pr(d)


for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(w, h) = map(int, input().split())
	width = x2 - x1
	height = y2 - y1
	to_add = float('inf')
	if height + h <= H:
		to_add = max(0, h - max(y1, H - y2))
	if width + w <= W:
		to_add = min(to_add, max(0, w - max(x1, W - x2)))
	if to_add == float('inf'):
		print(-1)
	else:
		print(to_add)


from __future__ import print_function
from math import *
from collections import deque
import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	(w, h) = invr()
	(x1, y1, x2, y2) = invr()
	(a, b) = invr()
	ans = float('inf')
	if a + abs(x1 - x2) > w and b + abs(y1 - y2) > h:
		print(-1)
		continue
	if (b <= y1 or b <= h - y2) or a <= x1 or a <= w - x2:
		ans = 0
	else:
		if y2 + b - y1 <= h:
			ans = min(ans, b - y1)
		if y1 - (b - (h - y2)) >= 0:
			ans = min(ans, b - (h - y2))
		if x2 + a - x1 <= w:
			ans = min(ans, a - x1)
		if x1 - (a - (w - x2)) >= 0:
			ans = min(ans, a - (w - x2))
	print(ans)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(w, h) = I()
	(x1, y1, x2, y2) = I()
	(rw, rh) = I()
	curw = abs(x1 - x2)
	curh = abs(y1 - y2)
	pos = [[w, h - curh], [w - curw, h]]
	if rw <= pos[0][0] and rh <= pos[0][1] or (rw <= pos[1][0] and rh <= pos[1][1]):
		up = h - y2
		down = y1
		left = x1
		right = w - x2
		if rh <= max(up, down) or rw <= max(left, right):
			print(0)
		else:
			x = rh - max(up, down)
			y = rw - max(left, right)
			if curw + rw > w:
				print(x)
			elif curh + rh > h:
				print(y)
			else:
				print(min(x, y))
	else:
		print(-1)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(X, Y) = map(int, input().split())
	ans = float('inf')
	(x1, y1, x2, y2) = map(int, input().split())
	(x, y) = map(int, input().split())
	ans = float('inf')
	if x1 < x and y1 < y:
		(dx, dy) = (x - x1, y - y1)
		if x2 + dx <= X:
			ans = min(ans, dx)
		if y2 + dy <= Y:
			ans = min(ans, dy)
	else:
		ans = 0
	if x2 > X - x and y1 < y:
		(dx, dy) = (x2 - (X - x), y - y1)
		if x1 - dx >= 0:
			ans = min(ans, dx)
		if y2 + dy <= Y:
			ans = min(ans, dy)
	else:
		ans = 0
	if x1 < x and y2 > Y - y:
		(dx, dy) = (x - x1, y2 - (Y - y))
		if x2 + dx <= X:
			ans = min(ans, dx)
		if y1 - dy >= 0:
			ans = min(ans, dy)
	else:
		ans = 0
	if x2 > X - x and y2 > Y - y:
		(dx, dy) = (x2 - (X - x), y2 - (Y - y))
		if x1 - dx >= 0:
			ans = min(ans, dx)
		if y1 - dy >= 0:
			ans = min(ans, dy)
	print(ans if ans < float('inf') else -1)


t = int(input())
for _ in range(0, t):
	inputs = [int(num) for num in input().split()]
	W = inputs[0]
	H = inputs[1]
	inputs = [int(num) for num in input().split()]
	x1 = inputs[0]
	y1 = inputs[1]
	x2 = inputs[2]
	y2 = inputs[3]
	inputs = [int(num) for num in input().split()]
	w = inputs[0]
	h = inputs[1]
	wt = x2 - x1
	ht = y2 - y1
	if wt + w > W and ht + h > H:
		print(-1)
	else:
		shift = 0
		avah = max(H - y2, y1)
		avaw = max(W - x2, x1)
		if w <= avaw and h <= avah:
			print(0)
		else:
			if h == H - ht and w == W - wt:
				shift = min(max(w - avaw, 0), max(h - avah, 0))
			elif h > H - ht:
				shift += max(w - avaw, 0)
			elif w > W - wt:
				shift += max(h - avah, 0)
			else:
				shift = min(max(w - avaw, 0), max(h - avah, 0))
			print(shift)


for _ in range(int(input())):
	(W, H) = map(int, input().split())
	(a, b, c, d) = map(int, input().split())
	(w, h) = map(int, input().split())
	if w + c - a <= W or h + d - b <= H:
		if w + c - a > W:
			print(max(0, h - max(H - d, b)))
		elif h + d - b > H:
			print(max(0, w - max(W - c, a)))
		else:
			print(min(max(0, w - max(W - c, a)), max(0, h - max(H - d, b))))
	else:
		print(-1)


for _ in range(int(input())):
	(W, H) = list(map(float, input().split()))
	(x1, y1, x2, y2) = list(map(float, input().split()))
	(tw, th) = list(map(float, input().split()))
	d = []
	if tw + x2 - x1 <= W:
		d.append(max(0, tw - max(x1, W - x2)))
	if th + y2 - y1 <= H:
		d.append(max(0, th - max(y1, H - y2)))
	print('{:.9f}'.format(min(d)) if d else -1)


from sys import stdin

def read_int():
	return int(stdin.readline())

def read_ints():
	return map(int, stdin.readline().split(' '))
t = read_int()
for case_num in range(t):
	(W, H) = read_ints()
	(x1, y1, x2, y2) = read_ints()
	(w, h) = read_ints()
	w0 = x2 - x1
	h0 = y2 - y1
	if w + w0 > W and h + h0 > H:
		print(-1)
	else:
		ans = int(1000000000.0)
		if w + w0 <= W:
			ans = min(ans, max(0, w - x1), max(0, x2 - W + w))
		if h + h0 <= H:
			ans = min(ans, max(0, h - y1), max(0, y2 - H + h))
		print(ans)


t = int(input())
for _ in range(t):
	(wr, hr) = map(int, input().split())
	(x1, y1, x2, y2) = map(int, input().split())
	(wt, ht) = map(int, input().split())
	movex = movey = 100000000.0
	if wr >= x2 - x1 + wt:
		movex = min(max(0, wt - x1), max(0, x2 - (wr - wt)))
	if hr >= y2 - y1 + ht:
		movey = min(max(0, ht - y1), max(0, y2 - (hr - ht)))
	dis = min(movex, movey)
	resp = [-1, dis]
	print(resp[dis < 100000000.0])