#include <bits/stdc++.h>
using namespace std;
long long i, j, k, n, m, M, s, len, ans, co, T, f, r, t, pos, pre;
long long xl, yl, xr, yr, h, w;
int main() {
  scanf("%lld", &T);
  while (T--) {
    ans = 99999999999;
    scanf("%lld %lld", &n, &m);
    scanf("%lld %lld %lld %lld", &xl, &yl, &xr, &yr);
    scanf("%lld %lld", &w, &h);
    t = 99999999999;
    if (yl < h && yr + h - yl <= m) t = min(t, h - yl);
    if (xl < w && xr + w - xl <= n) t = min(t, w - xl);
    if (yl >= h || xl >= w) t = 0;
    ans = min(ans, t);
    t = 99999999999;
    if (yr > m - h && yl - (yr - (m - h)) >= 0) t = min(t, yr - (m - h));
    if (xr > n - w && xl - (xr - (n - w)) >= 0) t = min(t, xr - (n - w));
    if (yr <= m - h || xr <= n - w) t = 0;
    ans = min(ans, t);
    if (ans == 99999999999)
      printf("-1\n");
    else
      printf("%lld.000000000\n", ans);
  }
  return 0;
}
