#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll p,ans,a,b,c,d,x;
int T;

int main() {
	p=1;for(int i=1;i<=30;i++)p*=3;
	scanf("%d",&T); 
	while(T--) {
		scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
		a--;b--;c--;d--;
		if(abs(a-c)>abs(b-d)) swap(a,b),swap(c,d);
		x=p; ans=abs(a-c)+abs(b-d);
		while(x) {
			ll A=a/x,C=c/x,B=b/x,D=d/x;
			if(A^C) {ans=abs(a-c)+abs(b-d);break;}
			if(A==1&&abs(B-D)>1) {ans=min(a%x+c%x+2,2*x-a%x-c%x)+abs(b-d);break;}
			a%=x;c%=x;x/=3;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
		
