#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int va, vb, vc, ta, tb, tc;
bool visit[205][205][205];
struct state
{
int a;
int b;
int c;
int step;
};
inline bool check(state t)
{
if(t.a == ta && t.b == tb && t.c == tc)
return true;
return false;
}
int BFS(int sa, int sb, int sc)
{
queue <state> q;
state t1, t2;
t1.a = sa;
t1.b = sb;
t1.c = sc;
t1.step = 0;
q.push(t1);
while(!q.empty())
{
t1 = q.front();
q.pop();
if(t1.b != vb && t1.a) // pour va to vb
{
if(t1.a <= vb - t1.b)
{
t2.a = 0;
t2.b = t1.b + t1.a;
t2.c = t1.c;
}
else
{
t2.a = t1.a - (vb - t1.b);
t2.b = vb;
t2.c = t1.c;
}
if(!visit[t2.a][t2.b][t2.c])
{
t2.step = t1.step + 1;
if(check(t2))
return t2.step;
visit[t2.a][t2.b][t2.c] = true;
q.push(t2);
}
}
if(t1.c != vc && t1.a) // pour va to vc
{
if(t1.a <= vc - t1.c)
{
t2.a = 0;
t2.b = t1.b;
t2.c = t1.c + t1.a;
}
else
{
t2.a = t1.a - (vc - t1.c);
t2.b = t1.b;
t2.c = vc;
}
if(!visit[t2.a][t2.b][t2.c])
{
t2.step = t1.step + 1;
if(check(t2))
return t2.step;
visit[t2.a][t2.b][t2.c] = true;
q.push(t2);
}
}
if(t1.a != va && t1.b) // pour vb to va
{
if(t1.b <= va - t1.a)
{
t2.b = 0;
t2.a = t1.b + t1.a;
t2.c = t1.c;
}
else
{
t2.b = t1.b - (va - t1.a);
t2.a = va;
t2.c = t1.c;
}
if(!visit[t2.a][t2.b][t2.c])
{
t2.step = t1.step + 1;
if(check(t2))
return t2.step;
visit[t2.a][t2.b][t2.c] = true;
q.push(t2);
}
}
if(t1.a != va && t1.b) // pour vb to vc
{
if(t1.b <= vc - t1.c)
{
t2.b = 0;
t2.a = t1.a;
t2.c = t1.b + t1.c;
}
else
{
t2.b = t1.b - (vc - t1.c);
t2.a = t1.a;
t2.c = vc;
}
if(!visit[t2.a][t2.b][t2.c])
{
t2.step = t1.step + 1;
if(check(t2))
return t2.step;
visit[t2.a][t2.b][t2.c] = true;
q.push(t2);
}
}
if(t1.b != vb && t1.c) // pour vc to vb
{
if(t1.c <= vb - t1.b)
{
t2.c = 0;
t2.b = t1.b + t1.c;
t2.a = t1.a ;
}
else
{
t2.c = t1.c - (vb - t1.b);
t2.b = vb;
t2.a = t1.a;
}
if(!visit[t2.a][t2.b][t2.c])
{
t2.step = t1.step + 1;
if(check(t2))
return t2.step;
visit[t2.a][t2.b][t2.c] = true;
q.push(t2);
}
}
if(t1.a != va && t1.c) // pour vc to va
{
if(t1.c <= va - t1.a)
{
t2.c = 0;
t2.a = t1.a + t1.c;
t2.b = t1.b ;
}
else
{
t2.c = t1.c - (va - t1.a);
t2.a = va;
t2.b = t1.b;
}
if(!visit[t2.a][t2.b][t2.c])
{
t2.step = t1.step + 1;
if(check(t2))
return t2.step;
visit[t2.a][t2.b][t2.c] = true;
q.push(t2);
}
}
}
return -1;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(visit, false, sizeof(visit));
scanf("%d %d %d", &va, &vb, &vc);
scanf("%d %d %d", &ta, &tb, &tc);
if(va == ta && 0 == tb && 0 == tc)
{
printf("0\n");
continue;
}
visit[va][0][0] = true;
printf("%d\n", BFS(va, 0, 0));
}
return 0;
}