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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

const (
	N = 8
)

func main() {
	sc := newInputScanner(true, 1024)
	Q := sc.nextInt()
	sol := &solution{
		sideLength: uint64(math.Pow(3, N)),
	}
	for i := 0; i < Q; i++ {
		fmt.Println(sol.solve(sc.nextUInt64()-1, sc.nextUInt64()-1, sc.nextUInt64()-1, sc.nextUInt64()-1))
	}
}

type solution struct {
	debuggable
	sideLength uint64
}

func (sol *solution) solve(x1, y1, x2, y2 uint64) uint64 {
	sol.debug(func() {
		fmt.Printf("\nsolving %d %d %d %d\n", x1, y1, x2, y2)
	})
	return sol.abs(x1, x2) + sol.abs(y1, y2) + sol.max(sol.getExtra(x1, y1, x2, y2), sol.getExtra(y1, x1, y2, x2))
}

func (sol *solution) abs(m, n uint64) uint64 {
	if m < n {
		return n - m
	} else {
		return m - n
	}
}

func (spl *solution) max(m, n uint64) uint64 {
	if m < n {
		return n
	} else {
		return m
	}
}

func (spl *solution) min(m, n uint64) uint64 {
	if m < n {
		return m
	} else {
		return n
	}
}

func (sol *solution) getExtra(x1, y1, x2, y2 uint64) uint64 {
	s := sol.sideLength
	var blockX1, blockY1, blockX2, blockY2 uint64
	var left, right uint64
	var result uint64 = 0
	for {
		s /= 3
		if s == 0 {
			break
		}
		blockX1, blockX2 = x1/s, x2/s
		if blockX1 != blockX2 || blockX1%3 != 1 {
			continue
		}
		blockY1, blockY2 = y1/s, y2/s
		if sol.abs(blockY1, blockY2) <= 1 {
			continue
		}
		left = s*blockX1 - 1
		right = s * (blockX1 + 1)
		result = 2 * sol.min(sol.min(x1, x2)-left, right-sol.max(x1, x2))
		sol.debug(func() {
			fmt.Printf("size %d: (%d, %d), (%d, %d); left %d, right %d, result %d\n", s, x1, y1, x2, y2, left, right, result)
		})
	}
	return result
}

type inputScanner struct {
	*bufio.Scanner
}

func newInputScanner(enabledScanWords bool, sizeInKB int) *inputScanner {
	scanner := bufio.NewScanner(os.Stdin)
	if enabledScanWords {
		scanner.Split(bufio.ScanWords)
	}
	capacity := sizeInKB * 1024
	buf := make([]byte, capacity)
	scanner.Buffer(buf, capacity)
	return &inputScanner{
		Scanner: scanner,
	}
}

func (inputSc *inputScanner) nextInt() int {
	i, e := strconv.Atoi(inputSc.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (inputSc *inputScanner) nextUInt64() uint64 {
	i, e := strconv.ParseUint(inputSc.nextStr(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func (inputSc *inputScanner) nextStr() string {
	inputSc.Scan()
	return inputSc.Text()
}

type debuggable struct {
}

const (
	DEBUG_ON = false
)

func (d *debuggable) debug(f func()) {
	if DEBUG_ON {
		f()
	}
}