#include<ctime>
//#include<cmath>
#include<queue>
#include<bitset>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ls (x<<1)
#define ll long long
using namespace std;
#define rs (x<<1|1)
#define seed 20090115
#define cht 123456789
#define ld long double
#define ull unsigned long long
//#define D(i,j,n)for(int i=j;i>=n;i--)
//#define E(i,now)for(int i=first[now];i;i=e[i].nxt)
//#define F(i,j,n)for(int i=j;i<=n;i++)
#define mem(x,val)memset(x,val,sizeof x)
#define D(i,j,n)for(register int i=j;i>=n;i--)
#define E(i,now)for(register int i=first[now];i;i=e[i].nxt)
#define F(i,j,n)for(register int i=j;i<=n;i++)
ll read(){
	ll ans=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')ans=ans*10+c-'0',c=getchar();
	return ans*f;
}
ll T,W,H,x1,y1,x2,y2,w1,h1,w2,h2,ans,tmp;
int main(){
	ll T=read();
	while(T--){
		W=read();H=read();x1=read();y1=read();x2=read();y2=read();
		w1=x2-x1;h1=y2-y1;w2=read();h2=read();ans=1e9;
		if(w1+w2>W&&h1+h2>H){puts("-1");continue;}
		if(w1+w2<=W){
			ans=min(ans,max(w2-x1,0ll));
			ans=min(ans,max(x2-W+w2,0ll));
		}
		if(h1+h2<=H){
			ans=min(ans,max(h2-y1,0ll));
			ans=min(ans,max(y2-H+h2,0ll));
		}
		printf("%.10lf\n",(double)ans);
	}
	return 0;
}