Problem:
Score : 600 points

Problem Statement

For a non-negative integer K, we define a fractal of level K as follows:

A fractal of level 0 is a grid with just one white square.

When K > 0, a fractal of level K is a 3^K \times 3^K grid. If we divide this grid into nine 3^{K-1} \times 3^{K-1} subgrids:

The central subgrid consists of only black squares.

Each of the other eight subgrids is a fractal of level K-1.

For example, a fractal of level 2 is as follows:

In a fractal of level 30, let (r, c) denote the square at the r-th row from the top and the c-th column from the left.

You are given Q quadruples of integers (a_i, b_i, c_i, d_i).
For each quadruple, find the distance from (a_i, b_i) to (c_i, d_i).

Here the distance from (a, b) to (c, d) is the minimum integer n that satisfies the following condition:

There exists a sequence of white squares (x_0, y_0), \ldots, (x_n, y_n) satisfying the following conditions:

(x_0, y_0) = (a, b)

(x_n, y_n) = (c, d)

For every i (0 \leq i \leq n-1), (x_i, y_i) and (x_{i+1}, y_{i+1}) share a side.

Constraints

1 \leq Q \leq 10000

1 \leq a_i, b_i, c_i, d_i \leq 3^{30}

(a_i, b_i) \neq (c_i, d_i)

(a_i, b_i) and (c_i, d_i) are white squares.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
a_1 \ b_1 \ c_1 \ d_1
:
a_Q \ b_Q \ c_Q \ d_Q

Output

Print Q lines.
The i-th line should contain the distance from (a_i, b_i) to (c_i, d_i).

Sample Input 1

2
4 2 7 4
9 9 1 9

Sample Output 1

5
8

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=505;
const ll INF=1LL<<60;
ll rui[MAX];

void nor(ll &h1,ll &w1,ll &h2,ll &w2,ll level){
    if(h1>h2){
        h1=rui[level]-1-h1;
        h2=rui[level]-1-h2;
    }
    if(w1>w2){
        w1=rui[level]-1-w1;
        w2=rui[level]-1-w2;
    }
}

ll findmi(ll s,ll level){
    if(level==0) return 0;
    ll ba1=rui[level-1];
    
    ll ps=s/ba1;
    s%=ba1;
    
    if(ps==0) return findmi(s,level-1);
    if(ps==1) return ba1-1;
    if(ps==2) return ba1*2+findmi(s,level-1);
    
    return 0;
}

ll findma(ll s,ll level){
    if(level==0) return 0;
    ll ba1=rui[level-1];
    
    ll ps=s/ba1;
    s%=ba1;
    
    if(ps==0) return findma(s,level-1);
    if(ps==1) return ba1*2;
    if(ps==2) return ba1*2+findma(s,level-1);
    
    return 0;
}

ll solve(ll h1,ll w1,ll h2,ll w2,ll level){
    if(level==0) return 0;
    
    ll ba1=rui[level-1],ba2=rui[level];
    
    nor(h1,w1,h2,w2,level);
    
    if(h1==0&&w1==0){
        return h2+w2;
    }
    if(h1==0&&w1==ba2-1){
        return h2;
    }
    if(h1==ba2-1&&w1==0){
        return w2;
    }
    if(h2==ba2-1&&w2==ba2-1){
        return ba2-1-h1+ba2-1-w1;
    }
    if(h2==0&&w2==ba2-1){
        return ba2-1-w1;
    }
    if(h2==ba2-1&&w2==0){
        return ba2-1-h1;
    }
    
    ll ph1,pw1,ph2,pw2;
    ph1=h1/ba1;
    pw1=w1/ba1;
    ph2=h2/ba1;
    pw2=w2/ba1;
    
    h1%=ba1;
    w1%=ba1;
    h2%=ba1;
    w2%=ba1;
    
    if(ph1!=ph2&&pw1!=pw2){
        ll a=solve(h1,w1,ba1-1,ba1-1,level-1);
        ll b=solve(h2,w2,0,0,level-1);
        
        return a+b+2+(ph2-ph1-1+pw2-pw1-1)*ba1;
    }
    
    if(ph1==ph2&&pw1==pw2){
        return solve(h1,w1,h2,w2,level-1);
    }
    
    if(ph1==ph2){
        if(ph1==1){
            ll a1=solve(h1,w1,0,ba1-1,level-1),a2=solve(h1,w1,ba1-1,ba1-1,level-1);
            ll b1=solve(h2,w2,0,0,level-1),b2=solve(h2,w2,ba1-1,0,level-1);
            
            return min(a1+b1+ba1+2,a2+b2+ba1+2);
        }else{
            ll mi1=findmi(h1,level-1),mi2=findmi(h2,level-1);
            ll ma1=findma(h1,level-1),ma2=findma(h2,level-1);
            
            ll res=INF;
            
            ll a1=solve(h1,w1,mi1,ba1-1,level-1),a2=solve(h1,w1,ma1,ba1-1,level-1);
            ll b1=solve(h2,w2,mi2,0,level-1),b2=solve(h2,w2,ma2,0,level-1);
            
            chmin(res,a1+b1+abs(mi2-mi1)+1+ba1);
            chmin(res,a1+b2+abs(ma2-mi1)+1+ba1);
            chmin(res,a2+b1+abs(mi2-ma1)+1+ba1);
            chmin(res,a2+b2+abs(ma2-ma1)+1+ba1);
            
            return res;
        }
    }
    
    if(pw1==pw2){
        if(pw1==1){
            ll a1=solve(h1,w1,ba1-1,0,level-1),a2=solve(h1,w1,ba1-1,ba1-1,level-1);
            ll b1=solve(h2,w2,0,0,level-1),b2=solve(h2,w2,0,ba1-1,level-1);
            
            return min(a1+b1+ba1+2,a2+b2+ba1+2);
        }else{
            ll mi1=findmi(w1,level-1),mi2=findmi(w2,level-1);
            ll ma1=findma(w1,level-1),ma2=findma(w2,level-1);
            
            ll res=INF;
            
            ll a1=solve(h1,w1,ba1-1,mi1,level-1),a2=solve(h1,w1,ba1-1,ma1,level-1);
            ll b1=solve(h2,w2,0,mi2,level-1),b2=solve(h2,w2,0,ma2,level-1);
            
            chmin(res,a1+b1+abs(mi2-mi1)+1+ba1);
            chmin(res,a1+b2+abs(ma2-mi1)+1+ba1);
            chmin(res,a2+b1+abs(mi2-ma1)+1+ba1);
            chmin(res,a2+b2+abs(ma2-ma1)+1+ba1);
            
            return res;
        }
    }
    
    return 0;
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    rui[0]=1;
    for(int k=1;k<=30;k++) rui[k]=rui[k-1]*3;
    
    int Q;cin>>Q;
    while(Q--){
        ll h1,w1,h2,w2;cin>>h1>>w1>>h2>>w2;
        h1--;w1--;h2--;w2--;
        nor(h1,w1,h2,w2,30);
        
        cout<<solve(h1,w1,h2,w2,30)<<"\n";
    }
}