def R(): return map(int, input().split())
def I(): return int(input())
def S(): return str(input())

def L(): return list(R())

from collections import Counter

import math
import sys

from itertools import permutations

import bisect

mod=10**9+7

#print(bisect.bisect_right([1,2,3],2))
#print(bisect.bisect_left([1,2,3],2))

n,d=R()

x=L()+[1e10]

x.sort()

l=0
r=0
ans=0
while r<n:
    while r+1<=n and x[r+1]-x[l]<=d: r+=1

    ans+=(r-l-1)*(r-l)//2

    l+=1

print(ans)


from sys import *
from math import *
n,k=map(int,stdin.readline().split())
a=list(map(int,stdin.readline().split()))
ans=0
for i in range(n-2):
    x=a[i]+k
    l=i+2
    h=n-1
    j=0
    f=0
    while l<=h:
        m=(l+h)//2
        if a[m]>x:
            h=m-1
        elif a[m]<=x:
            j=l
            l=m+1
            f=1
    if f==1:
        y=j-i-2
        ans+=(y+1)*(y+2)//2
print(ans)


from sys import *
from math import *
n,k=map(int,stdin.readline().split())
a=list(map(int,stdin.readline().split()))
ans=0
for i in range(n-2):
    x=a[i]+k
    l=i+2
    h=n-1
    j=0
    f=0
    while l<=h:
        m=(l+h)//2
        if a[m]>x:
            h=m-1
        elif a[m]<=x:
            j=m
            l=m+1
            f=1
    if f==1:
        y=j-i-2
        ans+=(y+1)*(y+2)//2
print(ans)


n,d = map(int,input().split())
arr = list(map(int,input().split()))
import math

def C(n,k):
    if n < k: return 0
    return math.factorial(n) // (math.factorial(k)*math.factorial(n-k))
i = 0
j = 1
cnt = 1
ans = 0
while i < n and j < n:
    if arr[j] - arr[i] > d:
        i += 1
    else:
        cnt = j - i
        j += 1
        ans = ans + cnt*(cnt-1) // 2

print(ans)


n,d = map(int,input().split())
arr = list(map(int,input().split()))
import math

def C(n,k):
    if n < k: return 0
    return math.factorial(n) // (math.factorial(k)*math.factorial(n-k))
i = 0
j = 1
cnt = 1
ans = 0
while i < n and j < n:
    if arr[j] - arr[i] > d:
        i += 1
    else:
        cnt = j - i
        j += 1
        ans = ans + C(cnt,2)

print(ans)


n,d = map(int,input().split())
arr = list(map(int,input().split()))
import math

def C(n,k):
    if n < k: return 0
    return math.factorial(n) // (math.factorial(k)*math.factorial(n-k))
i = 0
j = 1
cnt = 1
ans = 0
while i < n and j < n:
    if arr[j] - arr[i] > d:
        i += 1
        j += 1
        if cnt >= 3:
            ans = ans + C(cnt,3) - C(cnt - 1,3)
        if j == n-1:
            ans += C(cnt - 1,3)
            break
    else:
        cnt += 1
        j += 1
else:
    if cnt >= 3:
        ans += C(cnt,3)
print(ans)


#include <bits/stdc++.h>
#include <algorithm>
#include <vector>
using namespace std;
//int maxProductSubarrayOfSizeK(int A[], int n, int k)
//{
//	sort(A, A + n);
//	int product = 1;
//	if (A[n - 1] == 0 && (k & 1))
//		return 0;
//	if (A[n - 1] <= 0 && (k & 1)) {
//		for (int i = n - 1; i >= n - k; i--)
//			product *= A[i];
//		return product;
//	}
//	int i = 0;
//	int j = n - 1;
//	if (k & 1) {
//		product *= A[j];
//		j--;
//		k--;
//	}
//	k >>= 1;
//	for (int itr = 0; itr < k; itr++) {
//		int left_product = A[i] * A[i + 1];
//		int right_product = A[j] * A[j - 1];
//		if (left_product > right_product) {
//			product *= left_product;
//			i += 2;
//		}
//		else {
//			product *= right_product;
//			j -= 2;
//		}
//	}
//	return product;
//}
//int main()
//{
//	int t;
//	cin>>t;
//	while(t--)
//	{
//		int n,k=5;
//		cin>>n;
//		int a[n];
//		for(int i=0;i<n;i++)
//		{
//			cin>>a[i];
//		}
//		cout<<maxProductSubarrayOfSizeK(a, n, k);
//		cout<<'\n';
//	}
//	return 0;
//
//}

//int main()
//{
//	int t,x;
//	cin>>t;
//	while(t--)
//	{
//		int n;
//		cin>>n;
//		for(int i=0;i<n;i++)
//		{
//			cin>>x;
//		}
//		if(x%2==0)
//		{
//			cout<<"2"<<endl;
//		}
//		else
//			cout<<"1"<<endl;
//	}
//	return 0;
//}

//int main()
//{
//	int t;
//	cin>>t;
//	while(t--)
//	{
//		int n,a[100];
//		cin>>n;
//		for(int i=0;i<n;i++)
//		{
//			cin>>a[i];
//		}
//	}
//}

//int minSwaps(long long arr[], long long n)
//{
//	pair<long, long> arrPos[n];
//	for (int i = 0; i < n; i++)
//	{
//		arrPos[i].first = arr[i];
//		arrPos[i].second = i;
//	}
//	sort(arrPos, arrPos + n);
//	vector<bool> vis(n, false);
//	int ans = 0;
//	for (int i = 0; i < n; i++)
//	{
//		if (vis[i] || arrPos[i].second == i)
//			continue;
//		int cycle_size = 0;
//		int j = i;
//		while (!vis[j])
//		{
//			vis[j] = 1;
//			j = arrPos[j].second;
//			cycle_size++;
//		}
//		if (cycle_size > 0)
//		{
//			ans += (cycle_size - 1);
//		}
//	}
//	return ans;
//}
//int main()
//{
//	int t;
//	long long n,arr[50000];
//	cin>>t;
//	while(t--)
//	{
//		cin>>n;
//		for(int i=0;i<n;i++)
//		{
//			cin>>arr[i];
//		}
//	long long x=minSwaps(arr, n);
//	cout<<x<<endl;
//	long long y=(n*(n-1)/2)-1;
//	if(x<=y)
//	{
//		cout<<"YES"<<endl;
//	}
//	else
//		cout<<"NO"<<endl;
//	}
//	return 0;
//}

//int main()
//{
//	int t;
//	while(t--)
//	{
//		int n,c=0,a[];
//		cin>>n;
//		for (int i=0;i<n;i++)
//		{
//
//		}
//	}
//}

