description: You have an axis-aligned rectangle room with width W and height H, so the lower left corner is in point (0, 0) and the upper right corner is in (W, H).
  
  There is a rectangular table standing in this room. The sides of the table are parallel to the walls, the lower left corner is in (x_1, y_1), and the upper right corner in (x_2, y_2).
  
  You want to place another rectangular table in this room with width w and height h with the width of the table parallel to the width of the room.
  
  The problem is that sometimes there is not enough space to place the second table without intersecting with the first one (there are no problems with tables touching, though).
  
  You can't rotate any of the tables, but you can move the first table inside the room. 
  
  <image> Example of how you may move the first table.
  
  What is the minimum distance you should move the first table to free enough space for the second one?
  
  Input
  
  The first line contains the single integer t (1 ≤ t ≤ 5000) — the number of the test cases.
  
  The first line of each test case contains two integers W and H (1 ≤ W, H ≤ 10^8) — the width and the height of the room.
  
  The second line contains four integers x_1, y_1, x_2 and y_2 (0 ≤ x_1 < x_2 ≤ W; 0 ≤ y_1 < y_2 ≤ H) — the coordinates of the corners of the first table.
  
  The third line contains two integers w and h (1 ≤ w ≤ W; 1 ≤ h ≤ H) — the width and the height of the second table.
  
  Output
  
  For each test case, print the minimum distance you should move the first table, or -1 if there is no way to free enough space for the second table.
  
  Your answer will be considered correct if its absolute or relative error doesn't exceed 10^{-6}.
  
  Example
  
  Input
  
  
  5
  8 5
  2 1 7 4
  4 2
  5 4
  2 2 5 4
  3 3
  1 8
  0 3 1 6
  1 5
  8 1
  3 0 6 1
  5 1
  8 10
  4 5 7 8
  8 5
  
  
  Output
  
  
  1.000000000
  -1
  2.000000000
  2.000000000
  0.000000000
  
  Note
  
  The configuration of the first test case is shown in the picture. But the movement of the first table is not optimal. One of the optimal movement, for example, is to move the table by (0, -1), so the lower left corner will move from (2, 1) to (2, 0). Then you can place the second table at (0, 3)-(4, 5).
  
  In the second test case, there is no way to fit both tables in the room without intersecting.
  
  In the third test case, you can move the first table by (0, 2), so the lower left corner will move from (0, 3) to (0, 5).
