#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif

using namespace std;


namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y){ return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y){ return x = max(x, y); }
template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}


#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8

LL p3[31] = {1};

LL solve(LL a, LL b, LL c, LL d){
	if(a > c){
		a = p3[30] - 1 - a;
		c = p3[30] - 1 - c;
	}
	if(b > d){
		b = p3[30] - 1 - b;
		d = p3[30] - 1 - d;
	}

	int l;
	for(l = 29; ; --l){
		if(l < 0){
			return c + d - a - b;
		}
		LL e = c / p3[l], f = d / p3[l];
		if(e % 3 != 1){ --e; }
		if(e % 3 != 1){ --e; }
		if(f % 3 != 1){ --f; }
		if(f % 3 != 1){ --f; }
		if(e >= 0 && f >= 0 && (e + 1) * p3[l] - 1 >= a && (f + 1) * p3[l] - 1 >= b){
			auto calc = [](LL x, LL y, LL z){ return abs(x - z) + abs(y - z); };
			LL ret = 0;
			ret += min(calc(a, c, e * p3[l] - 1), calc(a, c, (e + 1) * p3[l]));
			ret += min(calc(b, d, f * p3[l] - 1), calc(b, d, (f + 1) * p3[l]));
			return ret;
		}
	}
}

void mainmain(){
	for(int i = 1; i <= 30; ++i){ p3[i] = p3[i - 1] * 3; }

	int q;
	scanf("%d", &q);
	for(int i = 0; i < q; ++i){
		LL a, b, c, d;
		scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
		LL ans = solve(a - 1, b - 1, c - 1, d - 1);
		printf("%lld\n", ans);
	}
}



}
int main() try{
//	ios::sync_with_stdio(false); cin.tie(0);
	cout << fixed << setprecision(10);
	cerr << fixed << setprecision(4);
	mainmain();
}
catch(...){
	throw;
}
