#include <bits/stdc++.h>
using namespace std;
using namespace std;
long long int lli;
gcd(long long int lli; a, long long int lli; b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int lli;
gcdExtended(long long int lli; a, long long int lli; b, long long int lli;
            *x, long long int lli; *y) {
  if (a == 0) {
    *x = 0;
    *y = 1;
    return b;
  }
  long long int lli;
  x1, y1;
  long long int lli;
  gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
long long int fac[1000001];
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int modInverse(long long int n, long long int p) {
  return power(n, p - 2, p);
}
long long int nCrModPFermat(unsigned long long n, long long int r,
                            long long int p) {
  if (r == 0) return 1;
  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
long long int mod = 1000000007;
long long int binpower(long long int a, long long int b) {
  long long int ans = 1;
  while (b > 0) {
    if (b & 1) ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
bool isPrime(long long int lli; n) {
  for (long long int lli; i = 2; 1ll * i * i <= n; ++i) {
    if (n % i == 0) return false;
  }
  return true;
}
long long int lli;
fact[3000005];
void factcalc() {
  fact[0] = 1;
  fact[1] = 1;
  for (long long int lli; i = 2; i < 3000005; ++i) {
    fact[i] = (fact[i - 1] * i) % mod;
  }
}
long long int lli;
C(long long int lli; a, long long int lli; b) {
  long long int lli;
  c = (fact[a] * fact[b - a]) % mod;
  long long int lli;
  res = (fact[b] * binpower(c, mod - 2)) % mod;
  return res;
}
struct item {
  long long int lli;
  ele;
};
struct segtree {
  vector<item> sums;
  int sz = 1;
  item NEUTRAL = {0ll};
  item single(item v) { return v; }
  item merg(item a, item b) {
    item c = {gcd(a.ele, b.ele)};
    return c;
  }
  void init(int n) {
    while (sz < n) sz *= 2;
    sums.resize(2 * sz, NEUTRAL);
  }
  void sett(int i, item v, int x, int lx, int rx) {
    if (rx - lx == 1) {
      sums[x] = single(v);
      return;
    }
    int m = (lx + rx) / 2;
    if (i < m) {
      sett(i, v, 2 * x + 1, lx, m);
    } else {
      sett(i, v, 2 * x + 2, m, rx);
    }
    sums[x] = merg(sums[2 * x + 1], sums[2 * x + 2]);
    return;
  }
  void sett(int i, item v) { sett(i, v, 0, 0, sz); }
  item add(int l, int r, int x, int lx, int rx) {
    if (l >= rx || lx >= r) return NEUTRAL;
    if (lx >= l && rx <= r) return sums[x];
    int m = (lx + rx) / 2;
    return merg(add(l, r, 2 * x + 1, lx, m), add(l, r, 2 * x + 2, m, rx));
  }
  item add(int l, int r) {
    item ans = add(l, r, 0, 0, sz);
    return ans;
  }
};
const int N = 1000001;
long long int lli;
dp[N][3];
int main() {
  int test = 1;
  cin >> test;
  while (test--) {
    long double w, h;
    cin >> w >> h;
    long double x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    long double nw, nh;
    cin >> nw >> nh;
    long double height = max(y1, abs(h - y2)), width = max(x1, abs(w - x2));
    long double ans = 0;
    if (nh <= height || nw <= width) {
      cout << fixed << setprecision(6) << ans << "\n";
      continue;
    }
    long double ry = (nh - height), rx = (nw - width);
    if (ry <= min(y1, abs(h - y2)) && rx <= min(x1, abs(w - x2))) {
      cout << fixed << setprecision(6) << min(ry, rx) << endl;
      continue;
    }
    if (rx <= min(x1, abs(w - x2))) {
      cout << fixed << setprecision(6) << rx << endl;
      continue;
    }
    if (ry <= min(y1, abs(h - y2))) {
      cout << fixed << setprecision(6) << ry << endl;
      continue;
    }
    if (ry >= min(y1, abs(h - y2)) && rx >= min(x1, abs(w - x2))) {
      cout << "-1 \n";
      continue;
    }
    ans = sqrt(rx * rx + ry * ry);
    cout << fixed << setprecision(6) << ans << "\n";
  }
  return 0;
}