solution: import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.util.ArrayList;
  import java.util.StringTokenizer;
  
  public class CodeForces {
      static reader input = new reader();
      static PrintWriter output = new PrintWriter(System.out);
  
      public static void main(String[] args) throws IOException {
          /*
          BufferedReader bf = new BufferedReader(new FileReader("input.txt"));
          PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
          StringTokenizer stk= new StringTokenizer(bf.readLine());;
          int n=Integer.parseInt(stk.nextToken());
          */
          B();
          output.close();
      }
  
      public static void A(){
  
      }
  
      public static void B(){
          int t=input.nextInt();
          for(int i=0;i<t;i++){
              int W=input.nextInt(),H=input.nextInt();
              int x1=input.nextInt(),y1=input.nextInt(),x2=input.nextInt(),y2=input.nextInt();
              int w=input.nextInt(),h=input.nextInt();
              if(W-(x2-x1)<w&&H-(y2-y1)<h){
                  output.println(-1);
              }else{
                  if(x1>=w||y1>=h)
                      output.println(0);
                  else if(W-x2>=w||H-y2>=h)
                      output.println(0);
                  else{
                      double min=Long.MAX_VALUE;
                      if(w-x1<=W-x2)
                          min=Math.min(min,w-x1);
                      if(h-y1<=H-y2)
                          min=Math.min(min,h-y1);
                      if(w-(W-x2)<=x1)
                          min=Math.min(min,w-(W-x2));
                      if(h-(H-y2)<=y1)
                      min=Math.min(min,h-(H-y2));
                      output.println(min);
                  }
              }
          }
      }
  
      public static void C(){
  
      }
  
      public static void D(){
  
      }
  
      public static void E(){
  
      }
  
      public static void F(){
  
      }
  
      public static long factorial(int a){
          long ans=1;
          for(int i=1;i<=a;i++){
              ans*=i;
          }
          return ans;
      }
      public static long LCM(long a,long b){
          return (a*b)/GCD(a,b);
      }
      public static long GCD(long a,long b){
          if(a==0)
              return b;
          else if(b==0)
              return a;
          else
              return GCD(b%a,a);
      }
  
      public static boolean isPrime(long num){
          if(num==1)
              return false;
          else if(num==2||num==3)
              return true;
          else if(num%2==0||num%3==0)
              return false;
          else{
              for(long i=5;i*i<=num;i+=6){
                  if(num%i==0||num%(i+2)==0)
                      return false;
              }
          }
          return true;
      }
  
      public static ArrayList<Integer> SieveofEratothenis(){
          ArrayList<Integer>ans=new ArrayList<>();
          boolean visited[]=new boolean[1000000];
          for(int i=2;i<1000000;i++){
              if(!visited[i]){
                  ans.add(i);
                  for(long j=(long)i*i;j<1000000;j+=i){
                      visited[(int)j]=true;
                  }
              }
          }
          return ans;
      }
  
      public static void mergesort(long arr[],int start,int end){//start and end must be indexes
          if(start<end) {
              int mid=(start+end)/2;
              mergesort(arr,start,mid);
              mergesort(arr, mid+1, end);
              merge(arr, start,mid,end);
          }
      }
      public static void merge(long arr[],int start,int mid,int end){
          int lsize=mid-start+1,rsize=end-mid;
          long l[]=new long[lsize],r[]=new long[rsize];
          for(int i=start;i<=mid;i++){
              l[i-start]=arr[i];
          }
          for(int i=mid+1;i<=end;i++){
              r[i-mid-1]=arr[i];
          }
          int i=0,j=0,k=start;
          while(i<lsize&&j<rsize){
              if(l[i]<=r[j]){
                  arr[k++]=l[i++];
              }else{
                  arr[k++]=r[j++];
              }
          }
          while(i<lsize)
              arr[k++]=l[i++];
          while(j<rsize)
              arr[k++]=r[j++];
      }
  }
  
  class Value{
      int ans;
      Value(int ans)
      {
          this.ans=ans;
      }
  }
  
  class Triple{
      int num,size,money;
      Triple(int num,int size,int money){
          this.num=num;
          this.size=size;
          this.money=money;
      }
  }
  
  class Pair{
      int num, size;
      Pair(int num,int size){
          this.size=size;
          this.num=num;
      }
  }
  
  class Op{
      int op,count;
      char dir;
      public Op(int op){
          this.op=op;
      }
  }
  
  class reader {
      BufferedReader br;
      StringTokenizer st;
  
      public reader() {
          br = new BufferedReader(new
                  InputStreamReader(System.in));
      }
  
      String next() {
          while (st == null || !st.hasMoreElements()) {
              try {
                  st = new StringTokenizer(br.readLine());
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return st.nextToken();
      }
  
      int nextInt() {
          return Integer.parseInt(next());
      }
  
      long nextLong() {
          return Long.parseLong(next());
      }
  
      double nextDouble() {
          return Double.parseDouble(next());
      }
  
      String nextLine() {
          String str = "";
          try {
              str = br.readLine();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return str;
      }
  
      public int[] nextIntArray(int arraySize) {
          int array[] = new int[arraySize];
          for (int i = 0; i < arraySize; i++) {
              array[i] = nextInt();
          }
          return array;
      }
  
      public long[] nextLongArray(int arraySize) {
          long array[] = new long[arraySize];
          for (int i = 0; i < arraySize; i++) {
              array[i] = nextLong();
          }
          return array;
      }
  }