#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
struct Node{
int v1,v2,v3,steps;
};
queue<Node>Q;
int V1=0,V2=0,V3=0,E1=0,E2=0,E3=0,visit[105][105][105];
int check(Node temp)
{
if(temp.v1==E1&&temp.v2==E2&&temp.v3==E3)
return 1;
return 0;
}
void BFS()
{
while(!Q.empty()) Q.pop();
memset(visit,0,sizeof(visit));
Node n,temp,temp1;
n.v1=V1,n.v2=0,n.v3=0,n.steps=0;
visit[n.v1][n.v2][n.v3]=1;
Q.push(n);
while(!Q.empty()){
temp1=Q.front(); Q.pop();
if(check(temp1)) {
printf("%d\n",temp1.steps);
return;
}
temp=temp1;
if(temp.v2!=V2&&temp.v1){///pour V1->V2,temp.v1有水且temp.v2未满
if(temp.v1<=V2-temp.v2){///V1 water is not enough
temp.v2+=temp.v1;
temp.v1=0;
}
else{
temp.v1-=(V2-temp.v2);
temp.v2=V2;
}
if(!visit[temp.v1][temp.v2][temp.v3]){
temp.steps+=1;
Q.push(temp);
visit[temp.v1][temp.v2][temp.v3]=1;///最少步数嘛,一定要标记防重复
}
}
temp=temp1;
if(temp.v3!=V3&&temp.v1){///pour V1->V3
if(temp.v1<=V3-temp.v3){///V1 water is not enough
temp.v3+=temp.v1;
temp.v1=0;
}
else{
temp.v1-=(V3-temp.v3);
temp.v3=V3;
}
if(!visit[temp.v1][temp.v2][temp.v3]){
temp.steps+=1;
Q.push(temp);
visit[temp.v1][temp.v2][temp.v3]=1;
}
}
temp=temp1;
if(temp.v1!=V1&&temp.v2){///pour V2->V1
if(temp.v2<=V1-temp.v1){///V2 water is not enough
temp.v1+=temp.v2;
temp.v2=0;
}
else{
temp.v2-=(V1-temp.v1);
temp.v1=V1;
}
if(!visit[temp.v1][temp.v2][temp.v3]){
temp.steps+=1;
Q.push(temp);
visit[temp.v1][temp.v2][temp.v3]=1;
}
}
temp=temp1;
if(temp.v3!=V3&&temp.v2){///pour V2->V3
if(temp.v2<=V3-temp.v3){///V2 water is not enough
temp.v3+=temp.v2;
temp.v2=0;
}
else{
temp.v2-=(V3-temp.v3);
temp.v3=V3;
}
if(!visit[temp.v1][temp.v2][temp.v3]){
temp.steps+=1;
Q.push(temp);
visit[temp.v1][temp.v2][temp.v3]=1;
}
}
temp=temp1;
if(temp.v1!=V1&&temp.v3){///pour V3->V1
if(temp.v3<=V1-temp.v1){///V1 water is not enough
temp.v1+=temp.v3;
temp.v3=0;
}
else{
temp.v3-=(V1-temp.v1);
temp.v1=V1;
}
if(!visit[temp.v1][temp.v2][temp.v3]){
temp.steps+=1;
Q.push(temp);
visit[temp.v1][temp.v2][temp.v3]=1;
}
}
temp=temp1;
if(temp.v2!=V2&&temp.v3){///pour V3->V2
if(temp.v3<=V2-temp.v2){///V1 water is not enough
temp.v2+=temp.v3;
temp.v3=0;
}
else{
temp.v3-=(V2-temp.v2);
temp.v2=V2;
}
if(!visit[temp.v1][temp.v2][temp.v3]){
temp.steps+=1;
Q.push(temp);
visit[temp.v1][temp.v2][temp.v3]=1;
}
}
}
printf("-1\n");
}
int main()
{
int N=0;
scanf("%d",&N);
while(N--){
scanf("%d%d%d%d%d%d",&V1,&V2,&V3,&E1,&E2,&E3);
BFS();
}
return 0;
}
09-06