for _ in range(int(input())):
total_width, total_height = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
w1 = x2 - x1
h1 = y2 - y1
w2, h2 = map(int, input().split())
ans = float('inf')
left_place = w2
right_place = total_width - w2
top_place = total_height - h2
bottom_place = h2
amount_needed_left = max(left_place - x1, 0)
amount_needed_right = max(x2 - right_place, 0)
amount_needed_top = max(y2 - top_place, 0)
amount_needed_bottom = max(bottom_place - y1, 0)
if w1 + w2 > total_width:
amount_needed_left = float('inf')
amount_needed_right = float('inf')
if h1 + h2 > total_height:
amount_needed_top = float('inf')
amount_needed_bottom = float('inf')
ans = min(amount_needed_left, amount_needed_right, amount_needed_top, amount_needed_bottom);
print(ans if ans != float('inf') else -1)
#include <bits/stdc++.h>
using namespace std;
void solve() {
int W, H, w, h, x1, y1, x2, y2;
cin >> W >> H >> x1 >> y1 >> x2 >> y2 >> w >> h;
int res = INT_MAX;
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))); }
if (res == INT_MAX) cout << -1 << "\n";
else cout << (double) (res) << "\n"; return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T; cout << fixed << setprecision(9);
for (int t = 1; t <= T; t++) solve();
return 0;
}
53. Maximum Subarray | 1527A. And Then There Were K |
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers | 318. Maximum Product of Word Lengths |
448. Find All Numbers Disappeared in an Array | 1155. Number of Dice Rolls With Target Sum |
415. Add Strings | 22. Generate Parentheses |
13. Roman to Integer | 2. Add Two Numbers |
515. Find Largest Value in Each Tree Row | 345. Reverse Vowels of a String |
628. Maximum Product of Three Numbers | 1526A - Mean Inequality |
1526B - I Hate 1111 | 1881. Maximum Value after Insertion |
237. Delete Node in a Linked List | 27. Remove Element |
39. Combination Sum | 378. Kth Smallest Element in a Sorted Matrix |
162. Find Peak Element | 1529A - Eshag Loves Big Arrays |
19. Remove Nth Node From End of List | 925. Long Pressed Name |
1051. Height Checker | 695. Max Area of Island |
402. Remove K Digits | 97. Interleaving String |
543. Diameter of Binary Tree | 124. Binary Tree Maximum Path Sum |