#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

template <class T1, class T2>
ostream& operator << (ostream& out, pair <T1, T2> p)
{
	out << '(' << p.first << ',' << p.second << ')';
	return out;
}

template <class T1, class T2>
istream& operator >> (istream& in, pair<T1, T2> &p)
{
	in >> p.first >> p.second;
	return in;
}

template <class T>
istream& operator >> (istream& in, vector<T> &v)
{
	for (T &x : v)
		in >> x;
	return in;
}

template <class T>
ostream& operator << (ostream& out, vector<vector<T>> &v)
{
	for (vector<T> &x : v)
		out << x << '\n';
	return out;
}

template <class T>
ostream& operator << (ostream& out, vector<T> &v)
{
	for (T &x : v)
		out << x << ' ';
	return out;
}

long long gcd (long long a, long long b)
{
	if (b > a)
		swap(a, b);
	return (b ? gcd(b, a % b) : a);
}

using ll   = long long;
using pii  = pair<int, int>;
using pll  = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi   = vector<int>;
using vl   = vector<long long>;
using vvi  = vector<vector<int>>;
using vvl  = vector<vector<long long>>;

#define F          first
#define S          second
#define First      first.first
#define Second     first.second
#define Third      second
#define mp         make_pair
#define rep(i,a,b) for (int i = a; i < b; i++)
#define per(i,b,a) for (int i = b; i > a; i--)
#define all(x)     x.begin(), x.end()
#define ret(x)     return cout << x, 0;
#define throwex    throw runtime_error ("Found the error.");

const int h = 1000000007;

ll dist(ll a1, ll a2, ll b1, ll b2)
{
	// cerr << a1 << ' ' << a2 << ' ' << b1 << ' ' << b2 << '\n';
	return abs(a1 - b1) + abs(a2 - b2);
}

ll f(ll a1, ll a2, ll b1, ll b2, int level, ll size)
{
	// cerr << a1 << ' ' << a2 << ' ' << b1 <<  ' ' << b2 << ' ' << level << ' ' << size << '\n';
	if(level == 0)
		return 0;
	
	int sec_index_a_i = a1 / (size / 3);
	int sec_index_a_j = a2 / (size / 3);
	int sec_index_b_i = b1 / (size / 3);
	int sec_index_b_j = b2 / (size / 3);

	if(sec_index_a_i == sec_index_b_i and sec_index_a_j == sec_index_b_j)
		return f(a1 % (size / 3), a2 % (size / 3), b1 % (size / 3), b2 % (size / 3), level - 1, size / 3);

	if(sec_index_a_j == sec_index_b_j)
	{
		swap(a1, a2);
		swap(b1, b2);
	}

	while(size != 1)
	{
		int sec_index_a = a1 / (size / 3);
		int sec_index_b = b1 / (size / 3);
		if(sec_index_a != sec_index_b)
			return dist(a1, a2, b1, b2);
		if(sec_index_a == 1 and abs((a2 / (size / 3)) - (b2 / (size / 3))) > 1)
			return abs(a2 - b2) + min((a1 % (size / 3) + 1 + (b1 % (size / 3)) + 1), 2 * (size / 3) - (a1 % (size / 3) + (b1 % (size / 3))));
		a1 %= (size / 3);
		b1 %= (size / 3);
		size /= 3;
	}
	return abs(a2 - b2);
}

signed main()
{
	ios::sync_with_stdio(false);
	#ifdef ONLINE_JUDGE
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.setstate(ios::failbit);
	#endif

	int q;
	cin >> q;
	while(q--)
	{
		ll a, b, c, d;
		cin >> a >> b >> c >> d;
		a--, b--, c--, d--;
		ll size = 1;
		rep(i,0,30)
			size *= 3;
		cout << f(a, b, c, d, 30, size) << '\n';
	}
}