//int main()
//{
//	int t;
//	cin>>t;
//	while (t--)
//	{
//		long long arr[3];
//		for(int i=0;i<3;i++)
//		{
//			cin>>arr[i];
//		}
//    	sort(arr, arr+3);
//        if((arr[0]+arr[1]+arr[2])/2<arr[2])
//        {
//        	long long x = sqrt(arr[2]*arr[2]-arr[0]*arr[0]-arr[1]*arr[1]);
//        	cout<<x<<endl;
//		}
//		else
//			{
//				long long y=sqrt(arr[2]*arr[2]+arr[0]*arr[0]+arr[1]*arr[1])+1;
//				cout<<y<<endl;
//			}
//	}
//}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long a,b,c,d;
		cin>>a>>b>>c>>d;
		if(a==c)
		{
			cout<<abs(b-d)<<endl;
		}
		else if(b==d)
		{
			cout<<abs(a-c)<<endl;
		}
		else if(a==b && c==d)
		{
			if(a==c+1 || c==a+1)
			{
				cout<<4<<endl;
			}
			else
			{
				if(a>c)
				{
					cout<<((a*2)+1)<<endl;
				}
				else
					cout<<((c*2)+1)<<endl;

			}
		}
		else
		{
			cout<<abs(a-c)+abs(b-d)<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#include <algorithm>
#include <vector>
using namespace std;
//int maxProductSubarrayOfSizeK(int A[], int n, int k)
//{
//	sort(A, A + n);
//	int product = 1;
//	if (A[n - 1] == 0 && (k & 1))
//		return 0;
//	if (A[n - 1] <= 0 && (k & 1)) {
//		for (int i = n - 1; i >= n - k; i--)
//			product *= A[i];
//		return product;
//	}
//	int i = 0;
//	int j = n - 1;
//	if (k & 1) {
//		product *= A[j];
//		j--;
//		k--;
//	}
//	k >>= 1;
//	for (int itr = 0; itr < k; itr++) {
//		int left_product = A[i] * A[i + 1];
//		int right_product = A[j] * A[j - 1];
//		if (left_product > right_product) {
//			product *= left_product;
//			i += 2;
//		}
//		else {
//			product *= right_product;
//			j -= 2;
//		}
//	}
//	return product;
//}
//int main()
//{
//	int t;
//	cin>>t;
//	while(t--)
//	{
//		int n,k=5;
//		cin>>n;
//		int a[n];
//		for(int i=0;i<n;i++)
//		{
//			cin>>a[i];
//		}
//		cout<<maxProductSubarrayOfSizeK(a, n, k);
//		cout<<'\n';
//	}
//	return 0;
//
//}

//int main()
//{
//	int t,x;
//	cin>>t;
//	while(t--)
//	{
//		int n;
//		cin>>n;
//		for(int i=0;i<n;i++)
//		{
//			cin>>x;
//		}
//		if(x%2==0)
//		{
//			cout<<"2"<<endl;
//		}
//		else
//			cout<<"1"<<endl;
//	}
//	return 0;
//}

//int main()
//{
//	int t;
//	cin>>t;
//	while(t--)
//	{
//		int n,a[100];
//		cin>>n;
//		for(int i=0;i<n;i++)
//		{
//			cin>>a[i];
//		}
//	}
//}

//int minSwaps(long long arr[], long long n)
//{
//	pair<long, long> arrPos[n];
//	for (int i = 0; i < n; i++)
//	{
//		arrPos[i].first = arr[i];
//		arrPos[i].second = i;
//	}
//	sort(arrPos, arrPos + n);
//	vector<bool> vis(n, false);
//	int ans = 0;
//	for (int i = 0; i < n; i++)
//	{
//		if (vis[i] || arrPos[i].second == i)
//			continue;
//		int cycle_size = 0;
//		int j = i;
//		while (!vis[j])
//		{
//			vis[j] = 1;
//			j = arrPos[j].second;
//			cycle_size++;
//		}
//		if (cycle_size > 0)
//		{
//			ans += (cycle_size - 1);
//		}
//	}
//	return ans;
//}
//int main()
//{
//	int t;
//	long long n,arr[50000];
//	cin>>t;
//	while(t--)
//	{
//		cin>>n;
//		for(int i=0;i<n;i++)
//		{
//			cin>>arr[i];
//		}
//	long long x=minSwaps(arr, n);
//	cout<<x<<endl;
//	long long y=(n*(n-1)/2)-1;
//	if(x<=y)
//	{
//		cout<<"YES"<<endl;
//	}
//	else
//		cout<<"NO"<<endl;
//	}
//	return 0;
//}

//int main()
//{
//	int t;
//	while(t--)
//	{
//		int n,c=0,a[];
//		cin>>n;
//		for (int i=0;i<n;i++)
//		{
//
//		}
//	}
//}

//int main()
//{
//	int t;
//	cin>>t;
//	while (t--)
//	{
//		long long arr[3];
//		for(int i=0;i<3;i++)
//		{
//			cin>>arr[i];
//		}
//    	sort(arr, arr+3);
//        if((arr[0]+arr[1]+arr[2])/2<arr[2])
//        {
//        	long long x = sqrt(arr[2]*arr[2]-arr[0]*arr[0]-arr[1]*arr[1]);
//        	cout<<x<<endl;
//		}
//		else
//			{
//				long long y=sqrt(arr[2]*arr[2]+arr[0]*arr[0]+arr[1]*arr[1])+1;
//				cout<<y<<endl;
//			}
//	}
//}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long a,b,c,d;
		cin>>a>>b>>c>>d;
		cout<<(((a-c)*(a-c)+(b-d)*(b-d))*((a-c)*(a-c)+(b-d)*(b-d)))<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#include <algorithm>
#include <vector>
using namespace std;
//int maxProductSubarrayOfSizeK(int A[], int n, int k)
//{
//	sort(A, A + n);
//	int product = 1;
//	if (A[n - 1] == 0 && (k & 1))
//		return 0;
//	if (A[n - 1] <= 0 && (k & 1)) {
//		for (int i = n - 1; i >= n - k; i--)
//			product *= A[i];
//		return product;
//	}
//	int i = 0;
//	int j = n - 1;
//	if (k & 1) {
//		product *= A[j];
//		j--;
//		k--;
//	}
//	k >>= 1;
//	for (int itr = 0; itr < k; itr++) {
//		int left_product = A[i] * A[i + 1];
//		int right_product = A[j] * A[j - 1];
//		if (left_product > right_product) {
//			product *= left_product;
//			i += 2;
//		}
//		else {
//			product *= right_product;
//			j -= 2;
//		}
//	}
//	return product;
//}
//int main()
//{
//	int t;
//	cin>>t;
//	while(t--)
//	{
//		int n,k=5;
//		cin>>n;
//		int a[n];
//		for(int i=0;i<n;i++)
//		{
//			cin>>a[i];
//		}
//		cout<<maxProductSubarrayOfSizeK(a, n, k);
//		cout<<'\n';
//	}
//	return 0;
//
//}

//int main()
//{
//	int t,x;
//	cin>>t;
//	while(t--)
//	{
//		int n;
//		cin>>n;
//		for(int i=0;i<n;i++)
//		{
//			cin>>x;
//		}
//		if(x%2==0)
//		{
//			cout<<"2"<<endl;
//		}
//		else
//			cout<<"1"<<endl;
//	}
//	return 0;
//}

//int main()
//{
//	int t;
//	cin>>t;
//	while(t--)
//	{
//		int n,a[100];
//		cin>>n;
//		for(int i=0;i<n;i++)
//		{
//			cin>>a[i];
//		}
//	}
//}

//int minSwaps(long long arr[], long long n)
//{
//	pair<long, long> arrPos[n];
//	for (int i = 0; i < n; i++)
//	{
//		arrPos[i].first = arr[i];
//		arrPos[i].second = i;
//	}
//	sort(arrPos, arrPos + n);
//	vector<bool> vis(n, false);
//	int ans = 0;
//	for (int i = 0; i < n; i++)
//	{
//		if (vis[i] || arrPos[i].second == i)
//			continue;
//		int cycle_size = 0;
//		int j = i;
//		while (!vis[j])
//		{
//			vis[j] = 1;
//			j = arrPos[j].second;
//			cycle_size++;
//		}
//		if (cycle_size > 0)
//		{
//			ans += (cycle_size - 1);
//		}
//	}
//	return ans;
//}
//int main()
//{
//	int t;
//	long long n,arr[50000];
//	cin>>t;
//	while(t--)
//	{
//		cin>>n;
//		for(int i=0;i<n;i++)
//		{
//			cin>>arr[i];
//		}
//	long long x=minSwaps(arr, n);
//	cout<<x<<endl;
//	long long y=(n*(n-1)/2)-1;
//	if(x<=y)
//	{
//		cout<<"YES"<<endl;
//	}
//	else
//		cout<<"NO"<<endl;
//	}
//	return 0;
//}

//int main()
//{
//	int t;
//	while(t--)
//	{
//		int n,c=0,a[];
//		cin>>n;
//		for (int i=0;i<n;i++)
//		{
//
//		}
//	}
//}

//int main()
//{
//	int t;
//	cin>>t;
//	while (t--)
//	{
//		long long arr[3];
//		for(int i=0;i<3;i++)
//		{
//			cin>>arr[i];
//		}
//    	sort(arr, arr+3);
//        if((arr[0]+arr[1]+arr[2])/2<arr[2])
//        {
//        	long long x = sqrt(arr[2]*arr[2]-arr[0]*arr[0]-arr[1]*arr[1]);
//        	cout<<x<<endl;
//		}
//		else
//			{
//				long long y=sqrt(arr[2]*arr[2]+arr[0]*arr[0]+arr[1]*arr[1])+1;
//				cout<<y<<endl;
//			}
//	}
//}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long a,b,c,d;
		cin>>a>>b>>c>>d;
		if(a==c && b!=d)
		{
			cout<<abs(b-d)<<endl;
		}
		else if(b==d && a!=c)
		{
			cout<<abs(a-c)<<endl;
		}
		else if(a==b && c==d)
		{
			if(a==c+1 || c==a+1)
			{
				cout<<4<<endl;
			}
			else
			{
				if(a>c)
				{
					cout<<((a*2)+1)<<endl;
				}
				else
					cout<<((c*2)+1)<<endl;

			}
		}
		else
		{
			cout<<abs(a-c)+abs(b-d)<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll t;
	cin>>t;
	while(t--)
	{
		ll x1,y1,x2,y2;
		cin>>x1>>y1>>x2>>y2;
		if(x1==x2 || y1==y2)
		{
			cout<<abs(x1-x2)+abs(y1-y2)<<"\n";
		}
		else
		{
			cout<<abs(x1-x2)+abs(y1-y2)+2<<"\n";
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int t=0;
    cin>>t;
    while(t--){
        long long int x1=0,y1=0,x2=0,y2=0;
        cin>>x1>>y1>>x2>>y2;
        long long int temp1=x2-x1,temp2=y2-y1;
        if((temp1==0)&&(temp2==0)){
            cout<<0<<endl;
        }
        else if((temp1!=0)&&(temp2==0)){
            cout<<(temp1)<<endl;
        }
        else if((temp1==0)&&(temp2!=0)){
            cout<<temp2<<endl;
        }
        else if((temp1!=0)&&(temp2!=0)){
            cout<<(temp1+temp2+2)<<endl;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int t=0;
    cin>>t;
    while(t--){
        long long int x1=0,y1=0,x2=0,y2=0;
        cin>>x1>>y1>>x2>>y2;
        long long int temp1=0,temp2=0,ans=0;
        if(x1>=x2){
            temp1=x1-x2;
        }
        else if(x1<x2){
            temp1=x2-x1;
        }
        if(y1>=y2){
            temp2=y1-y2;
        }
        else{
            temp2=y2-y1;
        }
        if((temp1==0)&&(temp2==0)){
            ans=0;
        }
        else if((temp1!=0)&&(temp2==0)){
            ans=temp1;
        }
        else if((temp1==0)&&(temp2!=0)){
            ans=temp2;
        }
        else if((temp1!=0)&&(temp2!=0)){
            ans=temp1+temp2+2;
        }
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int t=0;
    cin>>t;
    while(t--){
        long long int x1=0,y1=0,x2=0,y2=0;
        cin>>x1>>y1>>x2>>y2;
        long long int temp1=x2-x1,temp2=y2-y1,ans=0;
        if((temp1<0)){
            temp1=-temp1;
        }
        else if(temp2<0){
            temp2=-temp2;
        }
        if((temp1==0)&&(temp2==0)){
            ans=0;
        }
        else if((temp1!=0)&&(temp2==0)){
            ans=temp1;
        }
        else if((temp1==0)&&(temp2!=0)){
            ans=temp2;
        }
        else if((temp1!=0)&&(temp2!=0)){
            ans=temp1+temp2+2;
        }
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int t=0;
    cin>>t;
    while(t--){
        long long int x1=0,y1=0,x2=0,y2=0;
        cin>>x1>>y1>>x2>>y2;
        long long int temp1=x2-x1,temp2=y2-y1;
        if((temp1<0)){
            temp1=-temp1;
        }
        else if(temp2<0){
            temp2=-temp2;
        }
        if((temp1==0)&&(temp2==0)){
            cout<<0<<endl;
        }
        else if((temp1!=0)&&(temp2==0)){
            cout<<(temp1)<<endl;
        }
        else if((temp1==0)&&(temp2!=0)){
            cout<<temp2<<endl;
        }
        else if((temp1!=0)&&(temp2!=0)){
            cout<<(temp1+temp2+2)<<endl;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int t=0;
    cin>>t;
    while(t--){
        int x1=0,y1=0,x2=0,y2=0;
        cin>>x1>>y1>>x2>>y2;
        int temp1=x2-x1,temp2=y2-y1;
        if((temp1==0)&&(temp2==0)){
            cout<<0<<endl;
        }
        else if((temp1!=0)&&(temp2==0)){
            cout<<(temp1)<<endl;
        }
        else if((temp1==0)&&(temp2!=0)){
            cout<<temp2<<endl;
        }
        else if((temp1!=0)&&(temp2!=0)){
            cout<<(temp1+temp2+2)<<endl;
        }
    }
}


#include<iostream>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int x1,y1,x2,y2;
		cin>>x1>>y1>>x2>>y2;
		if(y1==y2)
		{
			cout<<x2-x1<<endl;
		}
		else if(x1==x2)
		{
			cout<<y2-y1<<endl;
		}
		else
		{
			long long int ans=(x2-x1)+(y2-y1)+2;
			cout<<ans<<endl;
		}
	}
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int x1,y1,x2,y2;
		cin>>x1>>y1>>x2>>y2;
		if(y1==y2)
		{
			cout<<x2-x1<<endl;
		}
		else if(x1==x2)
		{
			cout<<y2-y1<<endl;
		}
		else
		{
			long long int a1=abs(x2-x1);
			long long int a2=abs(y2-y1);
			long long int ans=(a1)+(a2)+2;
			cout<<ans<<endl;
		}
	}
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int x1,y1,x2,y2;
		cin>>x1>>y1>>x2>>y2;
		if(y1==y2)
		{
			cout<abs(x2-x1) <<endl;
		}
		else if(x1==x2)
		{
			cout<abs(y2-y1) <<endl;
		}
		else
		{
			long long int a1=abs(x2-x1);
			long long int a2=abs(y2-y1);
			long long int ans=(a1)+(a2)+2;
			cout<<ans<<endl;
		}
	}
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int x1,y1,x2,y2;
		cin>>x1>>y1>>x2>>y2;
		if(y1==y2)
		{
			cout<<abs(x2-x1)<<endl;
		}
		else if(x1==x2)
		{
			cout<<abs(y2-y1)<<endl;
		}
		else
		{
			long long int a1=abs(x2-x1);
			long long int a2=abs(y2-y1);
			long long int ans=(a1)+(a2)+2;
			cout<<ans<<endl;
		}
	}
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int x1,y1,x2,y2;
		cin>>x1>>y1>>x2>>y2;
		if(y1==y2)
		{
			cout<<x2-x1<<endl;
		}
		else if(x1==x2)
		{
			cout<<y2-y1<<endl;
		}
		else
		{
			int a1=abs(x2-x1);
			int a2=abs(y2-y1);
			long long int ans=(a1)+(a2)+2;
			cout<<ans<<endl;
		}
	}
	return 0;
}


#include <iostream>
#include <math.h>
double X1, X2, Y1, Y2;
//WHat Lol

int main(){
    int cases;
    std::cin >> cases;
    for(int i=0;i<cases;i++){
        std::cin >> X1 >> Y1 >> X2 >> Y2;
        if (X2-X1 >= 0){
            std::cout << (X2-X1) + 2 * (Y1!=Y2) + (Y2-Y1) << std::endl;
        }else{
            std::cout << (X2-X1) + 4 + (Y2-Y1) << std::endl;
        }
    }
}


#include <iostream>
#include <bits/stdc++.h>

double X1, X2, Y1, Y2;

int main(){
    int cases;
    std::cin >> cases;
    for(int i=0;i<cases;i++){
        std::cin >> X1 >> Y1 >> X2 >> Y2;
        if (X1==X2||Y1==Y2){
            std::cout << abs(X1-X2) + abs(Y1-Y2) << std::endl;
        }else{
            std::cout << abs(X1-X2) + 2 + abs(Y1-Y2) << std::endl;
        }
    }
}


#include <iostream>
#include <math.h>
int X1, X2, Y1, Y2;
//WHat Lol

int main(){
    int cases;
    std::cin >> cases;
    for(int i=0;i<cases;i++){
        std::cin >> X1 >> Y1 >> X2 >> Y2;
        if (X2-X1 >= 0){
            std::cout << (X2-X1) + 2 * (Y1!=Y2) + (Y2-Y1) << std::endl;
        }else{
            std::cout << (X2-X1) + 4 + (Y2-Y1) << std::endl;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t, x1, y1, x2, y2;
    cin >> t;
    for (int i = 1; i <= t; ++i){
            cin >> x1 >> y1 >> x2 >> y2;
            if (x1 == x2 || y1 == y2){
                        cout << abs(x1-x2) + abs(y1-y2) << '\n';
                    }
            else cout << abs(x1-x2) + abs(y1-y2) + 2 << '\n';
        }
}


#include <iostream>
#include <math.h>
int X1, X2, Y1, Y2;

int main(){
    int cases;
    std::cin >> cases;
    for(int i=0;i<cases;i++){
        std::cin >> X1 >> Y1 >> X2 >> Y2;
        if (X2-X1 >= 0){
            std::cout << (X2-X1) + 2 * (Y1!=Y2) + (Y2-Y1) << std::endl;
        }else{
            std::cout << (X2-X1) + 4 + (Y2-Y1) << std::endl;
        }
    }
}


#include <iostream>
#include <bits/stdc++.h>

int X1, X2, Y1, Y2;

int main(){
    int cases;
    std::cin >> cases;
    for(int i=0;i<cases;i++){
            std::cin >> X1 >> Y1 >> X2 >> Y2;
            if (X1==X2||Y1==Y2){
                std::cout << abs(X1-X2) + abs(Y1-Y2) << std::endl;
            }else{
                std::cout << abs(X1-X2) + 2 + abs(Y1-Y2) << std::endl;
            }
        }
}


#include<bits/stdc++.h>
#define optimize() ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int main()
{
  long long int t;
  cin>>t;
  while(t--)
  {
    long long int x,y,x1,y1,ans=0;
    cin>>x>>y>>x1>>y1;

    if(x==x1 && y1!=y)
    {
      ans=abs(y1-y);
    }
    else if(x!=x1&&y==y1)
    {
      ans=abs(x1-x);
    }
    else{
      ans=2+abs((x1-x)+abs(y1-y));

    }
    cout<<ans<<endl;
    ans=0;
    }

  return 0;

}


#include<bits/stdc++.h>
#define optimize() ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int main()
{
  long long int t;
  cin>>t;
  while(t--)
  {
    long long int x,y,x1,y1,ans=0;
    cin>>x>>y>>x1>>y1;

    if(x>0&&y>0&&x1>0&&y1>0){
    if(x==x1 && y1!=y)
    {
      ans=abs(y1-y);
    }
    else if(x!=x1&&y==y1)
    {
      ans=abs(x1-x);
    }
    else{
      ans=2+abs((x1-x)+abs(y1-y));

    }
    cout<<ans<<endl;
    ans=0;
    }
  }

  return 0;

}


#include<bits/stdc++.h>
#define optimize() ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int main()
{
  long long int t;
  cin>>t;
  while(t--)
  {
    long long int x,y,x1,y1,ans=0;
    cin>>x>>y>>x1>>y1;

    if(x==x1 && y1!=y)
    {
      ans=abs(y1-y);
    }
    else if(x!=x1&&y==y1)
    {
      ans=abs(x1-x);
    }
    else{
      ans=abs((x1-x)+abs(y1-y)+2);

    }
    cout<<ans<<endl;
    ans=0;
    }

  return 0;

}


#include<bits/stdc++.h>
#define optimize() ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int main()
{
  long long int t;
  cin>>t;
  while(t--)
  {
    long long int x,y,x1,y1,ans=0;
    cin>>x>>y>>x1>>y1;
    if(x==x1 && y1!=y)
    {
      ans=(y1-y);
    }
    else if(x!=x1&&y==y1)
    {
      ans=(x1-x);
    }
    else{
      ans=2*((x1-x)+(y1-y));

    }
    cout<<ans<<endl;
    ans=0;
  }

  return 0;

}


#include<bits/stdc++.h>
#define optimize() ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int main()
{
  long long int t;
  cin>>t;
  while(t--)
  {
    long long int x,y,x1,y1,ans=0;
    cin>>x>>y>>x1>>y1;
    if(x==x1 && y1!=y)
    {
      ans=abs(y1-y);
    }
    else if(x!=x1&&y==y1)
    {
      ans=abs(x1-x);
    }
    else{
      ans=2*(abs(x-x1)+abs(y-y1));

    }
    cout<<ans<<endl;
    ans=0;
  }

  return 0;

}


#include<bits/stdc++.h>
#define optimize() ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int main()
{
  long long int t;
  cin>>t;
  while(t--)
  {
    long long int x,y,x1,y1,ans=0;
    cin>>x>>y>>x1>>y1;

    if(x==x1 || y==y1)
    {
      cout<<abs(y1-y)+abs(x1-x)<<endl;
    }

    else{
      cout<<abs(x-x1)+abs(y-y1)+2<<endl;;

    }

    }

  return 0;

}


#include<bits/stdc++.h>
#define optimize() ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    int x,y,x1,y1,ans=0;
    cin>>x>>y>>x1>>y1;
    if(x==x1 && y1!=y)
    {
      ans=abs(y1-y);
    }
    else if(x!=x1&&y==y1)
    {
      ans=abs(x1-x);
    }
    else{
      ans=2*(abs(x-x1)+abs(y-y1));

    }
    cout<<ans<<endl;
  }

  return 0;

}


#include<bits/stdc++.h>
#define optimize() ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int main()
{
  long long int t;
  cin>>t;
  while(t--)
  {
    long long int x,y,x1,y1,ans=0;
    cin>>x>>y>>x1>>y1;

    if(x==x1 || y==y1)
    {
      ans=abs(y1-y)+abs(x1-x);
    }

    else{
      ans=abs((x1-x)+abs(y1-y)+2);

    }
    cout<<ans<<endl;
    ans=0;
    }

  return 0;

}


#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <map>
#include <vector>
#include <queue>

using namespace std;

void solution()
{
	int x1,y1,x2,y2;
	scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
	int t = 0;
	t += x2 - x1;
	if(t==0 )
		t+=y2-y1;
	else
		if(x1 != x2 && y1!=y2)
		{
			t+=2;
			t+=y2-y1;
		}
	printf("%d\n",t);
}

int main()
{
	int T;
	scanf("%d",&T);
	for(int z = 0; z < T; ++z)
	{
		solution();
	}
}


#include <bits/stdc++.h>

using namespace std;

void solution()
{
	int x1,y1,x2,y2;
	scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
	int t = 0;
	t += x2 - x1;
	if(t==0 )
		t+=y2-y1;
	else
		if(x1 != x2 && y1!=y2)
		{
			t+=2;
			t+=y2-y1;
		}
	printf("%d\n",t);
}

int main()
{
	int T;
	scanf("%d",&T);
	for(int z = 0; z < T; ++z)
	{
		solution();
	}
}


#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <map>
#include <vector>
#include <queue>

using namespace std;

void solution()
{
	int x1,y1,x2,y2;
	scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
	long long t = 0;
	t += abs(x2 - x1);
	if(t==0 )
		t+=abs(y2-y1);
	else
		if(x1 != x2 && y1!=y2)
		{
			t+=2;
			t+=abs(y2-y1);
		}
	printf("%d\n",t);
}

int main()
{
	int T;
	scanf("%d",&T);
	for(int z = 0; z < T; ++z)
	{
		solution();
	}
}


#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <map>
#include <vector>
#include <queue>

using namespace std;

void solution()
{
	long long x1,y1,x2,y2;
	scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
	long long t = 0;
	t += x2 - x1;
	if(t==0 )
		t+=y2-y1;
	else
		if(x1 != x2 && y1!=y2)
		{
			t+=2;
			t+=y2-y1;
		}
	printf("%d\n",t);
}

int main()
{
	long long T;
	scanf("%d",&T);
	for(int z = 0; z < T; ++z)
	{
		solution();
	}
}


#include<iostream>
using namespace std;
int main(){
	int t; cin >> t;
	int *ans = new int[t];
	int x1, x2, y1, y2;
	for (int i = 0; i < t; i++){
		cin >> x1 >> y1 >> x2 >> y2;
		int dx = abs(x1 - x2); int dy =abs(y1 - y2);
		if (x1 == x2 || y1 == y2)
			ans[i] = dx + dy;

		if (x1 != x2 && y1 != y2)
			ans[i] = dx + dy + 2;
	}
	for (int i = 0; i < t; i++)
		cout << ans[i] << '\n';
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
		int x1,x2,y1,y2;
		cin>>x1>>y1>>x2>>y2;
		int ans;
		ans=abs(x1-x2)+abs(y1-y2);
		if(x1==x2||y1==y2)cout<<ans<<endl;
		else {
			ans+=2;
			cout<<ans<<endl;
		}

	}
}


//#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
#include<numeric>
#include<deque>
#include<map>
#include<set>
#include<queue>
#include<optional>
#include<assert.h>
#include<tuple>
#include<list>
#include<deque>

using namespace std;

void solve() {
	int x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	if (x1 == x2 || y1 == y2) {
		cout << abs(x1 - x2) + abs(y1 - y2) << "\n";
		return;
	}
	cout << abs(x1 - x2) + abs(y1 - y2) + 2 << "\n";
	return;

}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	cin >> t;
	while (t--)solve();
	return 0;
}


//#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#pragma GCC optimize("Ofast,inline,unroll-loops,no-stack-protector")
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define all(v) v.begin(), v.end()
//#define sz(v) ((int)(v).size())
//#define trav(a, x) for (auto& a: x)
typedef long long ll;
typedef double db;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef string str;
typedef vector<int> vi;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
//const int di[4] = { 1, 0, -1, 0 };
//const int dj[4] = { 0, 1 ,0, -1 };
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--) {
		ll x1, y1, x2, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		if (x1 == x2) {
			cout << abs(y1 - y2) << endl;
		}
		else if (y1 == y2) {
			cout << abs(x1 - x2) << endl;
		}
		else {
			cout << abs(x1 - x2) + abs(y1 - y2) + 2 << endl;
		}
	}
}


