#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int INF = 1e8;
  int t;
  cin >> t;
  while (t--) {
    int w_room, h_room;
    cin >> w_room >> h_room;
    int x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int w, h;
    cin >> w >> h;
    int shift = 0;
    int horizontal = INF;
    int vertical = INF;
    if (w_room - x2 + x1 >= w) {
      horizontal = w - max(x1, w_room - x2);
      horizontal = max(0, horizontal);
    }
    if (h_room - y2 + y1 >= h) {
      vertical = h - max(y1, h_room - y2);
      vertical = max(0, vertical);
    }
    shift = min(horizontal, vertical);
    if (w_room - x2 + x1 < w && h_room - y2 + y1 < h)
      cout << -1 << endl;
    else
      cout << shift << endl;
  }
}
