#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
  if (n <= 3) {
    return true;
  }
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
const int mxn = 100005;
long long lg[mxn + 1];
void findlog() {
  lg[1] = 0;
  for (int i = 2; i < mxn; i++) {
    lg[i] = lg[i / 2] + 1;
  }
}
const int k = 25;
long long table[mxn][k + 1];
void fill(long long a[], int n) {
  for (int i = 0; i < n; i++) {
    table[i][0] = a[i];
  }
  for (int j = 1; j <= k; j++) {
    for (int i = 0; i + (1 << j) <= n; i++) {
      table[i][j] = min(table[i][j - 1], table[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t-- > 0) {
    double w, h;
    cin >> w >> h;
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    double ww = (x2 - x1);
    double hh = (y2 - y1);
    int w1, h1;
    cin >> w1 >> h1;
    if (w1 + ww > w && h1 + hh > h) {
      cout << -1 << "\n";
      continue;
    }
    double ans = INT_MAX;
    if (w < ww + w1) {
      if (y2 > (h - h1)) {
        ans = min(ans, (double)(y2 - (h - h1)));
      } else
        ans = 0;
      if (y1 < h1) {
        ans = min(ans, (double)(h1 - y1));
      } else
        ans = 0;
      cout << fixed << setprecision(10) << ans << "\n";
      continue;
    } else if (h1 + hh > h) {
      if (x2 > (w - w1)) {
        ans = min(ans, (double)(x2 - (w - w1)));
      } else {
        ans = 0;
      }
      if (x1 < w1) {
        ans = min(ans, (double)(w1 - x1));
      } else
        ans = 0;
      cout << fixed << setprecision(10) << ans << "\n";
      continue;
    }
    if (x2 > (w - w1)) {
      ans = min(ans, (double)(x2 - (w - w1)));
    } else {
      ans = 0;
    }
    if (y2 > (h - h1)) {
      ans = min(ans, (double)(y2 - (h - h1)));
    } else
      ans = 0;
    if (x1 < w1) {
      ans = min(ans, (double)(w1 - x1));
    } else {
      ans = 0;
    }
    if (y1 < h1) {
      ans = min(ans, (double)(h1 - y1));
    } else
      ans = 0;
    cout << fixed << setprecision(10) << ans << "\n";
  }
  return 0;
}
