#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  vector<long double> v(t);
  long long int i, w, h, W, H, x1, x2, y1, y2, w0, h0, w1, w2, h1, h2;
  long long int aw, ah, sw, sh;
  for (i = 0; i < t; i++) {
    cin >> W >> H;
    cin >> x1 >> y1 >> x2 >> y2;
    cin >> w >> h;
    w0 = x2 - x1;
    h0 = y2 - y1;
    if ((w0 + w > W && h0 + h > H) || ((w0 * h0) + (w * h) > (W * H))) {
      v[i] = -1;
      continue;
    }
    w1 = W - x2;
    w2 = x1;
    h1 = H - y2;
    h2 = y1;
    aw = max(w1, w2);
    ah = max(h1, h2);
    if (aw >= w || ah >= h) {
      v[i] = 0;
      continue;
    }
    sw = min(w1, w2);
    sh = min(h1, h2);
    if (w - aw <= sw && h - ah <= sh) {
      v[i] = min(w - aw, h - ah);
      continue;
    } else if (w - aw > sw) {
      v[i] = h - ah;
      continue;
    } else
      v[i] = w - aw;
  }
  for (i = 0; i < t; i++) {
    cout << setprecision(8) << v[i] << endl;
  }
  return 0;
}
