#include <bits/stdc++.h>
const long long int inf = 10e16 + 7;
using namespace std;
void print(int arr[], int n) {
  for (int i = 1; i < n + 1; i++) {
    cout << arr[i] << " ";
  }
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long int w, h;
    cin >> w >> h;
    int x[2];
    int y[2];
    cin >> x[0] >> y[0] >> x[1] >> y[1];
    long long int w2, h2;
    cin >> w2 >> h2;
    long long int h1 = y[1] - y[0];
    long long int w1 = x[1] - x[0];
    if (h - h1 < h2) {
      if (w - w1 < w2) {
        cout << -1 << endl;
        continue;
      }
    }
    long double ans = 1e9;
    if (h2 <= h - h1) {
      long long int up = h - y[1];
      long long int dn = y[0];
      if (h2 <= up || h2 <= dn) {
        ans = 0;
      } else {
        long double mn = h2 - up;
        mn = min(mn, (long double)h2 - dn);
        ans = mn;
      }
    }
    if (w2 <= w - w1) {
      long long int up = w - x[1];
      long long int dn = x[0];
      if (w2 <= up || w2 <= dn) {
        ans = min(ans, (long double)0);
      } else {
        long double mn = w2 - up;
        mn = min(mn, (long double)w2 - dn);
        ans = min(ans, mn);
      }
    }
    cout << setprecision(7) << fixed << ans << endl;
  }
  return 0;
}
