import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class node
{
int v1,v2,v3,step;
public node(int v1,int v2,int v3,int step) {
this.v1=v1;
this.v2=v2;
this.v3=v3;
this.step=step;
}
}
public class Main {
static int v1,v2,v3,vv1,vv2,vv3;
static boolean vis[][][];
public static int bfs()
{
vis=new boolean[110][110][110];
Queue<node>queue=new LinkedList<node>();
node first=new node(v1,0,0,0);
queue.add(first);
while(!queue.isEmpty())
{
first=queue.remove();
vis[first.v1][first.v2][first.v3]=true;
if(first.v1==vv1&&first.v2==vv2&&first.v3==vv3)
{
return first.step;
}
if(first.v1>0&&first.v2<v2)
{
node next=new node(first.v1, first.v2, first.v3, first.step);
int x=next.v1>(v2-next.v2)?(v2-next.v2):next.v1;
next.v1-=x;
next.v2+=x;
if(!vis[next.v1][next.v2][next.v3])
{
vis[next.v1][next.v2][next.v3]=true;
next.step++;
queue.add(next);
}
}
if(first.v1>0&&first.v3<v3)
{
node next=new node(first.v1, first.v2, first.v3, first.step);
int x=next.v1>(v3-next.v3)?(v3-next.v3):next.v1;
next.v1-=x;
next.v3+=x;
if(!vis[next.v1][next.v2][next.v3])
{
vis[next.v1][next.v2][next.v3]=true;
next.step++;
queue.add(next);
}
}
if(first.v2>0&&first.v3<v3)
{
node next=new node(first.v1, first.v2, first.v3, first.step);
int x=next.v2>(v3-next.v3)?(v3-next.v3):next.v2;
next.v2-=x;
next.v3+=x;
if(!vis[next.v1][next.v2][next.v3])
{
vis[next.v1][next.v2][next.v3]=true;
next.step++;
queue.add(next);
}
}
if(first.v2>0&&first.v1<v1)
{
node next=new node(first.v1, first.v2, first.v3, first.step);
int x=next.v2>(v1-next.v1)?(v1-next.v1):next.v2;
next.v2-=x;
next.v1+=x;
if(!vis[next.v1][next.v2][next.v3])
{
vis[next.v1][next.v2][next.v3]=true;
next.step++;
queue.add(next);
}
}
if(first.v3>0&&first.v1<v1)
{
node next=new node(first.v1, first.v2, first.v3, first.step);
int x=next.v3>(v1-next.v1)?(v1-next.v1):next.v3;
next.v3-=x;
next.v1+=x;
if(!vis[next.v1][next.v2][next.v3])
{
vis[next.v1][next.v2][next.v3]=true;
next.step++;
queue.add(next);
}
}
if(first.v3>0&&first.v2<v2)
{
node next=new node(first.v1, first.v2, first.v3, first.step);
int x=next.v3>(v2-next.v2)?(v2-next.v2):next.v3;
next.v3-=x;
next.v2+=x;
if(!vis[next.v1][next.v2][next.v3])
{
vis[next.v1][next.v2][next.v3]=true;
next.step++;
queue.add(next);
}
}
}
return -1;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int cases=scanner.nextInt();
while(cases--!=0)
{
v1=scanner.nextInt();
v2=scanner.nextInt();
v3=scanner.nextInt();
vv1=scanner.nextInt();
vv2=scanner.nextInt();
vv3=scanner.nextInt();
if(v1<vv1+vv2+vv3)
{
System.out.println("-1");continue;
}
System.out.println(bfs());
}
}
}