#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int W, H, w, h;
    int x1, x2, y1, y2;
    scanf("%d%d", &W, &H);
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    scanf("%d%d", &w, &h);
    if (w + (x2 - x1) > W && h + (y2 - y1) > H) {
      puts("-1");
      continue;
    }
    if (x1 >= w || y1 >= h || (W - x2) >= w || (H - y2) >= h)
      puts("0.000000000");
    else {
      int wx = max(x1, W - x2);
      int hx = max(y1, H - y2);
      double a = 1e9, b = 1e9;
      if (w + (x2 - x1) <= W && w > wx) a = w - wx;
      if (h + (y2 - y1) <= H && h > hx) b = h - hx;
      printf("%.9lf\n", min(a, b));
    }
  }
  return 0;
}
