#include<bits/stdc++.h>
#define rgi register int
#define ll long long
 
ll a,b,c,d;
inline ll solve()
{
	--a,--b,--c,--d;
	ll res=std::abs(a-c)+std::abs(b-d),t=1LL;
	for(rgi i=1;i<=33;++i) t*=3LL;
	for(rgi i=33;i>=0;--i)
	{
		for(rgi j=0;j<2;++j)
		{
			if(a/t==c/t&&(a/t)%3LL==1LL&&std::abs(b/t-d/t)!=1LL&&std::abs(b-d)>=t)
			{
				ll mn=std::min(a,c),mx=std::max(a,c);
				return res+std::min(mn-mn/t*t+1LL,(mx/t+1LL)*t-mx)*2LL;
			}
			std::swap(a,b),std::swap(c,d);
		}
		t/=3LL;
	}
	return res;
}
 
signed main()
{
	rgi q;
	std::cin>>q;
	while(q--)
	{
		std::cin>>a>>b>>c>>d;
		std::cout<<solve()<<std::endl;
	}
	return 0;
}
// ----------------------------
// by imzzy