#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using std::cout;
using std::cin;
using std::size_t;
using std::string;
using std::vector;
using std::pair;

// Problem A
class rabbit_move_box
{
    struct coordinate
    {
        int x;
        int y;
    };

    coordinate start_;
    coordinate end_;
public:
    constexpr rabbit_move_box(const coordinate start, const coordinate end) noexcept : start_(start), end_(end) {}

    void solve() const noexcept
    {
        const auto dis_x = std::abs(end_.x - start_.x);
        const auto dis_y = std::abs(end_.y - start_.y);

        if(dis_x == 0) cout << dis_y;
        else if(dis_y == 0) cout << dis_x;
        else cout << dis_x + dis_y + 2;
    }

    static void create_from_input()
    {
        size_t size;
        cin >> size;

        for(decltype(size) i = 0; i < size; ++i)
        {
            coordinate start{};
            coordinate end{};

            cin >> start.x;
            cin >> start.y;
            cin >> end.x;
            cin >> end.y;

            rabbit_move_box{start, end}.solve();
        }
    }
};

int main()
{
    rabbit_move_box::create_from_input();
    return 0;
}


#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using std::cout;
using std::cin;
using std::size_t;
using std::string;
using std::vector;
using std::pair;

// Problem A
class rabbit_move_box
{
    struct coordinate
    {
        int x;
        int y;
    };

    coordinate start_;
    coordinate end_;
public:
    constexpr rabbit_move_box(const coordinate start, const coordinate end) noexcept : start_(start), end_(end) {}

    void solve() const noexcept
    {
        const auto dis_x = std::abs(end_.x - start_.x);
        const auto dis_y = std::abs(end_.y - start_.y);

        if(dis_x == 0) cout << dis_y;
        else if(dis_y == 0) cout << dis_x;
        else cout << dis_x + dis_y + 2;
        cout << '\n';
    }

    static void create_from_input()
    {
        size_t size;
        cin >> size;

        for(decltype(size) i = 0; i < size; ++i)
        {
            coordinate start{};
            coordinate end{};

            cin >> start.x;
            cin >> start.y;
            cin >> end.x;
            cin >> end.y;

            rabbit_move_box{start, end}.solve();
        }
    }
};

int main()
{
    rabbit_move_box::create_from_input();
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
	long long int t,i,x1,y1,x2,y2;
	scanf("%lld",&t);
	for(i=1;i<=t;i++)
	{
		scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
		if(abs(x1-x2)==0 || abs(y2-y1)==0)
			printf("%lld\n",abs(x1-x2)+abs(y1-y2));
		else
		   printf("%lld\n",abs(x1-x2)+abs(y1-y2)+2);
	}
	return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
	long long int t,i,x1,y1,x2,y2;
	scanf("%lld",&t);
	for(i=1;i<=t;i++)
	{
		scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
		if(y1==y2)
			printf("%lld\n",abs(x1-x2)+abs(y1-y2));
		else
		   printf("%lld\n",abs(x1-x2)+abs(y1-y2)+2);
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int x1, y1, x2, y2, x, y;
		cin >> x1 >> y1 >> x2 >> y2;
		x = abs(x1 - x2), y = abs(y1 - y2);
		if (!x || !y) cout << max(x, y) << "\n";
		else cout << x + y + 2 << "\n";
	}
	return 0;
}


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="1.cpp" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/1.cpp" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/1.cpp" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="main.cpp" />
		<Extensions>
			<lib_finder disable_auto="1" />
		</Extensions>
	</Project>
</CodeBlocks_project_file>


#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    int cases,x1,y1,x2,y2;
    cin >> cases;
    for(int i=0;i<cases;i++){
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        if(x1 ==  x2 || y1 == y2){
            printf("%d\n",abs(x2-x1)+abs(y2-y1));
        }
        else{
            printf("%d\n",abs(x2-x1)+abs(y2-y1)+2);
        }
    }
    return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int t;
	cin >> t;
	int *x = new int [2 * t];
	int *y = new int [2 * t];
	for (int i = 0; i < 2*t; i++)
	{
		cin >> *(x + i) >> *(y + i);
	}
	for (int i = 0; i < t; i++)
	{
		int s=0;
		if (x[2 * i] - x[2 * i + 1] != 0&& y[2 * i] - y[2 * i + 1] != 0)s=s+2;
		s = s + abs(x[2 * i] - x[2 * i + 1]) + abs(y[2 * i] - y[2 * i + 1]);
		cout << s << endl;
	}
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;

