#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1100;
ll a[4];
int main(){
int _;scanf("%d",&_);
while(_--){
ll x1,y1,z1,x2,y2,z2;
scanf("%lld%lld%lld",&x1,&y1,&z1);
scanf("%lld%lld%lld",&x2,&y2,&z2);
ll x=x1-x2,y=y1-y2,z=z1-z2;
if(x%20||y%20||z%20){
puts("-1");continue;
}
x=x/20,y=y/20,z=z/20;
ll tx=(x%3+3)%3,ty=(y%3+3)%3,tz=(z%3+3)%3;
if(tx!=ty||tx!=tz){
puts("-1");continue;
}
if((x+y+z)!=0){
puts("-1");continue;
}
a[1]=x,a[2]=y,a[3]=z;
int num=0;
if(x>0) num++;
if(y>0) num++;
if(z>0) num++;
if(num==2) a[1]*=(-1),a[2]*=(-1),a[3]*=(-1);
sort(a+1,a+1+3);
ll ans=0;
// cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<endl;
ans=ans+-1*(a[2]);
a[3]-=2*ans;
ans=ans+abs(a[3])/3*2;
printf("%lld\n",ans);
//cout<<ans<<endl;
}
return 0;
}
/*#-2 1 1
4
25 30 35
65 10 15
100 200 300
200 180 220
100 100 100
0 0 0
777 888 999
777 888 999
##ans=
1
3
-1
0
*/