#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long mod1 = 1000000007;
const long long mod2 = 998244353;
void _print(int a) { cerr << a; }
void _print(double a) { cerr << a; }
void _print(long double a) { cerr << a; }
void _print(long long a) { cerr << a; }
void _print(char a) { cerr << a; }
void _print(string a) { cerr << a; }
void _print(bool a) { cerr << a; }
template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
  cerr << "{";
  _print(p.first);
  cerr << ",";
  _print(p.second);
  cerr << "}";
}
template <class T>
void _print(vector<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T>
void _print(set<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T>
void _print(multiset<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T, class V>
void _print(map<T, V> v) {
  cerr << "[ ";
  for (auto i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
void precomp() {}
void solve() {
  cout.precision(10);
  long long w, h;
  cin >> w >> h;
  ;
  long long x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  ;
  long long a, b;
  cin >> a >> b;
  ;
  long double ansx = 0;
  long double ansy = 0;
  if ((y2 - y1) + b > h && (x2 - x1) + a > w)
    cout << -1 << "\n";
  else {
    long long curry = max(h - y2, y1);
    long long currx = max(w - x2, x1);
    if ((x2 - x1) + a <= w && currx < a) {
      ansx += a - currx;
    }
    if ((y2 - y1) + b <= h && curry < b) {
      ansy += b - curry;
    }
    if ((y2 - y1) + b > h)
      cout << ansx << "\n";
    else if ((x2 - x1) + a > w)
      cout << ansy << "\n";
    else
      cout << (long double)min(ansx, ansy) << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long tc = 1;
  cin >> tc;
  precomp();
  while (tc--) {
    solve();
  }
}