int main()
{
   int a[1000];
   int b[1000];
   int c[1000];
   int d[1000];
    int i,j,t,m,n;
    cin >>n;
    j=0;
    t=0;
    for(i=0;i<n;i++)
    {
    cin>>a[i]>>b[i]>>c[i]>>d[i];
    }

    //读入

    for(i=0;i<n;i++)
    {
    t=abs(a[i]-c[i]);
    if(b[i]==d[i])
    cout << t << endl;
    else if(a[i]!=c[i])
    {
    t=t+2+abs(b[i]-d[i]);
    cout <<t<<endl;
    }
    else
    cout << abs(b[i]-d[i]) << endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include<cctype>
#include<deque>
#include<stack>
#include<queue>
#include <iomanip>

#define ll   long long
using namespace std;

int main()
{
    int t;
    cin >> t;
    ll x1, y1, x2, y2;
    while (t--) {
        cin >> x1 >> y1 >> x2 >> y2;
        if (x1 == x2 || y1 == y2) {
            cout << abs(x1 - x2) + abs(y1 - y2) << endl;
        }
        else {
            cout << abs(x1 - x2) + abs(y1 - y2) + 2 << endl;
        }
    }

    return 0;
}


#include <algorithm>
#include <iomanip>
#include <istream>
#include <map>
#include <numeric>
#include <ostream>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// Powered by caide (code generator, tester, and library code inliner)

class Input
{
public:
    int x1;
    int y1;
    int x2;
    int y2;
};

Input read_input (std::istream& in)
{
  Input res;
  in >> res.x1 >> res.y1 >> res.x2 >> res.y2;
  return res;
}

int solve (const Input &in)
{
    auto cost = std::abs (in.x1 - in.x2) + std::abs (in.y1 - in.y2);
    if (in.x1 != in.x2 && in.y1 != in.y2)
      cost += 2;
    return cost;
}

class Solution {
public:
    void solve(std::istream& in, std::ostream& out) {
        int t;
        in >> t;
        for (int i = 0; i < t; ++i)
            out << ::solve (read_input(in)) << '\n';
    }
};

void solve(std::istream& in, std::ostream& out)
{
    out << std::setprecision(12);
    Solution solution;
    solution.solve(in, out);
}

#include <fstream>
#include <iostream>

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    istream& in = cin;

    ostream& out = cout;

    solve(in, out);
    return 0;
}


#include<iostream>

using namespace std;
int main()
{
	int n;
	cin >> n;
	int x1, x2, y1, y2;
	int temp1,temp2;
	for (int i = 0; i < n; i++)
	{
		cin >> x1 >> y1 >> x2 >> y2;
		temp1 = x1 - x2;
		temp2 = y1 - y2;
		if (temp1 == 0 || temp2 == 0)
			cout << temp1 + temp2 << endl;
		else
		    cout << (abs(temp1) + abs(temp2) + 2)<<endl;
	}
	return 0;
}


#include<iostream>

using namespace std;
int main()
{
	int n;
	cin >> n;
	int x1, x2, y1, y2;
	int temp1,temp2;
	for (int i = 0; i < n; i++)
	{
		cin >> x1 >> y1 >> x2 >> y2;
		temp1 = abs(x1 - x2);
		temp2 = abs(y1 - y2);
		if (temp1 == 0 || temp2 == 0)
			cout << temp1 + temp2 << endl;
		else
		    cout << temp1 + temp2 + 2<<endl;
	}
	return 0;
}


#include<iostream>

using namespace std;
int main()
{
	int n;
	cin >> n;
	int x1, x2, y1, y2;
	int temp1,temp2;
	for (int i = 0; i < n; i++)
	{
		cin >> x1 >> y1 >> x2 >> y2;
		temp1 = x1 - x2;
		temp2 = y1 - y2;
		cout << (abs(temp1) + abs(temp2) + 2)<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int x1, y1, x2, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		if (x1 == x2 || y1 == y2) {
			cout << abs(x1 - x2) + abs(y1 - y2) << "\n";
		}
		else {
			cout << abs(x1 - x2) + abs(y1 - y2) + 2 << "\n";
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
struct Point
{
	int x, y;
};

int main()
{
	Point A, B;
	int t, p;
	cin >> t;
	vector<int> time(t, 0);

	for (int i = 0; i < t; i++)
	{
		cin >> A.x >> A.y >> B.x >> B.y;
		p = 2;
		if (A.x == B.x  || A.y == B.y)
			p = 0;
		time[i] = abs(B.y - A.y) + abs(B.x - A.x) + p;
	}
	for (int i = 0; i < t; i++)
		cout << time[i] << endl;
    return 0;
}


#include<iostream>
#include<set>

using namespace std;
int main() {
	int t;
	cin >> t;
	for (int yyyy = 1; yyyy <= t; ++yyyy) {
		int x1, x2, y1, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		if (x1 == x2 && y1 == y2) {
			cout << 0;
		}
		else {
			if (x1 == x2) {
				cout << abs(y2 - y1);
			}
			else {
				if (y1 == y2) {
					cout << abs(x2 - x1);
				}
				else {
					cout << abs(y2 - y1) + abs(y2 - y1) + 2;
				}
			}
		}
	}
	//system("pause");
	return 0;
}


#include<iostream>
#include<set>

using namespace std;
int main() {
	int t;
	cin >> t;
	for (int yyyy = 1; yyyy <= t; ++yyyy) {
		int x1, x2, y1, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		if (x1 == x2 && y1 == y2) {
			cout << 0 << endl;
		}
		else {
			if (x1 == x2) {
				cout << abs(y2 - y1) << endl;
			}
			else {
				if (y1 == y2) {
					cout << abs(x2 - x1) << endl;
				}
				else {
					cout << abs(y2 - y1) + abs(x2 - x1) + 2 << endl;
				}
			}
		}
	}
	//system("pause");
	return 0;
}


#include<iostream>
#include<set>

using namespace std;
int main() {
	int t;
	cin >> t;
	for (int yyyy = 1; yyyy <= t; ++yyyy) {
		int x1, x2, y1, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		if (x1 == x2 && y1 == y2) {
			cout << 0 << endl;
		}
		else {
			if (x1 == x2) {
				cout << abs(y2 - y1) << endl;
			}
			else {
				if (y1 == y2) {
					cout << abs(x2 - x1) << endl;
				}
				else {
					cout << abs(y2 - y1) + abs(y2 - y1) + 2 << endl;
				}
			}
		}
	}
	system("pause");
	return 0;
}


#include<iostream>
#include<set>

using namespace std;
int main() {
	int t;
	cin >> t;
	for (int yyyy = 1; yyyy <= t; ++yyyy) {
		int x1, x2, y1, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		if (x1 == x2 && y1 == y2) {
			cout << 0 << endl;
		}
		else {
			if (x1 == x2) {
				cout << abs(y2 - y1) << endl;
			}
			else {
				if (y1 == y2) {
					cout << abs(x2 - x1) << endl;
				}
				else {
					cout << abs(y2 - y1) + abs(y2 - y1) + 2 << endl;
				}
			}
		}
	}
	//system("pause");
	return 0;
}


#include<iostream>
#include<set>

using namespace std;
int main() {
	int t;
	cin >> t;
	for (int yyyy = 1; yyyy <= t; ++yyyy) {
		int x1, x2, y1, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		if (x1 == x2 && y1 == y2) {
			cout << 0;
		}
		else {
			if (x1 == x2) {
				cout << abs(y2 - y1);
			}
			else {
				if (y1 == y2) {
					cout << abs(x2 - x1);
				}
				else {
					cout << abs(y2 - y1) + abs(y2 - y1) + 2;
				}
			}
		}
	}
	system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int m,n,p,q;
        cin>>m>>n>>p>>q;
        if(m==p)
        {
            if(n==q)
            {
                cout<<0<<endl;
            }
            else
            {
                cout<<abs(n-q)<<endl;
            }
        }
        else
        {
            if(n==q)
            {
                cout<<abs(m-p)<<endl;
            }
            else
            {
                cout<<abs(m-p)+abs(n-q)+2<<endl;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int m,n,q,p;
        cin>>m>>n>>q>>p;
        if(m==p)
        {
            if(n==q)
            {
                cout<<0<<endl;
            }
            else
            {
                cout<<abs(n-q)<<endl;
            }
        }
        else
        {
            if(n==q)
            {
                cout<<abs(m-p)<<endl;
            }
            else
            {
                cout<<abs(m-p)+abs(n-q)+2<<endl;
            }
        }
    }
    return 0;
}


#include <iostream>

using namespace std;

int main() {
	long long t, x1, x2, y1, y2;
	cin >> t;
	for (int i = 0; i < t; ++i) {
		cin >> x1 >> y1 >> x2 >> y2;
		if (x1 == x2) {
			cout << abs(y2 - y1) << endl;
			continue;
		}
		if (y1 == y2) {
			cout << abs(x2 - x1) << endl;
			continue;
		}
		cout << abs(y2 - y1) + abs(x2 - x1) + 2 << endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <fstream>
#include <cmath>
#include <vector>

using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int x1, y1, x2, y2;
        cin >> x1 >>y1 >> x2 >> y2;
        int time = abs(x2 - x1) + abs(y2 - y1) + 2;
        cout << time << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <fstream>
#include <cmath>
#include <vector>

using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int x1, y1, x2, y2;
        cin >> x1 >>y1 >> x2 >> y2;
        int time;
        if(x1==x2 || y1==y2) time = abs(x2 - x1) + abs(y2 - y1);
        else  time = abs(x2 - x1) + abs(y2 - y1)+2;
        cout << time << endl;
    }
    return 0;
}


#include <iostream>
#include<vector>
#include<algorithm>
#include<climits>
#include<cmath>
//#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b,c,d;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a>>b>>c>>d;
        if(a==c)
        {
            cout<<abs(d-b)<<endl;
            continue;
        }
        if(b==d)
        {
            cout<<abs(a-c)<<endl;
            continue;
        }
        if(a!=c && b!=d)
        {
            cout<<2*(abs(a-c)+abs(b-d))<<endl;
            continue;
        }
        //cout<<endl;
    }
}


#include <iostream>
#include<vector>
#include<algorithm>
#include<climits>
#include<cmath>
//#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b,c,d;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a>>b>>c>>d;
        if(a==c)
        {
            cout<<abs(d-b)<<endl;
            continue;
        }
        if(b==d)
        {
            cout<<abs(a-c)<<endl;
            continue;
        }
        if(a!=c && b!=d)
        {
            cout<<(abs(a-c)+abs(b-d))+2<<endl;
            continue;
        }
        //cout<<endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int main()
{
    int t;
    cin>>t;
    for (int i = 1;i <= t; ++i)
    {
        int x1, y1, x2, y2;
        cin>>x1>>y1>>x2>>y2;
        int ans = abs(x1 - x2) + abs(y1 - y2) + 2;
        if (x1 ==x2 || y1 == y2) ans -= 2;
        cout<<ans<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin>>t;
    for (int i = 1;i <= t; ++i)
    {
        int x1, y1, x2, y2;
        cin>>x1>>y1>>x2>>y2;
        int ans = abs(x1 - x2) + abs(y1 - y2) + 2;
        if (x1 ==x2 || y1 == y2) ans -= 2;
        cout<<ans<<"\n";
    }
    return 0;
}


/*
URL_HERE
*/
/*

*/
#ifdef _WIN32
#pragma warning(disable:4996)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <queue>
#include <iostream>
using namespace std;

FILE* _fin = stdin;
FILE* _fout = stdout;
#define PI 3.141592653589793238462643383279502884197169399375105820974
#define ten5p1 100001
#define ten6p1 1000001
#define ten8p1 100000001
#define ten9p1 1000000001
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define rep(var,n) for(int var=0;var<n;++var)
#define repi(n) rep(i,n)
#define repj(n) rep(j,n)
#define repi1(n) for(int i=1;i<n;++i)
#define repj1(n) for(int j=1;j<n;++j)
int _min(int a, int b) { return a <= b ? a : b; }
ll _min(ll a, ll b) { return a <= b ? a : b; }
int _max(int a, int b) { return a >= b ? a : b; }
ll _max(ll a, ll b) { return a >= b ? a : b; }
void zero(int* data, int n) { memset(data, 0, sizeof(int) * n); }
void zero(ll* data, int n) { memset(data, 0, sizeof(ll) * n); }
void zero(unsigned long long* data, int n) { memset(data, 0, sizeof(unsigned long long) * n); }
void zero(char* data, int n) { memset(data, 0, sizeof(char) * n); }
char readc() { char var; fscanf(_fin, "%c", &var); return var; }
int readi() { int var; fscanf(_fin, "%d", &var); return var; }
double readlf() { double var; fscanf(_fin, "%lf", &var); return var; }
ll readll() { ll var; fscanf(_fin, "%lld", &var); return var; }
void repread(int* data, int n) { repi(n) data[i] = readi(); }
void repread(ll* data, int n) { repi(n) data[i] = readll(); }
int reads(char* str, int maxsize)
{
	for (;;) { if (fgets(str, maxsize, _fin) == NULL) break; if (str[0] != '\n' && str[0] != '\r') break; }
	int slen = strlen(str); if (slen == 0) return 0;
	if (str[slen - 1] == '\n' || str[slen - 1] == '\r') str[--slen] = 0;
	return slen;
}

#define writec(var) fprintf(_fout,"%c",var)
#define writecsp(var) fprintf(_fout,"%c ",var)
#define writecln(var) fprintf(_fout,"%c\n",var)
#define writei(var) fprintf(_fout,"%d",var)
#define writeisp(var) fprintf(_fout,"%d ",var)
#define writellsp(var) fprintf(_fout,"%lld ",var)
#define writeiln(var) fprintf(_fout,"%d\n",var)
#define writellln(var) fprintf(_fout,"%lld\n",var)
#define writeulln(var) fprintf(_fout,"%llu\n",var)
#define writefln(var) fprintf(_fout,"%f\n",var)
#define writes(str) fprintf(_fout,"%s",str)
#define writesp() fprintf(_fout," ")
#define writeln() fprintf(_fout,"\n")
#define RUN_LOCAL(testfilename) {_fin = fopen(testfilename, "r"); if(_fin==NULL) _fin=stdin;}
#define swap(type,a, b) {type t=a;a=b;b=t;}
#define sort(data,n) std::sort(data,data+n)
//#define mod(a,b) b==0 ? 0 : a==0 ? 0 :(a>0 ? a%b : b + a % b) //what the hell is this...
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return (a / g) * b; }
void reverse(char* data, int n) { int k = n >> 1; repi(k) { char tmp = data[i]; data[i] = data[n - i - 1]; data[n - i - 1] = tmp; } }
void reverse(int* data, int n) { int k = n >> 1; repi(k) { int tmp = data[i]; data[i] = data[n - i - 1]; data[n - i - 1] = tmp; } }
void reverse(ll* data, int n) { int k = n >> 1; repi(k) { ll tmp = data[i]; data[i] = data[n - i - 1]; data[n - i - 1] = tmp; } }

#define _Vec(type, name) \
struct name { type* data; int size; int n; };\
void init(name* t, int size) { t->data = (type*)malloc(sizeof(type) * size); t->size = size; t->n = 0; }\
void resize(name* t) { int ns = t->size * 1.2f; t->data = (type*)realloc(t->data, sizeof(type) * ns); t->size = ns; }\
void add(name* t, type val) { if (t->n >= t->size) resize(t); int k = t->n; t->data[k] = val; t->n = k + 1; }\
void free(name* t) { free(t->data);}
_Vec(int, Veci)
_Vec(long long, Vecll)
_Vec(char*, Vecs)

#define _ispal(type, name) int name(type* a, type* b, int n){repi(n){if (a[i] != b[n - i - 1]){return 0;}}return 1;}
_ispal(int, ispali)
_ispal(char, ispalc)

#define _Pair(type, name) struct name{type x, y;}; \
int cmp_##name(const void* _a, const void* _b)\
{\
	name* a = (name*)_a;name* b = (name*)_b;\
	if (a->x < b->x) return -1;\
	else\
	{\
		if (a->x == b->x)\
		{\
			if (a->y == b->y) return 0;\
			if (a->y < b->y) return -1;\
			else return 1;\
		}\
		else\
		{\
			return 1;\
		}\
	}\
}\
void sort_##name(name* d, int n){qsort(d, n, sizeof(name), cmp_##name);}
	_Pair(int, Pairi)
	_Pair(double,Pairlf)

	int cmp_Str(const void* _a, const void* _b) { char* a = *((char**)_a); char* b = *((char**)_b); return strcmp(a, b); }
void sort_Str(char** str, int n) { qsort(str, n, sizeof(char*), cmp_Str); }

ll expmod(ll x, ll n, ll m) {
	ll ans = 1;
	for (; n;) {
		if (n & 1) ans = (ans * x) % m;
		x = (x * x) % m;
		n >>= 1;
	}
	return ans;
}

ll combmod(ll n, ll k, ll m) {
	ll ret = 1;
	ll div = 1;
	for (ll i = 0; i < k; ++i) {
		ret = (ret * (n - i) % m) % m;
		div = (div * (i + 1)) % m;
	}
	div = expmod(div, m - 2, m) % m;
	return (ret * div) % m;
}

int next_perm(int* data, int n)
{
	//find first i,i+1 where d[i]<d[i+1]
	int rootidx = -1;
	for (int i = n - 2; i >= 0; --i)
	{
		if (data[i] < data[i + 1])
		{
			rootidx = i;
			break;
		}
	}

	if (rootidx == -1) return 0;

	int ceilingidx = rootidx + 1;
	//find smallest d[x] after d[rooti] where d[x]>d[rooti]
	for (int i = rootidx + 2; i < n; ++i)
	{
		if (data[i] > data[rootidx] && data[i] < data[ceilingidx])
		{
			ceilingidx = i;
		}
	}

	swap(int, data[rootidx], data[ceilingidx]);
	sort(&data[rootidx + 1], n - rootidx - 1);
	return 1;
}
int count_digits(ll a)
{
	int k = 0;
	for (;;)
	{
		if (a == 0)break;
		k++;
		a /= 10;
	}
	return k;
}

/*
use only when val EXISTS in data
*/
int bs_exists(int* data, int n, int val)
{
	int left = 0;
	int right = n - 1;
	for (; left < right;)
	{
		int mid = (left + right + 1) >> 1;
		if (data[mid] <= val) left = mid;
		else right = mid - 1;
	}
	return right;
}

ll simple_factorial(int x)
{
	ll f = 1.0;
	repi(x)
	{
		f *= (i + 1);
	}
	return f;
}

unsigned long long djb2(char* data, int n)
{
	unsigned long long h = 5381;
	for (int i = 0; i < n; ++i) h = (h << 5) + h + data[i];
	return h;
}

struct Hashcount
{
	int* count;
	unsigned long long* keys;
	int size_bits;
};
void init(Hashcount* h, int size_bits)
{
	h->size_bits = size_bits;
	int size = 1 << h->size_bits;
	h->count = (int*)malloc(sizeof(int) * size);
	zero(h->count, size);
	h->keys = (unsigned long long*)malloc(sizeof(unsigned long long) * size);
	zero(h->keys, size);
}

int set(Hashcount* h, unsigned long long key)
{
	//knuth's golden ratio multiplicative hashing for 64-bit keys
	int index = (key * 2654435761) >> (64 - h->size_bits);
	int size = 1 << h->size_bits;
	for (int i = 0; i < size; i++)
	{
		if (h->keys[index] == key) //if key exists, overwrite data
		{
			h->count[index]++;
			return index;
		}

		if (h->keys[index] == 0) //empty spot
		{
			h->count[index] = 1;
			h->keys[index] = key;
			return index;
		}

		index++;
		index &= size - 1;
	}

	return -1;//return invalid index if data was not placed. this could be used as an indicator to resize.
}

int get(Hashcount* h, unsigned long long key)
{
	int index = (key * 2654435761) >> (64 - h->size_bits);
	int size = 1 << h->size_bits;
	for (int i = 0; i < size; i++)
	{
		if (h->keys[index] == key)//found original key
		{
			return h->count[index];
		}

		if (h->keys[index] == 0) return 0;//if you see a zero,key was never there

		index++;
		index &= size - 1;
	}
	return 0;
}
//not sure if this works
int sub(Hashcount* h, unsigned long long key)
{
	int index = (key * 2654435761) >> (64 - h->size_bits);
	int size = 1 << h->size_bits;
	for (int i = 0; i < size; i++)
	{
		if (h->keys[index] == key)//found original key
		{
			--h->count[index];
			if (h->count[index] < 0) h->count[index] = 0;
			return h->count[index];
		}

		if (h->keys[index] == 0) return 0;//if you see a zero,key was never there

		index++;
		index &= size - 1;
	}
	return 0;
}

const ll MOD  = 1e9 + 7;
/*
//proper binary search???
//tested on 1 problem it works!!!!!!!
int left = 0;
int right = n-1;
for (;;)
{
	if (left >= right)break;
	int mid = (left + right) >> 1;
	if (x <= a[mid]) right = mid;
	else left = mid+1;
}

printf("%d %d\n",right, a[right]);
*/

//------------------------------------------

//------------

int main()
{

	RUN_LOCAL("input.txt");
	int t = readi();
	rep(_t, t)
	{
		int x[4];
		repread(x, 4);
		int ans = abs(x[0] - x[2]) + abs(x[1] - x[3]);
		if (x[1] - x[3] != 0) ans += 2;
		printf("%d\n", ans);
	}
	return 0;
}


/*
URL_HERE
*/
/*

*/
#ifdef _WIN32
#pragma warning(disable:4996)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <queue>
#include <iostream>
using namespace std;

FILE* _fin = stdin;
FILE* _fout = stdout;
#define PI 3.141592653589793238462643383279502884197169399375105820974
#define ten5p1 100001
#define ten6p1 1000001
#define ten8p1 100000001
#define ten9p1 1000000001
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define rep(var,n) for(int var=0;var<n;++var)
#define repi(n) rep(i,n)
#define repj(n) rep(j,n)
#define repi1(n) for(int i=1;i<n;++i)
#define repj1(n) for(int j=1;j<n;++j)
int _min(int a, int b) { return a <= b ? a : b; }
ll _min(ll a, ll b) { return a <= b ? a : b; }
int _max(int a, int b) { return a >= b ? a : b; }
ll _max(ll a, ll b) { return a >= b ? a : b; }
void zero(int* data, int n) { memset(data, 0, sizeof(int) * n); }
void zero(ll* data, int n) { memset(data, 0, sizeof(ll) * n); }
void zero(unsigned long long* data, int n) { memset(data, 0, sizeof(unsigned long long) * n); }
void zero(char* data, int n) { memset(data, 0, sizeof(char) * n); }
char readc() { char var; fscanf(_fin, "%c", &var); return var; }
int readi() { int var; fscanf(_fin, "%d", &var); return var; }
double readlf() { double var; fscanf(_fin, "%lf", &var); return var; }
ll readll() { ll var; fscanf(_fin, "%lld", &var); return var; }
void repread(int* data, int n) { repi(n) data[i] = readi(); }
void repread(ll* data, int n) { repi(n) data[i] = readll(); }
int reads(char* str, int maxsize)
{
	for (;;) { if (fgets(str, maxsize, _fin) == NULL) break; if (str[0] != '\n' && str[0] != '\r') break; }
	int slen = strlen(str); if (slen == 0) return 0;
	if (str[slen - 1] == '\n' || str[slen - 1] == '\r') str[--slen] = 0;
	return slen;
}

#define writec(var) fprintf(_fout,"%c",var)
#define writecsp(var) fprintf(_fout,"%c ",var)
#define writecln(var) fprintf(_fout,"%c\n",var)
#define writei(var) fprintf(_fout,"%d",var)
#define writeisp(var) fprintf(_fout,"%d ",var)
#define writellsp(var) fprintf(_fout,"%lld ",var)
#define writeiln(var) fprintf(_fout,"%d\n",var)
#define writellln(var) fprintf(_fout,"%lld\n",var)
#define writeulln(var) fprintf(_fout,"%llu\n",var)
#define writefln(var) fprintf(_fout,"%f\n",var)
#define writes(str) fprintf(_fout,"%s",str)
#define writesp() fprintf(_fout," ")
#define writeln() fprintf(_fout,"\n")
#define RUN_LOCAL(testfilename) {_fin = fopen(testfilename, "r"); if(_fin==NULL) _fin=stdin;}
#define swap(type,a, b) {type t=a;a=b;b=t;}
#define sort(data,n) std::sort(data,data+n)
//#define mod(a,b) b==0 ? 0 : a==0 ? 0 :(a>0 ? a%b : b + a % b) //what the hell is this...
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return (a / g) * b; }
void reverse(char* data, int n) { int k = n >> 1; repi(k) { char tmp = data[i]; data[i] = data[n - i - 1]; data[n - i - 1] = tmp; } }
void reverse(int* data, int n) { int k = n >> 1; repi(k) { int tmp = data[i]; data[i] = data[n - i - 1]; data[n - i - 1] = tmp; } }
void reverse(ll* data, int n) { int k = n >> 1; repi(k) { ll tmp = data[i]; data[i] = data[n - i - 1]; data[n - i - 1] = tmp; } }

#define _Vec(type, name) \
struct name { type* data; int size; int n; };\
void init(name* t, int size) { t->data = (type*)malloc(sizeof(type) * size); t->size = size; t->n = 0; }\
void resize(name* t) { int ns = t->size * 1.2f; t->data = (type*)realloc(t->data, sizeof(type) * ns); t->size = ns; }\
void add(name* t, type val) { if (t->n >= t->size) resize(t); int k = t->n; t->data[k] = val; t->n = k + 1; }\
void free(name* t) { free(t->data);}
_Vec(int, Veci)
_Vec(long long, Vecll)
_Vec(char*, Vecs)

#define _ispal(type, name) int name(type* a, type* b, int n){repi(n){if (a[i] != b[n - i - 1]){return 0;}}return 1;}
_ispal(int, ispali)
_ispal(char, ispalc)

#define _Pair(type, name) struct name{type x, y;}; \
int cmp_##name(const void* _a, const void* _b)\
{\
	name* a = (name*)_a;name* b = (name*)_b;\
	if (a->x < b->x) return -1;\
	else\
	{\
		if (a->x == b->x)\
		{\
			if (a->y == b->y) return 0;\
			if (a->y < b->y) return -1;\
			else return 1;\
		}\
		else\
		{\
			return 1;\
		}\
	}\
}\
void sort_##name(name* d, int n){qsort(d, n, sizeof(name), cmp_##name);}
	_Pair(int, Pairi)
	_Pair(double,Pairlf)

	int cmp_Str(const void* _a, const void* _b) { char* a = *((char**)_a); char* b = *((char**)_b); return strcmp(a, b); }
void sort_Str(char** str, int n) { qsort(str, n, sizeof(char*), cmp_Str); }

ll expmod(ll x, ll n, ll m) {
	ll ans = 1;
	for (; n;) {
		if (n & 1) ans = (ans * x) % m;
		x = (x * x) % m;
		n >>= 1;
	}
	return ans;
}

ll combmod(ll n, ll k, ll m) {
	ll ret = 1;
	ll div = 1;
	for (ll i = 0; i < k; ++i) {
		ret = (ret * (n - i) % m) % m;
		div = (div * (i + 1)) % m;
	}
	div = expmod(div, m - 2, m) % m;
	return (ret * div) % m;
}

int next_perm(int* data, int n)
{
	//find first i,i+1 where d[i]<d[i+1]
	int rootidx = -1;
	for (int i = n - 2; i >= 0; --i)
	{
		if (data[i] < data[i + 1])
		{
			rootidx = i;
			break;
		}
	}

	if (rootidx == -1) return 0;

	int ceilingidx = rootidx + 1;
	//find smallest d[x] after d[rooti] where d[x]>d[rooti]
	for (int i = rootidx + 2; i < n; ++i)
	{
		if (data[i] > data[rootidx] && data[i] < data[ceilingidx])
		{
			ceilingidx = i;
		}
	}

	swap(int, data[rootidx], data[ceilingidx]);
	sort(&data[rootidx + 1], n - rootidx - 1);
	return 1;
}
int count_digits(ll a)
{
	int k = 0;
	for (;;)
	{
		if (a == 0)break;
		k++;
		a /= 10;
	}
	return k;
}

/*
use only when val EXISTS in data
*/
int bs_exists(int* data, int n, int val)
{
	int left = 0;
	int right = n - 1;
	for (; left < right;)
	{
		int mid = (left + right + 1) >> 1;
		if (data[mid] <= val) left = mid;
		else right = mid - 1;
	}
	return right;
}

ll simple_factorial(int x)
{
	ll f = 1.0;
	repi(x)
	{
		f *= (i + 1);
	}
	return f;
}

unsigned long long djb2(char* data, int n)
{
	unsigned long long h = 5381;
	for (int i = 0; i < n; ++i) h = (h << 5) + h + data[i];
	return h;
}

struct Hashcount
{
	int* count;
	unsigned long long* keys;
	int size_bits;
};
void init(Hashcount* h, int size_bits)
{
	h->size_bits = size_bits;
	int size = 1 << h->size_bits;
	h->count = (int*)malloc(sizeof(int) * size);
	zero(h->count, size);
	h->keys = (unsigned long long*)malloc(sizeof(unsigned long long) * size);
	zero(h->keys, size);
}

int set(Hashcount* h, unsigned long long key)
{
	//knuth's golden ratio multiplicative hashing for 64-bit keys
	int index = (key * 2654435761) >> (64 - h->size_bits);
	int size = 1 << h->size_bits;
	for (int i = 0; i < size; i++)
	{
		if (h->keys[index] == key) //if key exists, overwrite data
		{
			h->count[index]++;
			return index;
		}

		if (h->keys[index] == 0) //empty spot
		{
			h->count[index] = 1;
			h->keys[index] = key;
			return index;
		}

		index++;
		index &= size - 1;
	}

	return -1;//return invalid index if data was not placed. this could be used as an indicator to resize.
}

int get(Hashcount* h, unsigned long long key)
{
	int index = (key * 2654435761) >> (64 - h->size_bits);
	int size = 1 << h->size_bits;
	for (int i = 0; i < size; i++)
	{
		if (h->keys[index] == key)//found original key
		{
			return h->count[index];
		}

		if (h->keys[index] == 0) return 0;//if you see a zero,key was never there

		index++;
		index &= size - 1;
	}
	return 0;
}
//not sure if this works
int sub(Hashcount* h, unsigned long long key)
{
	int index = (key * 2654435761) >> (64 - h->size_bits);
	int size = 1 << h->size_bits;
	for (int i = 0; i < size; i++)
	{
		if (h->keys[index] == key)//found original key
		{
			--h->count[index];
			if (h->count[index] < 0) h->count[index] = 0;
			return h->count[index];
		}

		if (h->keys[index] == 0) return 0;//if you see a zero,key was never there

		index++;
		index &= size - 1;
	}
	return 0;
}

const ll MOD  = 1e9 + 7;
/*
//proper binary search???
//tested on 1 problem it works!!!!!!!
int left = 0;
int right = n-1;
for (;;)
{
	if (left >= right)break;
	int mid = (left + right) >> 1;
	if (x <= a[mid]) right = mid;
	else left = mid+1;
}

printf("%d %d\n",right, a[right]);
*/

//------------------------------------------

//------------

int main()
{

	RUN_LOCAL("input.txt");
	int t = readi();
	rep(_t, t)
	{
		int x[4];
		repread(x, 4);
		int ans = abs(x[0] - x[2]) + abs(x[1] - x[3]);
		if (x[1] - x[3] == 0 || x[0] - x[2] == 0)
		{

		}
		else
		{
			ans += 2;
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <string.h>
#include <algorithm>
#include <math.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vc = vector<char>;
using pii = pair<int, int>;
using vpii = vector<pii>;

#define FORI(i, a, b, p) for (int i = a; i < b; i += p)
#define FOR(i, a, b) FORI(i, a, b, 1)
#define FOR0(i,b) FOR(i, 0, b)
#define RFOR(i, a, b) for (int i = a; i >= b; i--)
#define RFOR0(i, a) RFOR(i, a, 0)

int t;
vi nums[2];

int main() {
	cin >> t;
	while (t--) {
		int x1, y1, x2, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		int dist = abs(x1 - x2) + abs(y1 - y2);
		if (x1 != x2 && y1 != y2) {
			dist += 2;
		}
		cout << dist << '\n';
	}
}


#define _CRT_SECURE_NO_WARNINGS
#define fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define mp make_pair
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#include <algorithm>
#include <string>
# include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
using namespace std;
#define SIZE 500000
#define ll long long
typedef pair<ll, ll> pll;
ll arr[SIZE];

ll howMany(ll x, ll y, ll a, ll b) {
	if (x == a && y == b) return 0;
	if (y == b) return abs(x - a);
	if (x == a) return abs(y - b);
	return 2 + abs(x - a) + howMany(a, y, a, b);
}

int main() {
	ll t, n, temp, mod = 1000000000 + 7, cpow = 0, x, y, a, b;
	cin >> t;
	while (t--) {
		cin >> x >> y >> a >> b;
		cout << howMany(x,y,a,b) << "\n";
	}
}


#include <iostream>

using namespace std;
int t;
int main() {
	ios_base::sync_with_stdio(false);
	cin >> t;
	for (int i = 0; i < t; i++) {
		int x1, y1, x2, y2;
		int cnt = 0;
		cin >> x1 >> y1 >> x2 >> y2;
		int tx = abs(x1 - x2);
		int ty = abs(y1 - y2);
		if (tx != 0 && ty != 0) cnt += 2;
		while (tx--) {
			cnt++;
		}
		while (ty--) {
			cnt++;
		}
		cout << cnt << "\n";
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int test;

    cin>> test;

    for(int i=0; i<test; i++){

        int array[4];

        int arraySize = sizeof(array)/sizeof(array[0]);

        for(int i=0; i<arraySize; i++){

            cin >> array[i];

        }

        if(array[0]==array[2]){
            cout << abs(array[1]-array[3]);
        }else if(array[1]==array[3]){
            cout << abs(array[0]-array[2]);
        }else{
            cout << ((abs(array[1]-array[3]))+(abs(array[0]-array[2]))+2);
        }

        cout << endl;

    }

}


#include <iostream>
using namespace std;

int main()
{
    int test;

    cin>> test;

    for(int i=0; i<test; i++){

        int array[4];

        int arraySize = sizeof(array)/sizeof(array[0]);

        for(int i=0; i<arraySize; i++){

            cin >> array[i];

        }

        if(array[0]==array[2]){
            cout << abs(array[1]-array[3]);
        }else if(array[1]==array[3]){
            cout << abs(array[0]-array[2]);
        }else{
            cout << ((abs(array[1]-array[3]))+(abs(array[0]-array[2]))+2);
        }

        cout << endl;

    }

}


#include<iostream>
#include<algorithm>
#include <string>
#include <stdint.h>
using namespace std;
int absq(int a) {
	if (a >= 0) {
		return a;
	}
	else {
		return a * (-1);
	}
}
int Body() {
	int x1, x2, y1, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	int f = 2;
	if (y1 == y2 || x1 == x2) f = 0;
	cout << absq(x1 - x2) + absq(y1 - y2) + f;
	return 0;
}
int32_t main() {
	int test;
	cin >> test;
	for (int i = 0;i < test;i++) {
		Body();
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include <string>
#include <stdint.h>
using namespace std;
int absq(int a) {
	if (a >= 0) {
		return a;
	}
	else {
		return a * (-1);
	}
}
int Body() {
	int x1, x2, y1, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	int f = 2;
	if (y1 == y2 || x1 == x2) f = 0;
	cout << absq(x1 - x2) + absq(y1 - y2) + f<<" ";
	return 0;
}
int32_t main() {
	int test;
	cin >> test;
	for (int i = 0;i < test;i++) {
		Body();
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include <string>
#include <stdint.h>
using namespace std;
int abs(int a) {
	if (a >= 0) {
		return a;
	}
	else {
		return a * (-1);
	}
}
int Body() {
	int x1, x2, y1, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	int f = 2;
	if (y1 == y2 || x1 == x2) f = 0;
	cout << abs(x1 - x2) + abs(y1 - y2) + f;
	return 0;
}
int32_t main() {
	int test;
	cin >> test;
	for (int i = 0;i < test;i++) {
		Body();
	}
	return 0;
}


#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main()
{
    int t;
    cin >> t;
    int x1, x2, y1, y2;
    for (int h = 0; h < t; h++)
    {
        cin >> x1 >> y1 >> x2 >> y2;
        if (x1 == x2)
            cout << abs(y2 - y1) << endl;
        else if (y1 == y2)
            cout << abs(x2 - x1) << endl;
        else
            cout << abs(x2 - x1) + abs(y2 - y1) + 2 << endl;
    }

}


// codeforce1.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//

#include <iostream>
using namespace std;
#include <stdlib.h>
int main()
{
    int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0; int x; int y; int mul = 2; int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> x1 >> y1 >> x2 >> y2;
        if (x1 == x2 || y1 == y2) mul = 0;
        x = abs(x1 - x2); y = abs(y1 - y2);
        cout << x + y + mul;
    }

}


// codeforce1.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//

#include <iostream>
using namespace std;
#include <stdlib.h>
int main()
{
    int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0; int x; int y; int mul = 2;
    cin >> x1 >> y1 >> x2 >> y2;
    if (x1 == x2 || y1 == y2) mul = 0;
    x = abs(x1 - x2); y = abs(y1 - y2);
    cout << x+y+mul;

}


// codeforce1.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//

#include <iostream>
using namespace std;
#include <stdlib.h>
int main()
{
    int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0; int x; int y; int mul = 2; int n; int ans[1001];
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> x1 >> y1 >> x2 >> y2;
        mul = 2;
        if (x1 == x2 || y1 == y2) mul = 0;
        x = abs(x1 - x2); y = abs(y1 - y2);
        ans[i] = x + y + mul;
        // << x + y + mul;
    }
    for (int i = 0; i < n; i++) {
        cout << ans[i]<<endl;
    }
}


//#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
	int t;
	cin >> t;
	int x1, x2;
	int y1, y2;
	int time = 0;
	while(t--) {
		cin >> x1 >> y1 >> x2 >> y2;

		if (x1 == x2 && y1 == y2) {
			time = 0;
			cout << time;
			break;
		}
		time = abs(y2 - y1) + abs(x2 - x1);
		if (x1 != x2 && y1 != y2)
			time += 2;
		cout << time << endl;
	}
}


//#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
	int t;
	cin >> t;
	long long int x1, x2;
	long long int y1, y2;
	long long int time = 0;
	for (int i = 0; i < t; i++)
	{
		cin >> x1 >> y1 >> x2 >> y2;
		time = abs(y2 - y1) + abs(x2 - x1);
		if (x1 != x2 && y1 != y2)
			time += 2;
		cout << time << endl;
	}
	return 0;
}


#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
	int t;
	cin >> t;
	int x1, x2;
	int y1, y2;
	int time = 0;
	while(t--) {
		cin >> x1 >> y1 >> x2 >> y2;

		if (x1 == x2 && y1 == y2) {
			time = 0;
			cout << time;
			break;
		}
		time = abs(y2 - y1) + abs(x2 - x1);
		if (x1 != x2 && y1 != y2)
			time += 2;
		cout << time << endl;
	}
}


//#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
	int t;
	cin >> t;
	long long int x1, x2;
	long long int y1, y2;
	long long int time = 0;
	while(t--) {
		cin >> x1 >> y1 >> x2 >> y2;

		if (x1 == x2 && y1 == y2) {
			time = 0;
			cout << time;
			break;
		}
		time = abs(y2 - y1) + abs(x2 - x1);
		if (x1 != x2 && y1 != y2)
			time += 2;
		cout << time << endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
int main(){
	int t;
	cin>>t;
	for (int j=0;j<t;j++){
		long long int a,b,c,d,x,y;
		cin>>a>>b>>c>>d;
		if(a==c&&b==d){
			cout<<"0"<<endl;
		}
		else if(a==c||b==d){
			if(a==c){
				cout<<d-b<<endl;
			}
			else{
				cout<<c-a<<endl;
			}
		}
		else{
			x=c-a;
			y=d-b;
			cout<<x+y+2<<endl;

		}

	}

}


#include<iostream>
using namespace std;
int main(){
	int t;
	cin>>t;
	for (int j=0;j<t;j++){
		long long int a,b,c,d,x,y;
		cin>>a>>b>>c>>d;
		if(a==c&&b==d){
			cout<<"0"<<endl;
		}
		else if(a==c||b==d){
			if(a==c){
				int p=d-b;
				if(p<0){
					p=-p;
				}

				cout<<p<<endl;
			}
			else{
				int q=c-a;
				if(q<0){
					q=-q;
				}
				cout<<q<<endl;
			}
		}
		else{
			x=c-a;
			y=d-b;
			if(x<0){
				x=-x;
			}
			if(y<0){
				y=-y;
			}
			cout<<x+y+2<<endl;

		}

	}

}


#include<iostream>
using namespace std;
int main(){
	int t;
	cin>>t;
	for (int j=0;j<t;j++){
		long long int a,b,c,d,x,y;
		cin>>a>>b>>c>>d;
		if(a==c&&b==d){
			cout<<"0";
		}
		else if(a==c||b==d){
			if(a==c){
				cout<<d-b;
			}
			else{
				cout<<c-a;
			}
		}
		else{
			x=c-a;
			y=d-b;
			cout<<x+y+2;

		}

	}

}


#include <iostream>

using namespace std;

int main()
{
	int h_m, coor, secs;
	cin >> h_m;
	secs = 0;
	for (int i = 0; i < h_m; i++)
	{
		int arr[4]{};
		for (int j = 0; j < 4; j++)
		{
			cin >> coor;
			arr[j] = coor;
		}
		if (arr[1] != arr[3])
		{
			coor = abs(arr[0] - arr[2]) + abs(arr[1] - arr[3]) + 2;
		}
		else
		{
			coor = abs(arr[0] - arr[2]);
		}
		cout << coor << endl;
		coor = 0;
	}
}


#include <iostream>

using namespace std;

int main()
{
	int h_m, coor, secs;
	cin >> h_m;
	secs = 0;
	for (int i = 0; i < h_m; i++)
	{
		int arr[4]{};
		for (int j = 0; j < 4; j++)
		{
			cin >> coor;
			arr[j] = coor;
		}
		if (arr[1] != arr[3])
		{
			if (arr[0] != arr[2])
			{
				coor = abs(arr[0] - arr[2]) + abs(arr[1] - arr[3]) + 2;
			}
			else
			{
				coor = abs(arr[1] - arr[3]);
			}
		}
		else
		{
			coor = abs(arr[0] - arr[2]);
		}
		cout << coor << endl;
		coor = 0;
	}
}


#include <iostream>
using namespace std;

int main() {
	int t;
	int start_x, start_y;
	int end_x, end_y;
	int step, step_x, step_y;

	cin >> t;

	int i;
	for (i = 0; i < t; i++) {
		cin >> start_x >> start_y >> end_x >> end_y;
		step_x = abs(end_x - start_x);
		step_y = abs(end_y - start_y);
		step = step_x + step_y;
		if (step_x != 0 && step_y != 0) {
			step += 2;
		}
		cout << step << endl;
	}
}


#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
	int t;
	int start_x, start_y;
	int end_x, end_y;
	int step, step_x, step_y;

	cin >> t;
	int * output = new int (t);

	int i;
	for (i = 0; i < t; i++) {
		cin >> start_x >> start_y >> end_x >> end_y;
		step_x = abs(end_x - start_x);
		step_y = abs(end_y - start_y);
		step = step_x + step_y;
		if (step_x != 0 && step_y != 0) {
			step += 2;
		}
		output[i] = step;
	}
	for (i = 0; i < t; i++) {
		cout << output[i] << endl;
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <string>
#include <stack>

#define ll long long int
#define vec vector<int>
#define deq deque<int>
#define str string
#define get_ cin.get()

using namespace std;

int main()
{
    int t, x1, y1, x2, y2;
    cin >> t;
    while (t--) {
        cin >> x1 >> y1 >> x2 >> y2;
        if (x1 == x2) {
            cout << abs(y1 - y2) << endl;
        }
        else if (y1 == y2) {
            cout << abs(x1 - x2) << endl;
        }
        else {
            cout << abs(x1 - x2) + abs(y1 - y2) + 2 << endl;
        }
    }
}


#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        if (x1 == x2) cout << abs(y1 - y2) << endl;
        else if (y1 == y2) cout << abs(x1 - x2) << endl;
        else cout << abs(x1-x2) + abs(y1-y2) + 2 << endl;
    }
return 0;
}


#include <iostream>
using namespace std;
int main() {
	using namespace std;
	int T;
	cin >> T;
	while (T--) {
		long long x1, x2, y1, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		long long dx = abs(x2 - x1);
		long long dy = abs(y2 - y1);

		if (dx != 0 && dy != 0)
			cout << dx + dy + 2 << endl;
		else
			cout << dx + dy << endl;
	}

	return 0;
}


#include<iostream>
using namespace std;

int main() {
    long long int n, x1, y1, x2, y2, seconds = 0;
    cin >> n;

    while(n > 0) {
        seconds = 0;
        cin >> x1 >> y1 >> x2 >> y2;

        if(x2 > x1 && y2 > y1) {
            seconds += 2;
        }
        if(x2 > x1)
            seconds += x2-x1;
        else if(x2 < x1)
            seconds += x1-x2;

        if(y2 > y1)
            seconds += y2-y1;
        else if(y2 < y1)
            seconds += y1-y2;

        cout << seconds << endl;
        n--;
    }

}


#include<iostream>
using namespace std;

int main() {
    long long int n, x1, y1, x2, y2, seconds = 0;
    cin >> n;

    while(n > 0) {
        seconds = 0;
        cin >> x1 >> y1 >> x2 >> y2;

        if(x2 > x1 && y2 > y1) {
            seconds += 2;
        }
        if(x2 > x1)
            seconds += x2-x1;
        if(y2 > y1)
            seconds += y2-y1;

        cout << seconds << endl;
        n--;
    }

}


#include<iostream>
using namespace std;

int main() {
    long long int n, x1, y1, x2, y2, seconds = 0;
    cin >> n;

    while(n > 0) {
        seconds = 0;
        cin >> x1 >> y1 >> x2 >> y2;

        if(x2 != x1 && y2 != y1) {
            seconds += 2;
        }
        if(x2 > x1)
            seconds += x2-x1;
        else if(x2 < x1)
            seconds += x1-x2;

        if(y2 > y1)
            seconds += y2-y1;
        else if(y2 < y1)
            seconds += y1-y2;

        cout << seconds << endl;
        n--;
    }

}


#include<iostream>
using namespace std;

int main() {
    int n, x1, y1, x2, y2, seconds = 0;
    cin >> n;

    while(n > 0) {
        seconds = 0;
        cin >> x1 >> y1 >> x2 >> y2;
        cout << "(" << x1  << ", " << y1 << ") " << "(" << x2 << ", " << y2 << ")" << endl;
        if(x2 > x1 && y2 > y1) {
            seconds += 2;
        }
        if(x2 > x1)
            seconds += x2-x1;
        if(y2 > y1)
            seconds += y2-y1;

        cout << seconds << " Seconds\n";
        n--;
    }

}


#include<iostream>
using namespace std;

int main() {
    int n, x1, y1, x2, y2, seconds = 0;
    cin >> n;

    while(n > 0) {
        seconds = 0;
        cin >> x1 >> y1 >> x2 >> y2;
        if(x2 > x1 && y2 > y1) {
            seconds += 2;
        }
        if(x2 > x1)
            seconds += x2-x1;
        if(y2 > y1)
            seconds += y2-y1;

        cout << seconds << endl;
        n--;
    }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

#define for_ll(i,a,b) for(ll i=a;i<b;i++)
vector<ll> toBase3(ll n){
	vector<ll> ans;
	while(n>0){
		ans.push_back(n%3);
		n/=3;
	}
	reverse(ans.begin(),ans.end());
	return ans;
}
int main(){
 int q;
 cin >>q;
 while(q--){
 	ll x;
 	cin >>x;
 	vector<ll> base3 = toBase3(x);
 	ll pos = base3.size()-1;
 	for_ll(i,0,base3.size()){
 		if(base3.at(i) == 2){
 				pos = i;
 				break;
		}
	}
	ll pos_0 = -1;
	for(int i = pos; i>=0; i--){
		if(base3[i] == 0) {
			pos_0 = i;
			break;
		}
	}
	ll ans = 0;
	if(base3[pos]!=2){
		for_ll(i,0,base3.size()){
			ans += base3[base3.size()-1-i]*pow(3,i);
		}
		cout <<ans <<endl;
	}
	else {
		vector<ll> ans_v;
		for_ll(i,0,pos_0){
			ans_v.push_back(base3[i]);
		}
		ans_v.push_back(1);
		for_ll(i,pos_0+1,base3.size()){
			ans_v.push_back(0);
		}
		for_ll(i,0,ans_v.size()){
			ans += ans_v[ans_v.size()-1-i]*pow(3,i);
		}
		cout <<ans <<endl;
	}
 }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

#define for_ll(i,a,b) for(ll i=a;i<b;i++)
vector<ll> toBase3(ll n){
	vector<ll> ans;
	while(n>0){
		ans.push_back(n%3);
		n/=3;
	}
	reverse(ans.begin(),ans.end());
	return ans;
}
ll pow2(ll b, ll p){
	ll x = b;
	for_ll(i,0,p){
		x *= b;
	}
	return x;
}
int main(){
 int q;
 cin >>q;

 while(q--){
 	ll x;
 	cin >>x;
 	vector<ll> base3 = toBase3(x);
 	ll pos = base3.size()-1;
 	for_ll(i,0,base3.size()){
 		if(base3.at(i) == 2){
 				pos = i;
 				break;
		}
	}
	ll pos_0 = -1;
	for(int i = pos; i>=0; i--){
		if(base3[i] == 0) {
			pos_0 = i;
			break;
		}
	}
	ll ans = 0;
	if(base3[pos]!=2){
		for_ll(i,0,base3.size()){
			ans += base3[base3.size()-1-i]*pow2(3,i);
		}
		cout <<ans <<endl;
	}
	else {
		vector<ll> ans_v;
		for_ll(i,0,pos_0){
			ans_v.push_back(base3[i]);
		}
		ans_v.push_back(1);
		for_ll(i,pos_0+1,base3.size()){
			ans_v.push_back(0);
		}
		for_ll(i,0,ans_v.size()){
			ans += ans_v[ans_v.size()-1-i]*pow2(3,i);
		}
		cout <<ans <<endl;
	}
 }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int q, n;
	cin >> q;

	int count;

	while(q--)
	{
		cin >> n;

		bool flag=true;
		count = 0;
		int a = n;
		while(n)
		{
			if(n%3==2) flag=false;
			count++;
			n/=3;
		}
		if(!flag) cout << pow(3, count) << endl;
		else cout << a << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;

void operation()
{
	long long n;
	cin >> n;

	vector <long long> result;
	while(n)
	{
		result.push_back(n%3);
		n/=3;
	}
	result.push_back(0);
	long long ans = 0;
	long long cnt = 1;
	for(long long i = 0; i < result.size(); i++)
	{
		if(result[i] == 1)
		ans+=cnt;
		else if(result[i] == 2)
		{
			ans = 0;
			result[i+1]++;
		}
		else
		{
			cnt*=3;
			continue;
		}
		cnt*=3;
	}
	cout << ans << endl;
}

int main()
{
	long long q;
	cin >> q;
	while(q--)
	operation();
}


#include<bits/stdc++.h>
using namespace std;

void operation()
{
	long long n;
	cin >> n;

	vector <long long> result;
	while(n)
	{
		result.push_back(n%3);
		n/=3;
	}
	result.push_back(0);
	long long ans = 0;
	long long cnt = 1;
	for(long long i = 0; i < result.size(); i++)
	{
		if(result[i] == 1)
		ans+=cnt;
		else if(result[i] >= 2)
		{
			ans = 0;
			result[i+1]++;
		}
		else if(result[i] == 0)
		ans+=cnt*0;
		cnt*=3;
	}
	cout << ans << endl;
}

int main()
{
	long long q;
	cin >> q;
	while(q--)
	operation();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

void operation()
{
	long long n;
	cin >> n;

	vector <long long> result;
	while(n)
	{
		result.push_back(n%3);
		n/=3;
	}
	result.push_back(0);
	long long ans = 0;
	long long cnt = 1;
	for(long long i = 0; i < result.size(); i++)
	{
		if(result[i] == 1)
		ans+=cnt;
		else if(result[i] == 2)
		{
			ans = 0;
			result[i+1]++;
		}
		else if(result[i] == 0)
		ans+=cnt*0;
		cnt*=3;
	}
	cout << ans << endl;
}

int main()
{
	long long q;
	cin >> q;
	while(q--)
	operation();
	return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define out return 0
#define pii pair<ll,ll>
#define ll long long int
using namespace std;
ll mn=1e18,mx=-1e18;
const ll mv=1000005;
vector<ll> a,b;
ll q,n;
map<ll,ll> c;
ll f(ll x,ll z,ll w){
	ll y=lower_bound(a.begin(),a.end(),x)-a.begin();
	y--;
	z=x-a[y];
	w+=a[y];
	if(w>=n){
		return w;
	}
	ll v=f(z,0,w);
	if(v==-1){
		return -1;
	}
	return w+v;
}
int main(){
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);
	a.pb(1);
	b.pb(1);
	c[1]=1;
	for(;;){
		ll x=a.back();
		x*=3;
		a.pb(x);
		ll y=b.back();
		y+=x;
		b.pb(y);
		c[x]=1;
		if(x>1e18){
			break;
		}
	}
	cin>>q;
	while(q--){
		cin>>n;
		if(c[n]){
			cout<<n<<"\n";
			continue;
		}
		ll it=lower_bound(a.begin(),a.end(),n)-a.begin();
		it--;
		if(b[it]<n){
			it++;
			cout<<a[it]<<"\n";
			continue;
		}
		if(b[it]==n){
			cout<<b[it]<<"\n";
			continue;
		}
		ll x=f(n,0,0);
		if(x==-1){
			vector<ll>::iterator it=lower_bound(a.begin(),a.end(),n);
			cout<<a[it-a.begin()]<<"\n";
			continue;
		}
		cout<<x<<"\n";
	}
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define out return 0
#define pii pair<ll,ll>
#define ll long long int
using namespace std;
ll mn=1e18,mx=-1e18;
const ll mv=1000005;
vector<ll> a,b;
ll q,n;
map<ll,ll> c;
ll f(ll x,ll z,ll w){
	ll y=lower_bound(a.begin(),a.end(),x)-a.begin();
	y--;
	z=x-a[y];
	w+=a[y];
	if(w>=n){
		return w;
	}
	ll v=f(z,0,w);
	if(v==-1){
		return -1;
	}
	return w+v;
}
int main(){
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);
	a.pb(1);
	b.pb(1);
	c[1]=1;
	for(;;){
		ll x=a.back();
		x*=3;
		a.pb(x);
		ll y=b.back();
		y+=x;
		b.pb(y);
		c[x]=1;
		if(x>1e18){
			break;
		}
	}
	cin>>q;
	while(q--){
		cin>>n;
		if(c[n]){
			cout<<n<<"\n";
			continue;
		}
		ll it=upper_bound(a.begin(),a.end(),n)-a.begin();
		it--;
		if(b[it]<n){
			it++;
			cout<<a[it]<<"\n";
			continue;
		}
		if(b[it]==n){
			cout<<b[it]<<"\n";
			continue;
		}
		ll x=f(n,0,0);
		if(x==-1){
			vector<ll>::iterator it=upper_bound(a.begin(),a.end(),n);
			cout<<a[it-a.begin()]<<"\n";
			continue;
		}
		cout<<x<<"\n";
	}
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define out return 0
#define pii pair<ll,ll>
#define ll long long int
using namespace std;
ll mn=1e18,mx=-1e18;
const ll mv=1000005;
vector<ll> a,b;
ll q,n;
map<ll,ll> c;
ll f(ll x,ll z,ll w){
	ll y=upper_bound(a.begin(),a.end(),x)-a.begin();
	y--;
	z=x-a[y];
	w+=a[y];
	if(w>=n){
		return w;
	}
	ll v=f(z,0,w);
	if(v==-1){
		return -1;
	}
	return w+v;
}
int main(){
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);
	a.pb(1);
	b.pb(1);
	c[1]=1;
	for(;;){
		ll x=a.back();
		x*=3;
		a.pb(x);
		ll y=b.back();
		y+=x;
		b.pb(y);
		c[x]=1;
		if(x>1e18){
			break;
		}
	}
	cin>>q;
	while(q--){
		cin>>n;
		if(c[n]){
			cout<<n<<"\n";
			continue;
		}
		ll it=upper_bound(a.begin(),a.end(),n)-a.begin();
		it--;
		if(b[it]<n){
			it++;
			cout<<a[it]<<"\n";
			continue;
		}
		if(b[it]==n){
			cout<<b[it]<<"\n";
			continue;
		}
		ll x=f(n,0,0);
		cout<<x<<"\n";
	}
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define out return 0
#define pii pair<ll,ll>
#define ll long long int
using namespace std;
ll mn=1e18,mx=-1e18;
const ll mv=1000005;
vector<ll> a,b;
ll q,n;
map<ll,ll> c;
ll f(ll x,ll z,ll w,ll t){
	ll y=lower_bound(a.begin(),a.end(),x)-a.begin();
	if(a[y]/2>=x){
		y--;
	}
	z=x-a[y];
	if(a[y]>=t){
		return -1;
	}
	w+=a[y];
	t=min(t,a[y]);
	if(w>=n){
		return w;
	}
	ll v=f(z,0,w,t );
	if(v==-1){
		return -1;
	}
	return v;
}
int main(){
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);
	a.pb(1);
	b.pb(1);
	c[1]=1;
	for(;;){
		ll x=a.back();
		x*=3;
		a.pb(x);
		ll y=b.back();
		y+=x;
		b.pb(y);
		c[x]=1;
		if(x>1e18){
			break;
		}
	}
	cin>>q;
	while(q--){
		cin>>n;
		if(c[n]){
			cout<<n<<"\n";
			continue;
		}
		ll it=upper_bound(a.begin(),a.end(),n)-a.begin();
		it--;
		if(b[it]<n){
			it++;
			cout<<a[it]<<"\n";
			continue;
		}
		ll x=f(n,0,0,mn);
		if(x==-1){
			ll it=upper_bound(a.begin(),a.end(),n)-a.begin();
			cout<<a[it]<<"\n";
			continue;
		}
		cout<<x<<"\n";
	}
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define out return 0
#define pii pair<ll,ll>
#define ll long long int
using namespace std;
ll mn=1e18,mx=-1e18;
const ll mv=1000005;
vector<ll> a,b;
ll q,n;
map<ll,ll> c;
ll f(ll x,ll z,ll w){
	ll y=upper_bound(a.begin(),a.end(),x)-a.begin();
	y--;
	z=x-a[y];
	w+=a[y];
	if(w>=n){
		return w;
	}
	ll v=f(z,0,w);
	if(v==-1){
		return -1;
	}
	return v;
}
int main(){
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);
	a.pb(1);
	b.pb(1);
	c[1]=1;
	for(;;){
		ll x=a.back();
		x*=3;
		a.pb(x);
		ll y=b.back();
		y+=x;
		b.pb(y);
		c[x]=1;
		if(x>1e18){
			break;
		}
	}
	cin>>q;
	while(q--){
		cin>>n;
		if(c[n]){
			cout<<n<<"\n";
			continue;
		}
		ll it=upper_bound(a.begin(),a.end(),n)-a.begin();
		it--;
		if(b[it]<n){
			it++;
			cout<<a[it]<<"\n";
			continue;
		}
		ll x=f(n,0,0);
		cout<<x<<"\n";
	}
}


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

#define Scd(a) scanf("%d",&a)
using namespace std;
typedef long long ll;
const int maxn = 200010;
const int maxm = 1e6 + 10;
const int inf = 0x3f3f3f3f;
ll digitx[100];
ll x, y;
int cnt = 0;
void trans(ll num) {
	cnt = 0;
	while (num) {
		digitx[cnt++] = num % 3;
		num /= 3;
	}
}

void add(int st) {
	bool flag = 0;
	int jin = 0, c = 0;

	for (int i = st; i < cnt; i++) {
		digitx[i] += jin;
		jin = 0;
		if (i == cnt - 1 && digitx[i] == 1) {
			break;
		}
		if (digitx[i] > 1) {
			digitx[i] = 0;
			jin = 1;
			c = i;
		}
	}
	for (int i = 0; i < c; i++) digitx[i] = 0;
	if (jin) digitx[cnt++] = 1;
}

ll getans() {
	ll res = 0, bas = 1;
	for (int i = 0; i < cnt; i++, bas = bas * 3) {
		if (digitx[i])
			res = res + bas;
	}
	return res;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%I64d", &x);
		trans(x);
		bool adf = false;
		int st = -1;
		for (int i = 0; i < cnt; i++) if (digitx[i] > 1) {
			adf = true;
			st = i;
			break;
		}
//		for (int i = cnt-1; i+1; i--) printf("%I64d", digitx[i]);printf("\n");
		if (adf == true) {
			add(st);
		}
//		for (int i = cnt-1; i+1; i--) printf("%I64d", digitx[i]);printf("\n");
		ll ans = getans();
		printf("%I64d\n", ans);
	}
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

using namespace std;
typedef long long ll;
ll digitx[100];
ll x, y;
int cnt = 0;

void trans(ll num) {//转化3进制
	cnt = 0;
	while (num) {
		digitx[cnt++] = num % 3;
		num /= 3;
	}
}

void add(int st) {
	bool flag = 0;
	int jin = 0, c = 0;
	//jin 进位 c 最高位
	for (int i = st; i < cnt; i++) {
		digitx[i] += jin;
		jin = 0;
		if (digitx[i] == 2) {//如果遇到了2清0，进位，记录位数
			digitx[i] = 0;
			jin = 1;
			c = i;
		}
	}

	for (int i = 0; i < c; i++) digitx[i] = 0;
	//把后面的数字清成0，尽可能小
	if (jin) digitx[cnt++] = 1;
}

ll getans() {
	ll res = 0, bas = 1;
	for (int i = 0; i < cnt; i++, bas = bas * 3) {
		if (digitx[i])
			res = res + bas;
	}
	return res;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%I64d", &x);
		trans(x);
		bool adf = false;
		int st = -1;
		for (int i = 0; i < cnt; i++)
			if (digitx[i] > 1) {//如果三进制数有 2
				adf = true;
				st = i;
				break;
			}
		if (adf == true) {//就要把2都变成0，进位
			add(st);
		}
		ll ans = getans();
		printf("%I64d\n", ans);
	}
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

using namespace std;
typedef long long ll;
ll digitx[100];
ll x, y;
int cnt = 0;

void trans(ll num) {//转化3进制
	cnt = 0;
	while (num) {
		digitx[cnt++] = num % 3;
		num /= 3;
	}
}

void add(int st) {
	bool flag = 0;
	int jin = 0, c = 0;
	//jin 进位 c 最高位
	for (int i = st; i < cnt; i++) {
		digitx[i] += jin;
		jin = 0;
//		if (i == cnt - 1 && digitx[i] == 1) {
//			break;
//		}
		if (digitx[i] > 1) {
			digitx[i] = 0;
			jin = 1;
			c = i;
		}
	}

	for (int i = 0; i < c; i++) digitx[i] = 0;
	//把后面的数字清成0，尽可能小
	if (jin) digitx[cnt++] = 1;
}

ll getans() {
	ll res = 0, bas = 1;
	for (int i = 0; i < cnt; i++, bas = bas * 3) {
		if (digitx[i])
			res = res + bas;
	}
	return res;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%I64d", &x);
		trans(x);
		bool adf = false;
		int st = -1;
		for (int i = 0; i < cnt; i++)
			if (digitx[i] > 1) {//如果三进制数有 2
				adf = true;
				st = i;
				break;
			}
		if (adf == true) {//就要把2都变成0，进位
			add(st);
		}
		ll ans = getans();
		printf("%I64d\n", ans);
	}
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

#define Scd(a) scanf("%d",&a)
using namespace std;
typedef long long ll;
const int maxn = 200010;
const int maxm = 1e6 + 10;
const int inf = 0x3f3f3f3f;
ll digitx[100];
ll x, y;
int cnt = 0;
void trans(ll num) {
	cnt = 0;
	while (num) {
		digitx[cnt++] = num % 3;
		num /= 3;
	}
}

void add(int st) {
	bool flag = 0, jin = 0;
	for (int i = st; i < cnt; i++) {
		digitx[i] += jin;
		jin = 0;
		if (i == cnt - 1 && digitx[i] == 1) {
			break;
		}
		if (digitx[i] > 1) {
			digitx[i] = 0;
			jin = 1;
		}
	}
	if (jin) digitx[cnt++] = 1;
}

ll getans() {
	ll res = 0, bas = 1;
	for (int i = 0; i < cnt; i++, bas = bas * 3) {
		if (digitx[i])
			res = res + bas;
	}
	return res;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%I64d", &x);
		trans(x);
		bool adf = false;
		int st = -1;
		for (int i = 0; i < cnt; i++) if (digitx[i] > 1) {
			adf = true;
			st = i;
			break;
		}
//		for (int i = cnt-1; i+1; i--) printf("%I64d", digitx[i]);printf("\n");
		if (adf == true) {
			add(st);
		}
//		for (int i = cnt-1; i+1; i--) printf("%I64d", digitx[i]);printf("\n");
		ll ans = getans();
		printf("%I64d\n", ans);
	}
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

#define Scd(a) scanf("%d",&a)
using namespace std;
typedef long long ll;
const int maxn = 200010;
const int maxm = 1e6 + 10;
const int inf = 0x3f3f3f3f;
ll digitx[100];
ll x, y;
int cnt = 0;
void trans(ll num) {
	cnt = 0;
	while (num) {
		digitx[cnt++] = num % 3;
		num /= 3;
	}
}

void add(int st) {
	bool flag = 0, jin = 0;
	for (int i = 0; i < st; i++) digitx[i] = 0;
	for (int i = st; i < cnt; i++) {
		digitx[i] += jin;
		jin = 0;
		if (i == cnt - 1 && digitx[i] == 1) {
			break;
		}
		if (digitx[i] > 0) {
			digitx[i] = 0;
			jin = 1;
		}
	}
	if (jin) digitx[cnt++] = 1;
}

ll getans() {
	ll res = 0, bas = 1;
	for (int i = 0; i < cnt; i++, bas = bas * 3) {
		if (digitx[i])
			res = res + bas;
	}
	return res;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%I64d", &x);
		trans(x);
		bool adf = false;
		int st = -1;
		for (int i = 0; i < cnt; i++) if (digitx[i] > 1) {
			adf = true;
			st = i;
			break;
		}
//		for (int i = cnt-1; i+1; i--) printf("%I64d", digitx[i]);printf("\n");
		if (adf == true) {
			add(st);
		}
//		for (int i = cnt-1; i+1; i--) printf("%I64d", digitx[i]);printf("\n");
		ll ans = getans();
		printf("%I64d\n", ans);
	}
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

using namespace std;
typedef long long ll;
ll digitx[100];
ll x, y;
int cnt = 0;

void trans(ll num) {//转化3进制
	cnt = 0;
	while (num) {
		digitx[cnt++] = num % 3;
		num /= 3;
	}
}

void add(int st) {
	bool flag = 0;
	int jin = 0, c = 0;
	//jin 进位 c 最高位
	for (int i = st; i < cnt; i++) {
		digitx[i] += jin;
		jin = 0;
		if (digitx[i] > 1) {
			digitx[i] = 0;
			jin = 1;
			c = i;
		}
	}

	for (int i = 0; i < c; i++) digitx[i] = 0;
	//把后面的数字清成0，尽可能小
	if (jin) digitx[cnt++] = 1;
}

ll getans() {
	ll res = 0, bas = 1;
	for (int i = 0; i < cnt; i++, bas = bas * 3) {
		if (digitx[i])
			res = res + bas;
	}
	return res;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%I64d", &x);
		trans(x);
		bool adf = false;
		int st = -1;
		for (int i = 0; i < cnt; i++)
			if (digitx[i] > 1) {//如果三进制数有 2
				adf = true;
				st = i;
				break;
			}
		if (adf == true) {//就要把2都变成0，进位
			add(st);
		}
		ll ans = getans();
		printf("%I64d\n", ans);
	}
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

#define Scd(a) scanf("%d",&a)
using namespace std;
typedef long long ll;
const int maxn = 200010;
const int maxm = 1e6 + 10;
const int inf = 0x3f3f3f3f;
ll digitx[100];
ll x, y;
int cnt = 0;
void trans(ll num) {
	cnt = 0;
	while (num) {
		digitx[cnt++] = num % 3;
		num /= 3;
	}
}

void add() {
	bool flag = 0, jin = 0;
	for (int i = 0; i < cnt; i++) {
		digitx[i] += jin;
		jin = 0;
		if (i == cnt - 1 && digitx[i] == 1) {
			break;
		}
		if (digitx[i] > 1) {
			digitx[i] = 0;
			jin = 1;
		}
	}
	if (jin) digitx[cnt++] = 1;
}

ll getans() {
	ll res = 0, bas = 1;
	for (int i = 0; i < cnt; i++, bas = bas * 3) {
		if (digitx[i])
			res = res + bas;
	}
	return res;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%I64d", &x);
		trans(x);
		bool adf = false;
		for (int i = 0; i < cnt; i++) if (digitx[i] > 1) {
			adf = true;
			break;
		}
		if (adf == true) {
			x++;
			cnt = 0;
			trans(x);
			add();
		}
		ll ans = getans();
		printf("%I64d\n", ans);
	}
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

#define Scd(a) scanf("%d",&a)
using namespace std;
typedef long long ll;
const int maxn = 200010;
const int maxm = 1e6 + 10;
const int inf = 0x3f3f3f3f;
ll digitx[100];
ll x, y;
int cnt = 0;
void trans(ll num) {
	cnt = 0;
	while (num) {
		digitx[cnt++] = num % 3;
		num /= 3;
	}
}

void add() {
	bool flag = 0, jin = 0;
	for (int i = 0; i < cnt; i++) {
		digitx[i] += jin;
		jin = 0;
		if (i == cnt - 1 && digitx[i] == 1) {
			break;
		}
		if (digitx[i] > 0) {
			digitx[i] = 0;
			jin = 1;
		}
	}
	if (jin) digitx[cnt++] = 1;
}

ll getans() {
	ll res = 0, bas = 1;
	for (int i = 0; i < cnt; i++, bas = bas * 3) {
		if (digitx[i])
			res = res + bas;
	}
	return res;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%I64d", &x);
		trans(x);
		bool adf = false;
		for (int i = 0; i < cnt; i++) if (digitx[i] > 1) {
			adf = true;
			break;
		}
		if (adf == true) {
			x++;
			add();
		}
		ll ans = getans();
		printf("%I64d\n", ans);
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const unsigned long long maxn = 1e18;
int q;
unsigned long long n;
int main(){
	vector<unsigned long long> v;
	unsigned long long x = 1;
	while(x<maxn){
		v.push_back(x);
		x = x*3;
	}
	v.push_back(1350851717672992089);
	cin>>q;
	while(q--){
		cin>>n;
		int i = 0;
		for(i=0;i<v.size();i++){
			if(v[i]>=n){
				break;
			}
		}
		if(v[i]==n){
			cout<<n<<endl;
		}else{
			if(v[i]>n){
				i--;
			}
			long long tmp = v[i];
			for(int j=0;j<i;j++){
				tmp +=v[j];
				if(tmp>=n){
					break;
				}
			}
			if(tmp<n){
				cout<<v[i+1]<<endl;
			}else{
				cout<<tmp<<endl;
			}
		}
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const  long long maxn = 1e18;
int q;
long long n;
vector<long long> v;
long long sum[40];
long long ex(long long n){
		int i=0;
		for(i=0;i<v.size();i++){
			if(v[i]>=n){
				break;
			}
		}
		if(v[i]==n || sum[i-1]<n ){
			return v[i];
		}else{
			return v[i-1] + ex(n-v[i-1]);
		}
}
int main(){

	long long x = 1;
	while(x<maxn){
		v.push_back(x);
		x = x*3;
	}
	v.push_back(1350851717672992089);
	sum[0] = v[0];
	for(int i=1;i<v.size();i++){
		sum[i] = sum[i-1] + v[i];
	}
	cin>>q;
	while(q--){
		cin>>n;
		cout<<ex(n)<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const unsigned long long maxn = 1e18;
int q;
unsigned long long n;

set<unsigned long long> sss;

int main(){
	vector<unsigned long long> v;
	unsigned long long x = 1;
	while(x<maxn){
		v.push_back(x);
		x = x*3;
	}
	for(int i=0;i<v.size();i++){
		sss.insert(v[i]);
		for(int j=0;j<v.size();j++){
			if(j==i){
				continue;
			}
			if(v[i] >1350851717672992089 - v[j])continue;
			sss.insert(v[i]+v[j]);
			for(int k=0;k<v.size();k++){
				if(k==i||k==j)continue;
				if(v[i]>1350851717672992089 -v[j] - v[k])continue;
				sss.insert(v[i]+v[j]+v[k]);
			}
		}
	}
	sss.insert(1350851717672992089);
//	for(auto it:sss){
//		cout<<it<<" ";
//	}
	cin>>q;
	while(q--){
		cin>>n;
		auto it =sss.lower_bound(n);
		cout<<*it<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const unsigned long long maxn = 1e18;
int q;
unsigned long long n;
int main(){
	vector<unsigned long long> v;
	unsigned long long x = 1;
	while(x<maxn){
		v.push_back(x);
		x = x*3;
	}
	v.push_back(1350851717672992089);
	cin>>q;
	while(q--){
		cin>>n;
		int i = 0;
		for(i=0;i<v.size();i++){
			if(v[i]>=n){
				break;
			}
		}
		if(v[i]==n){
			cout<<n<<endl;
		}else{
			if(v[i]>n){
				i--;
			}
			long long tmp = v[i];
			for(int j=0;j<i;j++){
				tmp +=v[j];
				if(tmp>=n){
					cout<<tmp<<endl;
					break;
				}
			}
			if(tmp<n){
				cout<<v[i+1]<<endl;
			}
		}

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1000005;
typedef long long ll;
ll pw[N],ans,n,T;
bool inq[N];
ll read()
{
    ll x=0,ch=getchar(),f=1;
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0',ch=getchar();}
    return x*f;
}
int main(){
    pw[0]=1ll; for( int i=1;i<=38;++i) pw[i]=pw[i-1]*3;
    T=read();
    while(T--){
        n=read(),ans=0;
        for(int i=0;i<=38;++i) inq[i]=0;
        for(int i=38;i>=0;--i) if(n>pw[i]) n-=pw[i],ans+=pw[i],inq[i]=1;
        for(int i=0;i<=38;++i) if(inq[i])  ans-=pw[i]; else{ ans+=pw[i];break;}
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<cstring>
#define int long long
using namespace std;
const int maxn=1010;
int a[maxn];
signed main()
{
    int q;cin>>q;
    while(q--)
    {
        memset(a,0,sizeof(a));
        int x;cin>>x;int cnt=0;
        int y=x;
        while(x) a[cnt]=x%3,x/=3,cnt++;
        cnt--;
        int i,j;
        for(i=cnt;a[i]!=2&&i>=0;i--);
        if(i==-1)
        {
            cout<<y<<endl;
            continue;
        }
        for(j=i;a[j]!=0;j++);
        a[j]=1;
        for(int k=j-1;k>=0;k--) a[k]=0;
        int ans=0;
        for(int i=max(cnt,j);i>=0;i--) ans=ans*3+a[i];
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int p[60],T;
int main(){
    scanf("%d",&T);
    while(T--){
		memset(p,0,sizeof(p));
		ll n,pos=0,sum=0,P=1;
        scanf("%lld",&n);
        while(n) p[pos++]=n%3,n/=3;
        int k=pos-1;
        while(k&&p[k]!=2) k--;
        for(int i=k-1;i>=0;i--) p[i]=0;
        while(p[k]==2) p[k]=0,p[++k]++;
        pos=max(pos,k+1LL);
        for(int i=0;i<pos;i++){
            if(p[i]==1) sum+=P;
            P*=3;
        }
        printf("%lld\n",sum);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
long long cnt,q,n,flg,ans,tmp[2000];
ll poww(ll a,ll p)
{
	ll ans=1,base=a;
	for(;p;p>>=1)
	{
		if(p&1) ans=ans*base;
		base=base*base;
	}
	return ans;
}
int main()
{
  scanf("%d",&q);
  while(q--)
  {
  	cnt=0;
  	memset(tmp,0,sizeof(tmp));
  	ans=0;
  	cin>>n;
    while(n)
	{
	  tmp[++cnt]=n%3;
	  n/=3;
	}
	while(1)
	{
	flg=0;
    for(int i=1;i<=cnt;i++)
    if(tmp[i]==2) flg=1;
	if(flg==0) break;
	for(int i=1;i<=cnt;i++)
	if(tmp[i]>=1)
  {
	tmp[i]++;
    break;
  }
	for(int i=1;i<=cnt;i++)
	if(tmp[i]==3) tmp[i]=0,tmp[i+1]++;
   }
	for(int i=1;i<=cnt+1;i++)
	if(tmp[i]==1)ans+=poww(3,(i-1));
	cout<<ans<<endl;
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n;
int t;
int p[100];

int main(){
	cin>>t;
	while(t--){
		memset(p,0,sizeof(p));
	cin>>n;
	long long ans=0,temp=n;
	int sum=0;
	while(n){
		p[++sum]=n%3;
		n/=3;
	}
	bool flag=1;
	for(int i=sum+1;i>=1;i--){
	      if((pow(3,i-1)-1)/2+ans<temp){
	      	ans+=pow(3,i-1);
	      }
	}
	if(ans<temp)ans++;
	cout<<ans<<endl;
	}

	return 0;
}