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.*;
  import java.text.DecimalFormat;
  
  public class EduCF_R112_B{
      static class InputReader {
          private static final int DEFAULT_BUFFER_SIZE = 1 << 16; // Change this to increase your input size //
          private static final InputStream DEFAULT_STREAM = System.in;private static final int MAX_DECIMAL_PRECISION = 21;private int c;private byte[] buf;
          private int bufferSize, bufIndex, numBytesRead;private InputStream stream;private static final byte EOF = -1;private static final byte NEW_LINE = 10;
          private static final byte CARRIAGE_RETURN = 13;private static final byte SPACE = 32;private static final byte DASH = 45;private static final byte DOT = 46;
          private char[] charBuffer;private static byte[] bytes = new byte[58];private static int[] ints = new int[58];private static char[] chars = new char[128];
          static { char ch = ' ';int value = 0;byte _byte = 0;for (int i = 48; i < 58; i++) bytes[i] = _byte++;for (int i = 48; i < 58; i++) ints[i] = value++;for (int i = 32; i < 128; i++) chars[i] = ch++; }
          public InputReader() { this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE); }public InputReader(int bufferSize) { this(DEFAULT_STREAM, bufferSize); }public InputReader(InputStream stream) { this(stream, DEFAULT_BUFFER_SIZE); }
          public InputReader(InputStream stream, int bufferSize) { if (stream == null || bufferSize <= 0) throw new IllegalArgumentException();buf = new byte[bufferSize];charBuffer = new char[128];this.bufferSize = bufferSize;this.stream = stream; }
          private byte read() throws IOException { if (numBytesRead == EOF) throw new IOException();if (bufIndex >= numBytesRead) { bufIndex = 0;numBytesRead = stream.read(buf);if (numBytesRead == EOF) return EOF; }return buf[bufIndex++]; }
          private int readJunk(int token) throws IOException { if (numBytesRead == EOF) return EOF;do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > token) return 0;bufIndex++; }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return EOF;bufIndex = 0; } while (true); }
          public byte nextByte() throws IOException {return (byte) ni(); }
          public int ni() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException();int sgn = 1, res = 0;c = buf[bufIndex];if (c == DASH) { sgn = -1;bufIndex++; } do { while (bufIndex < numBytesRead)
          { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1);res += ints[buf[bufIndex++]]; } else { bufIndex++;return res * sgn; } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return res * sgn;bufIndex = 0; } while (true); }
          public long nl() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException();int sgn = 1;long res = 0L;c = buf[bufIndex];if (c == DASH) { sgn = -1;bufIndex++; } do { while (bufIndex < numBytesRead)
          { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1);res += ints[buf[bufIndex++]]; } else { bufIndex++;return res * sgn; } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return res * sgn;bufIndex = 0; } while (true); }
          private void doubleCharBufferSize() { char[] newBuffer = new char[charBuffer.length << 1];for (int i = 0; i < charBuffer.length; i++) newBuffer[i] = charBuffer[i];charBuffer = newBuffer; }
          public String nli() throws IOException { try { c = read(); } catch (IOException e) { return null; }if (c == NEW_LINE) return "";
              if (c == EOF) return null;int i = 0;charBuffer[i++] = (char) c;do { while (bufIndex < numBytesRead) { if (buf[bufIndex] != NEW_LINE && buf[bufIndex] != CARRIAGE_RETURN)
              { if (i == charBuffer.length) doubleCharBufferSize();charBuffer[i++] = (char) buf[bufIndex++]; } else { if(buf[bufIndex] == CARRIAGE_RETURN) bufIndex++;bufIndex++;
                  return new String(charBuffer, 0, i); } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return new String(charBuffer, 0, i);bufIndex = 0; } while (true); }
          public String ns() throws IOException { if (numBytesRead == EOF) return null;if (readJunk(SPACE) == EOF) return null;for (int i = 0; ; )
          { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { if (i == charBuffer.length) doubleCharBufferSize();charBuffer[i++] = (char) buf[bufIndex++]; }
          else {bufIndex++;return new String(charBuffer, 0, i); } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return new String(charBuffer, 0, i);bufIndex = 0; } }
          public double nd() throws IOException { String doubleVal = ns();if (doubleVal == null) throw new IOException();return Double.valueOf(doubleVal); }
          public void close() throws IOException {stream.close(); }
      }
  
      // region variables
      static InputReader sc = new InputReader();
      static OutputStream outputStream = System.out;
      static PrintWriter wr = new PrintWriter(outputStream);
      // endregion
  
      private static void initiateIO()
              throws IOException {if (System.getProperty("ONLINE_JUDGE") == null) {
          try { wr = new PrintWriter("output.txt");
              sc = new InputReader(new FileInputStream("input.txt"));
          } catch (Exception e) { throw new IOException(); }} }
  
      public static void main(String[] args)
              throws IOException {
  
          initiateIO();
  
          int t = sc.ni();
          while(t-- > 0)
              solve();
  
          wr.close();
  
      }
  
      static void solve() throws IOException {
          int w = sc.ni();
          int h = sc.ni();
          int x1 = sc.ni();
          int y1 = sc.ni();
          int x2 = sc.ni();
          int y2 = sc.ni();
          int x3 = sc.ni();
          int y3 = sc.ni();
  
          int dx1 = w - Math.max(x1, x2);
          int dx2 = Math.min(x1, x2);
          int dy1 = h - Math.max(y1, y2);
          int dy2 = Math.min(y1, y2);
  
          int dx = Math.max(dx1, dx2);
          int dy = Math.max(dy1, dy2);
          int dxx = Math.min(dx1, dx2);
          int dyy = Math.min(dy1, dy2);
  
          int xex = x3 - dx;
          int yex = y3 - dy;
  
          if (xex > dxx && yex > dyy) {
              wr.println(-1);
              return;
          }
  
          DecimalFormat df = new DecimalFormat("#.######");
  
          if (xex <= dxx && yex <= dyy) {
              if(xex <= 0) wr.println(0);
              else if(yex <= 0) wr.println(0);
              else wr.println(df.format(Math.min(xex, yex)));
          } else if(xex <= dxx) {
              if(xex <= 0) {
                  wr.println(0);
              } else wr.println(df.format(xex));
          } else {
              if(yex <= 0) {
                  wr.println(0);
              } else wr.println(df.format(yex));
          }
      }
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  