There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$.

Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le i < n$), so two rows form two independent computer networks.

Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $i$-th computer from the first row and the $j$-th computer from the second row costs $|a_i - b_j|$.

You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.

That is the minimum total cost to make a fault-tolerant network?


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next $t$ cases follow.

The first line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of computers in each row.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the grades of computers in the first row.

The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the grades of computers in the second row.

It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$.


-----Output-----

For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.


-----Examples-----

Input
2
3
1 10 1
20 4 25
4
1 1 1 1
1000000000 1000000000 1000000000 1000000000
Output
31
1999999998


-----Note-----

In the first test case, it's optimal to connect four pairs of computers:

computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;

computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$;

computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$;

computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$;

In total, $3 + 3 + 10 + 15 = 31$.

In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.

Difficulty: MEDIUM

Solutions:
from bisect import bisect_right

def helper(ele, l1):
	ind = bisect_right(l1, ele)
	if ind == len(l1):
		return abs(l1[ind - 1] - ele)
	elif ind == 0:
		return abs(l1[0] - ele)
	else:
		return min(abs(l1[ind - 1] - ele), abs(l1[ind] - ele))
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	(a0, a1) = (a[0], a[n - 1])
	b = [int(i) for i in input().split()]
	(b0, b1) = (b[0], b[n - 1])
	a.sort()
	b.sort()
	(c1, c2, c3, c4) = (helper(a0, b), helper(a1, b), helper(b0, a), helper(b1, a))
	ans = min(abs(a0 - b0) + abs(b1 - a1), abs(a1 - b0) + abs(a0 - b1))
	ans = min(ans, abs(a0 - b0) + c2 + c4)
	ans = min(ans, abs(a0 - b1) + c2 + c3)
	ans = min(ans, abs(a1 - b1) + c1 + c3)
	ans = min(ans, abs(a1 - b0) + c1 + c4)
	ans = min(ans, c1 + c4 + c2 + c3)
	print(ans)


import sys
input = sys.stdin.readline

def solve(n, A, B):
	ans = min(abs(A[0] - B[0]) + abs(A[-1] - B[-1]), abs(A[0] - B[-1]) + abs(A[-1] - B[0]))
	(minA1, minB1) = (min([abs(A[0] - b) for b in B]), min([abs(a - B[0]) for a in A]))
	(minA2, minB2) = (min([abs(A[-1] - b) for b in B]), min([abs(a - B[-1]) for a in A]))
	ans = min(ans, abs(A[0] - B[0]) + minA2 + minB2)
	ans = min(ans, abs(A[-1] - B[-1]) + minA1 + minB1)
	ans = min(ans, abs(A[0] - B[-1]) + minB1 + minA2)
	ans = min(ans, abs(A[-1] - B[0]) + minB2 + minA1)
	ans = min(ans, minA1 + minB1 + minA2 + minB2)
	print(ans)
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	solve(n, A, B)


def solve_problem():
	for _ in range(int(input())):
		_ = input()
		row1 = [int(i) for i in input().split()]
		row2 = [int(i) for i in input().split()]
		r10 = row1[0]
		r20 = row2[0]
		r11 = row1[-1]
		r21 = row2[-1]
		diff0 = abs(r10 - r20)
		diff1 = abs(r11 - r21)
		left1 = min((abs(r10 - j) for j in row2))
		left2 = min((abs(r20 - j) for j in row1))
		right1 = min((abs(r11 - j) for j in row2))
		right2 = min((abs(r21 - j) for j in row1))
		left = left1 + left2
		right = right1 + right2
		res = left + right
		excep1 = diff0 + diff1
		if excep1 < res:
			res = excep1
		excep2 = diff0 + right
		if excep2 < res:
			res = excep2
		excep3 = abs(r10 - r21) + left2 + right1
		if excep3 < res:
			res = excep3
		excep4 = diff1 + left
		if excep4 < res:
			res = excep4
		excep5 = abs(r11 - r20) + left1 + right2
		if excep5 < res:
			res = excep5
		excep6 = abs(r10 - r21) + abs(r11 - r20)
		if excep6 < res:
			res = excep6
		print(res)
solve_problem()


def best(A, i):
	return min([abs(i - j) for j in A])

def solv(A, B):
	(a1, an) = (A[0], A[-1])
	(b1, bn) = (B[0], B[-1])
	a1b = best(B, a1)
	anb = best(B, an)
	b1b = best(A, b1)
	bnb = best(A, bn)
	d11 = min(abs(a1 - b1), a1b + b1b)
	dnn = min(abs(an - bn), anb + bnb)
	d1n = min(abs(a1 - bn), a1b + bnb)
	dn1 = min(abs(an - b1), anb + b1b)
	return min(d11 + dnn, d1n + dn1)
for _ in range(int(input())):
	input()
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	print(solv(A, B))


def findmin(x, seq):
	t = 1000000000
	for i in seq:
		if abs(x - i) < t:
			t = abs(x - i)
			choose = i
	return (choose, t)
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	(ca0, xa0) = findmin(a[0], b)
	(ca1, xa1) = findmin(a[-1], b)
	(cb0, xb0) = findmin(b[0], a)
	(cb1, xb1) = findmin(b[-1], a)
	match00 = ca0 == b[0] or cb0 == a[0]
	match01 = ca0 == b[-1] or cb1 == a[0]
	match10 = ca1 == b[0] or cb0 == a[-1]
	match11 = ca1 == b[-1] or cb1 == a[-1]
	value00 = min(xa0 + xb0, abs(a[0] - b[0]))
	value01 = min(xa0 + xb1, abs(a[0] - b[-1]))
	value10 = min(xa1 + xb0, abs(a[-1] - b[0]))
	value11 = min(xa1 + xb1, abs(a[-1] - b[-1]))
	if match00:
		value00 = xa0 if ca0 == b[0] else xb0
	if match01:
		value01 = xa0 if ca0 == b[-1] else xb1
	if match10:
		value10 = xa1 if ca1 == b[0] else xb0
	if match11:
		value11 = xa1 if ca1 == b[-1] else xb1
	print(min(value00 + value11, value01 + value10))


import sys
readline = sys.stdin.readline
T = int(readline())
while T:
	T -= 1
	length = int(readline())
	a = [int(i) for i in readline().split()]
	b = [int(i) for i in readline().split()]
	a_begin = min((abs(a[0] - element) for element in b))
	a_end = min((abs(a[-1] - element) for element in b))
	b_begin = min((abs(b[0] - element) for element in a))
	b_end = min((abs(b[-1] - element) for element in a))
	begin_connect = abs(a[0] - b[0])
	end_connect = abs(a[-1] - b[-1])
	left_cross = abs(a[0] - b[-1])
	right_cross = abs(a[-1] - b[0])
	full_cross = right_cross + left_cross
	cases = [begin_connect + end_connect, begin_connect + a_end + b_end, end_connect + a_begin + b_begin, a_begin + a_end + b_begin + b_end, full_cross, left_cross + a_end + b_begin, right_cross + b_end + a_begin]
	print(min(cases))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	cost_00 = min([abs(arr[0] - i) for i in arr2])
	cost_01 = min([abs(arr[-1] - i) for i in arr2])
	cost_10 = min([abs(arr2[0] - i) for i in arr])
	cost_11 = min([abs(arr2[-1] - i) for i in arr])
	cst = (cost_00 + cost_01 + cost_10 + cost_11, cost_01 + abs(arr[0] - arr2[0]) + cost_11, cost_00 + cost_10 + abs(arr[-1] - arr2[-1]), cost_00 + abs(arr[-1] - arr2[0]) + cost_11, cost_01 + abs(arr[0] - arr2[-1]) + cost_10, abs(arr[0] - arr2[0]) + abs(arr[-1] - arr2[-1]), abs(arr[0] - arr2[-1]) + abs(arr[-1] - arr2[0]), abs(arr[-1] - arr2[0]) + abs(arr[0] - arr2[-1]))
	print(min(cst))


tc_n = int(input())

def read_int_ln():
	return [int(x) for x in input().split()]

def c(x, y):
	return abs(x - y)

def mc(x, arr):
	return min((abs(x - y) for y in arr))
for tc in range(tc_n):
	n = int(input())
	a = read_int_ln()
	b = read_int_ln()
	c0 = c(a[0], b[0])
	c1 = c(a[-1], b[-1])
	cx01 = c(a[0], b[-1])
	cx10 = c(a[-1], b[0])
	mca0 = mc(a[0], b)
	mca1 = mc(a[-1], b)
	mcb0 = mc(b[0], a)
	mcb1 = mc(b[-1], a)
	res = min(c0 + c1, cx01 + cx10)
	res = min(res, c0 + mca1 + mcb1)
	res = min(res, c1 + mca0 + mcb0)
	res = min(res, cx01 + mca1 + mcb0)
	res = min(res, cx10 + mca0 + mcb1)
	res = min(res, mca0 + mca1 + mcb0 + mcb1)
	print(res)


import sys
input = sys.stdin.readline
N = int(input())
for i in range(N):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	m1 = min_cost_a_1 = min([abs(a[0] - b[i]) for i in range(n)])
	m2 = min_cost_b_1 = min([abs(b[0] - a[i]) for i in range(n)])
	m3 = min_cost_a_n = min([abs(a[n - 1] - b[i]) for i in range(n)])
	m4 = min_cost_b_n = min([abs(b[n - 1] - a[i]) for i in range(n)])
	print(min([m1 + m2 + abs(a[n - 1] - b[n - 1]), m1 + m4 + abs(a[n - 1] - b[0]), m3 + m2 + abs(a[0] - b[n - 1]), m3 + m4 + abs(a[0] - b[0]), abs(a[0] - b[0]) + abs(a[n - 1] - b[n - 1]), abs(a[n - 1] - b[0]) + abs(a[0] - b[n - 1]), m1 + m2 + m3 + m4]))


def main():
	results = []
	number_of_test_cases = int(input())
	for _ in range(number_of_test_cases):
		n = int(input())
		a = list(map(int, input().split()))
		b = list(map(int, input().split()))
		minimum = min(abs(a[0] - b[0]) + abs(a[-1] - b[-1]), abs(a[0] - b[-1]) + abs(a[-1] - b[0]))
		minimum_a_start = min([abs(a[0] - b[i]) for i in range(n)])
		minimum_a_end = min([abs(a[-1] - b[i]) for i in range(n)])
		minimum_b_start = min([abs(a[i] - b[0]) for i in range(n)])
		minimum_b_end = min([abs(a[i] - b[-1]) for i in range(n)])
		minimum = min(minimum, abs(a[0] - b[0]) + minimum_a_end + minimum_b_end)
		minimum = min(minimum, abs(a[0] - b[-1]) + minimum_a_end + minimum_b_start)
		minimum = min(minimum, abs(a[-1] - b[0]) + minimum_a_start + minimum_b_end)
		minimum = min(minimum, abs(a[-1] - b[-1]) + minimum_a_start + minimum_b_start)
		minimum = min(minimum, minimum_a_start + minimum_a_end + minimum_b_start + minimum_b_end)
		results.append(minimum)
	for result in results:
		print(result)
main()


def solve_problem():
	n = int(input())
	for _ in range(n):
		nb_computers = int(input())
		tmp = nb_computers - 1
		row1 = [int(i) for i in input().split()]
		row2 = [int(i) for i in input().split()]
		result = 10000000000
		left1 = min((abs(row1[0] - j) for j in row2))
		left2 = min((abs(row2[0] - j) for j in row1))
		right1 = min((abs(row1[-1] - j) for j in row2))
		right2 = min((abs(row2[-1] - j) for j in row1))
		excep1 = abs(row1[0] - row2[0]) + abs(row1[-1] - row2[-1])
		excep2 = abs(row1[0] - row2[0]) + right1 + right2
		excep3 = abs(row1[0] - row2[-1]) + left2 + right1
		excep4 = abs(row1[-1] - row2[-1]) + left1 + left2
		excep5 = abs(row1[-1] - row2[0]) + left1 + right2
		excep6 = abs(row1[0] - row2[-1]) + abs(row1[-1] - row2[0])
		result = min(left1 + left2 + right1 + right2, excep1, excep2, excep3, excep4, excep5, excep6)
		print(result)
solve_problem()


t = int(input())
for _ in range(t):
	n = int(input())
	a = [*map(int, input().split())]
	b = [*map(int, input().split())]
	x = min((abs(B - a[0]) for B in b))
	y = min((abs(B - a[-1]) for B in b))
	z = min((abs(A - b[0]) for A in a))
	t = min((abs(A - b[-1]) for A in a))
	print(min(x + y + z + t, abs(a[0] - b[0]) + abs(a[n - 1] - b[n - 1]), abs(a[0] - b[0]) + y + t, abs(a[n - 1] - b[n - 1]) + x + z, abs(a[0] - b[n - 1]) + abs(b[0] - a[n - 1]), abs(a[0] - b[n - 1]) + y + z, abs(b[0] - a[n - 1]) + x + t))


def solve_problem():
	n = int(input())
	for _ in range(n):
		_ = input()
		row1 = [int(i) for i in input().split()]
		row2 = [int(i) for i in input().split()]
		left1 = min((abs(row1[0] - j) for j in row2))
		left2 = min((abs(row2[0] - j) for j in row1))
		right1 = min((abs(row1[-1] - j) for j in row2))
		right2 = min((abs(row2[-1] - j) for j in row1))
		tmp1 = abs(row1[0] - row2[0])
		tmp2 = abs(row1[-1] - row2[-1])
		tmp3 = abs(row1[0] - row2[-1])
		tmp4 = abs(row1[-1] - row2[0])
		tmp5 = left1 + left2
		tmp6 = right1 + right2
		excep1 = tmp1 + tmp2
		excep2 = tmp1 + tmp6
		excep3 = tmp3 + left2 + right1
		excep4 = tmp2 + tmp5
		excep5 = tmp4 + left1 + right2
		excep6 = tmp3 + tmp4
		result = min(tmp5 + tmp6, excep1, excep2, excep3, excep4, excep5, excep6)
		print(result)
solve_problem()


import heapq as hq
import math
from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	best = abs(a[0] - b[0]) + abs(a[-1] - b[-1])
	best = min(best, abs(a[-1] - b[0]) + abs(a[0] - b[-1]))
	a0c = min([abs(x - a[0]) for x in b])
	anc = min([abs(x - a[-1]) for x in b])
	b0c = min([abs(x - b[0]) for x in a])
	bnc = min([abs(x - b[-1]) for x in a])
	best = min(a0c + anc + b0c + bnc, best)
	best = min(a0c + b0c + abs(a[-1] - b[-1]), best)
	best = min(anc + bnc + abs(a[0] - b[0]), best)
	best = min(a0c + bnc + abs(a[-1] - b[0]), best)
	best = min(anc + b0c + abs(a[0] - b[-1]), best)
	print(best)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	s00 = abs(a[0] - b[0])
	s11 = abs(a[-1] - b[-1])
	s01 = abs(a[0] - b[-1])
	s10 = abs(a[-1] - b[0])
	sa0 = min((abs(i - b[0]) for i in a))
	sa1 = min((abs(i - b[-1]) for i in a))
	s0b = min((abs(a[0] - i) for i in b))
	s1b = min((abs(a[-1] - i) for i in b))
	ans = s00 + s11
	ans = min(ans, s01 + s10)
	ans = min(ans, s00 + sa1 + s1b)
	ans = min(ans, s11 + sa0 + s0b)
	ans = min(ans, s01 + s1b + sa0)
	ans = min(ans, s10 + s0b + sa1)
	ans = min(ans, s0b + s1b + sa0 + sa1)
	print(ans)


for i in [0] * int(input()):
	n = int(input())
	a = [*map(int, input().split())]
	b = [*map(int, input().split())]
	x = min((abs(a[0] - i) for i in b))
	y = min((abs(a[n - 1] - b[i]) for i in range(n - 1, -1, -1)))
	z = min((abs(b[0] - i) for i in a))
	t = min((abs(b[n - 1] - a[i]) for i in range(n - 1, -1, -1)))
	print(min(x + y + z + t, abs(a[0] - b[0]) + abs(a[n - 1] - b[n - 1]), abs(a[0] - b[0]) + y + t, abs(a[n - 1] - b[n - 1]) + x + z, abs(a[0] - b[n - 1]) + abs(b[0] - a[n - 1]), abs(a[0] - b[n - 1]) + y + z, abs(b[0] - a[n - 1]) + x + t))


INF = 10 ** 9 + 1

def main_tc():
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	min_a_left = INF
	min_a_right = INF
	min_b_left = INF
	min_b_right = INF
	for elem in b:
		min_a_left = min(min_a_left, abs(a[0] - elem))
		min_a_right = min(min_a_right, abs(a[-1] - elem))
	for elem in a:
		min_b_left = min(min_b_left, abs(b[0] - elem))
		min_b_right = min(min_b_right, abs(b[-1] - elem))
	answer = 4 * INF
	answer = min(answer, abs(a[0] - b[0]) + abs(a[-1] - b[-1]))
	answer = min(answer, abs(a[0] - b[-1]) + abs(a[-1] - b[0]))
	answer = min(answer, abs(a[0] - b[0]) + min_a_right + min_b_right)
	answer = min(answer, abs(a[0] - b[-1]) + min_a_right + min_b_left)
	answer = min(answer, abs(a[-1] - b[0]) + min_a_left + min_b_right)
	answer = min(answer, abs(a[-1] - b[-1]) + min_a_left + min_b_left)
	answer = min(answer, min_a_left + min_a_right + min_b_left + min_b_right)
	print(answer)

def main():
	tc = int(input())
	for _ in range(tc):
		main_tc()
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	z1 = abs(a[-1] - b[0])
	z2 = abs(a[0] - b[-1])
	a1 = abs(a[-1] - b[-1])
	a2 = abs(a[0] - b[0])
	m3 = m1 = a1
	m4 = m2 = a2
	for i in range(n):
		d1 = abs(a[i] - b[-1])
		if d1 < m1:
			m1 = d1
		d2 = abs(a[i] - b[0])
		if d2 < m2:
			m2 = d2
		d3 = abs(b[i] - a[-1])
		if d3 < m3:
			m3 = d3
		d4 = abs(b[i] - a[0])
		if d4 < m4:
			m4 = d4
	print(min(m1 + m2 + m3 + m4, a1 + a2, a1 + m2 + m4, a2 + m1 + m3, z1 + z2, z1 + m1 + m4, z2 + m2 + m3))


def solve_problem():
	for _ in range(int(input())):
		_ = input()
		row1 = [int(i) for i in input().split()]
		row2 = [int(i) for i in input().split()]
		r2 = range(0, len(row2))
		r1 = range(0, len(row1))
		left1 = min((abs(row1[0] - row2[j]) for j in r2))
		left2 = min((abs(row2[0] - row1[j]) for j in r1))
		right1 = min((abs(row1[-1] - row2[j]) for j in r2))
		right2 = min((abs(row2[-1] - row1[j]) for j in r1))
		excep1 = abs(row1[0] - row2[0]) + abs(row1[-1] - row2[-1])
		excep2 = abs(row1[0] - row2[0]) + right1 + right2
		excep3 = abs(row1[0] - row2[-1]) + left2 + right1
		excep4 = abs(row1[-1] - row2[-1]) + left1 + left2
		excep5 = abs(row1[-1] - row2[0]) + left1 + right2
		excep6 = abs(row1[0] - row2[-1]) + abs(row1[-1] - row2[0])
		result = min(left1 + left2 + right1 + right2, excep1, excep2, excep3, excep4, excep5, excep6)
		print(result)
solve_problem()


def find_smallest(va, vb, id):
	ret = 1000000000
	for b in vb:
		n = abs(va[id] - b)
		if n < ret:
			ret = n
	return ret
for i in range(int(input())):
	n = int(input())
	a = []
	b = []
	line = input().split()
	for j in range(n):
		a.append(int(line[j]))
	line = input().split()
	for j in range(n):
		b.append(int(line[j]))
	tl = find_smallest(a, b, 0)
	tr = find_smallest(a, b, n - 1)
	bl = find_smallest(b, a, 0)
	br = find_smallest(b, a, n - 1)
	tlbr_diag = abs(a[0] - b[n - 1])
	trbl_diag = abs(b[0] - a[n - 1])
	cycle = abs(a[0] - b[0]) + abs(a[n - 1] - b[n - 1])
	cycle_l = abs(a[0] - b[0]) + tr + br
	cycle_r = abs(a[n - 1] - b[n - 1]) + tl + bl
	accum = min(tl + br, tlbr_diag) + min(tr + bl, trbl_diag)
	print(min(min(cycle, accum), min(cycle_r, cycle_l)))


def minfault(a, b):
	ans = min(abs(a[0] - b[0]) + abs(a[-1] - b[-1]), abs(a[0] - b[-1]) + abs(a[-1] - b[0]))
	n = len(a)
	res = []
	for num in [a[0], a[-1]]:
		tmp = float('inf')
		for bi in b:
			tmp = min(tmp, abs(num - bi))
		res.append(tmp)
	for num in [b[0], b[-1]]:
		tmp = float('inf')
		for ai in a:
			tmp = min(tmp, abs(num - ai))
		res.append(tmp)
	ans = min(ans, abs(a[0] - b[0]) + res[1] + res[3], abs(a[0] - b[-1]) + res[1] + res[2], abs(a[-1] - b[0]) + res[0] + res[3], abs(a[-1] - b[-1]) + res[0] + res[2], sum(res))
	return ans
t = int(input())
for _ in range(t):
	_ = input()
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	print(minfault(a, b))


for _ in range(int(input())):
	n = int(input())
	l1 = list(map(int, input().strip().split()))
	l2 = list(map(int, input().strip().split()))
	lans = min(abs(l1[0] - l2[0]) + abs(l1[-1] - l2[-1]), abs(l1[0] - l2[-1]) + abs(l1[-1] - l2[0]))
	p1 = abs(l1[0] - l2[0])
	p2 = abs(l1[-1] - l2[-1])
	p3 = abs(l1[0] - l2[-1])
	p4 = abs(l1[-1] - l2[0])
	lans = min(p1 + p2, p3 + p4)
	(la1, la2, lb1, lb2) = (p1, p2, p3, p4)
	for i in range(n):
		k1 = abs(l2[0] - l1[i])
		k2 = abs(l2[-1] - l1[i])
		k3 = abs(l1[0] - l2[i])
		k4 = abs(l1[-1] - l2[i])
		if k1 < la1:
			la1 = k1
		if k2 < la2:
			la2 = k2
		if k3 < lb1:
			lb1 = k3
		if k4 < lb2:
			lb2 = k4
	print(min(lans, la1 + la2 + lb1 + lb2, p1 + la2 + lb2, p2 + la1 + lb1, p3 + la1 + lb2, p4 + la2 + lb1))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	mna0 = 10 ** 9
	mnan = 10 ** 9
	mnb0 = 10 ** 9
	mnbn = 10 ** 9
	for i in range(n):
		if abs(a[0] - b[i]) < mna0:
			mna0 = abs(a[0] - b[i])
		if abs(a[n - 1] - b[i]) < mnan:
			mnan = abs(a[n - 1] - b[i])
		if abs(b[0] - a[i]) < mnb0:
			mnb0 = abs(b[0] - a[i])
		if abs(b[n - 1] - a[i]) < mnbn:
			mnbn = abs(b[n - 1] - a[i])
	c = min([mna0 + mnan + mnb0 + mnbn, abs(a[0] - b[0]) + mnan + mnbn, abs(a[0] - b[n - 1]) + mnb0 + mnan, abs(a[n - 1] - b[0]) + mna0 + mnbn, abs(a[n - 1] - b[n - 1]) + mna0 + mnb0])
	c = min([c, abs(a[0] - b[0]) + abs(a[n - 1] - b[n - 1]), abs(a[0] - b[n - 1]) + abs(a[n - 1] - b[0])])
	print(c)


def fun(a, b):
	start = abs(a[0] - b[0])
	end = abs(a[-1] - b[-1])
	s_c1 = min((abs(i - a[0]) for i in b))
	s_c2 = min((abs(i - b[0]) for i in a))
	e_c1 = min((abs(i - a[-1]) for i in b))
	e_c2 = min((abs(i - b[-1]) for i in a))
	return min(start, s_c1 + s_c2) + min(end, e_c1 + e_c2)
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	print(min(fun(A, B), fun(A, B[::-1])))


from math import inf
T = int(input())
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	(p, q, r, s) = (float('inf'), float('inf'), float('inf'), float('inf'))
	for i in range(n):
		if abs(b[i] - a[0]) < p:
			p = abs(b[i] - a[0])
		if abs(a[i] - b[0]) < r:
			r = abs(a[i] - b[0])
		if abs(b[i] - a[-1]) < q:
			q = abs(b[i] - a[-1])
		if abs(a[i] - b[-1]) < s:
			s = abs(a[i] - b[-1])
	print(min(abs(a[0] - b[0]) + abs(a[-1] - b[-1]), abs(a[0] - b[0]) + q + s, abs(a[-1] - b[-1]) + p + r, p + q + r + s, abs(b[0] - a[-1]) + p + s, abs(a[0] - b[-1]) + q + r, abs(a[0] - b[-1]) + abs(a[-1] - b[0])))


def findmin(A1, B):
	ans = abs(A1 - B[0])
	for i in B:
		ans = min(ans, abs(A1 - i))
	return ans

def join(j1, j2, l1, l2):
	ans = abs(j1[0] - j2[0])
	extra = min(abs(l1[0] - l2[0]), l1[1] + l2[1])
	ans += extra
	return ans
for iters in range(int(input())):
	n = int(input())
	A = [int(i) for i in input().split(' ')]
	B = [int(i) for i in input().split(' ')]
	if n == 1:
		print(abs(A[0] - B[0]))
		continue
	A1 = [A[0], findmin(A[0], B)]
	A2 = [A[-1], findmin(A[-1], B)]
	B1 = [B[0], findmin(B[0], A)]
	B2 = [B[-1], findmin(B[-1], A)]
	ans = A1[1] + A2[1] + B1[1] + B2[1]
	ans = min(ans, join(A1, B1, A2, B2))
	ans = min(ans, join(A1, B2, A2, B1))
	ans = min(ans, join(A2, B1, A1, B2))
	ans = min(ans, join(A2, B2, A1, B1))
	print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a1 = a[0]
	a2 = a[-1]
	b1 = b[0]
	b2 = b[-1]
	y1 = 10000000000
	y2 = 10000000000
	y3 = abs(a[0] - b[0])
	y4 = abs(a[-1] - b[0])
	y5 = abs(a[0] - b[-1])
	y6 = abs(a[-1] - b[-1])
	x1 = 10000000000
	x2 = 10000000000
	for i in a:
		y1 = min(y1, abs(i - b1))
		y2 = min(y2, abs(i - b2))
	for i in b:
		x1 = min(x1, abs(i - a1))
		x2 = min(x2, abs(i - a2))
	print(min(y3 + y6, y4 + y5, y3 + y2 + x2, y4 + x1 + y2, y5 + x2 + y1, y6 + x1 + y1, x1 + x2 + y1 + y2))


def md(a, b):
	mn = 999999999
	for el in b:
		if abs(el - a) < mn:
			mn = abs(el - a)
	return mn
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	f = abs(a[0] - b[0]) + abs(a[-1] - b[-1])
	p = abs(md(a[0], b[1:])) + abs(md(b[0], a[1:])) + abs(md(a[-1], b[:n - 1])) + abs(md(b[-1], a[:n - 1]))
	q = abs(a[0] - b[0]) + abs(md(a[-1], b[:n - 1])) + abs(md(b[-1], a[:n - 1]))
	r = abs(a[-1] - b[-1]) + abs(md(a[0], b[1:])) + abs(md(b[0], a[1:]))
	w = abs(a[0] - b[-1]) + abs(a[-1] - b[0])
	a1 = abs(a[0] - b[-1]) + abs(md(b[0], a[1:])) + abs(md(a[-1], b[:n - 1]))
	b1 = abs(a[-1] - b[0]) + abs(md(a[0], b[1:])) + abs(md(b[-1], a[:n - 1]))
	print(min(f, p, q, r, w, a1, b1))


def calculate(arr_a, arr_b):
	out_a_1 = min((abs(x - arr_a[0]) for x in arr_b))
	out_a_2 = min((abs(x - arr_b[0]) for x in arr_a))
	out_b_1 = min((abs(x - arr_a[-1]) for x in arr_b))
	out_b_2 = min((abs(x - arr_b[-1]) for x in arr_a))
	return min(abs(arr_a[0] - arr_b[0]), out_a_1 + out_a_2) + min(abs(arr_a[-1] - arr_b[-1]), out_b_1 + out_b_2)
_ = int(input())
for __ in range(_):
	n = int(input())
	arr_1 = list(map(int, input().split()))
	arr_2 = list(map(int, input().split()))
	print(min(calculate(arr_1, arr_2), calculate(arr_1, arr_2[::-1])))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	first = list(map(int, input().split()))
	second = list(map(int, input().split()))
	l1 = abs(first[0] - second[0])
	l2 = abs(first[-1] - second[-1])
	l3 = abs(first[0] - second[-1])
	l4 = abs(first[-1] - second[0])
	l5 = abs(first[0] - second[0])
	l6 = abs(second[0] - first[0])
	l7 = abs(first[-1] - second[0])
	l8 = abs(second[-1] - first[0])
	for i in range(1, n):
		l5 = min(l5, abs(first[0] - second[i]))
		l6 = min(l6, abs(second[0] - first[i]))
		l7 = min(l7, abs(first[-1] - second[i]))
		l8 = min(l8, abs(second[-1] - first[i]))
	print(min(l1 + l2, l3 + l4, l1 + l7 + l8, l2 + l5 + l6, l3 + l6 + l7, l4 + l5 + l8, l5 + l6 + l7 + l8))


R = lambda : [*map(int, input().split())]
for _ in [0] * R()[0]:
	R()
	a = R()
	b = R()
	print(min((sum((min(abs(x[0] - y[0]), sum((min((abs(u[0] - z) for z in v)) for (u, v) in ((x, y), (y, x))))) for (x, y) in ((c, d), (c[::-1], d[::-1])))) for (c, d) in ((a, b), (a, b[::-1])))))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	m1 = 10 ** 9
	m2 = 10 ** 9
	p = a[0]
	q = a[-1]
	for i in range(len(b)):
		m1 = min(m1, abs(p - b[i]))
		m2 = min(m2, abs(q - b[i]))
	m3 = 10 ** 9
	m4 = 10 ** 9
	r = b[0]
	s = b[-1]
	for i in range(len(b)):
		m3 = min(m3, abs(r - a[i]))
		m4 = min(m4, abs(s - a[i]))
	ans1 = m1 + m2 + m3 + m4
	ans2 = min(abs(p - r) + abs(q - s), abs(p - s) + abs(q - r))
	ans3 = min(abs(p - r) + m2 + m4, abs(q - s) + m1 + m3, abs(p - s) + m2 + m3, abs(q - r) + m1 + m4)
	print(min(ans1, ans2, ans3))


import sys
input = sys.stdin.readline

def yes():
	print('YES')

def no():
	print('NO')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = 10 ** 10
	ans = min(ans, abs(a[0] - b[0]) + abs(a[-1] - b[-1]))
	ans = min(ans, abs(a[0] - b[-1]) + abs(a[-1] - b[0]))
	tmp1 = 10 ** 10
	tmp2 = 10 ** 10
	tmp3 = 10 ** 10
	tmp4 = 10 ** 10
	for i in range(n):
		tmp1 = min(tmp1, abs(b[0] - a[i]))
		tmp2 = min(tmp2, abs(a[0] - b[i]))
		tmp3 = min(tmp3, abs(b[-1] - a[i]))
		tmp4 = min(tmp4, abs(a[-1] - b[i]))
	ans = min(ans, tmp1 + tmp2 + tmp3 + tmp4)
	ans = min(ans, abs(a[-1] - b[0]) + tmp2 + tmp3)
	ans = min(ans, abs(a[0] - b[-1]) + tmp1 + tmp4)
	ans = min(ans, abs(a[0] - b[0]) + tmp3 + tmp4)
	ans = min(ans, abs(a[-1] - b[-1]) + tmp1 + tmp2)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c1 = abs(a[0] - b[0])
	c2 = abs(a[-1] - b[-1])
	(d1, d2, d3, d4) = [10 ** 18] * 4
	for i in range(n):
		d1 = min(d1, abs(b[i] - a[0]))
		d2 = min(d2, abs(a[-1] - b[i]))
		d3 = min(d3, abs(b[0] - a[i]))
		d4 = min(d4, abs(b[-1] - a[i]))
	c1 = min(c1, d1 + d3)
	c2 = min(c2, d2 + d4)
	f1 = abs(a[0] - b[-1])
	f2 = abs(a[-1] - b[0])
	ans = c1 + c2
	f2 = min(f2, d2 + d3)
	f1 = min(f1, d1 + d4)
	ans = min(ans, f1 + f2)
	print(ans)


import sys
from math import *
inf = 10 ** 9
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	k1 = abs(a[0] - b[0])
	k2 = abs(a[-1] - b[-1])
	k3 = abs(a[0] - b[-1])
	k4 = abs(a[-1] - b[0])
	ans1 = ans2 = ans3 = ans4 = 10 ** 9
	for i in range(n):
		ans1 = min(ans1, abs(a[0] - b[i]))
	for i in range(n):
		ans2 = min(ans2, abs(a[-1] - b[i]))
	for i in range(n):
		ans3 = min(ans3, abs(b[0] - a[i]))
	for i in range(n):
		ans4 = min(ans4, abs(b[-1] - a[i]))
	print(min(k1 + k2, k3 + k4, ans1 + ans3 + k2, ans2 + ans4 + k1, ans2 + ans3 + k3, ans1 + ans4 + k4, ans1 + ans2 + ans3 + ans4))


def find_best_index(ai, b):
	summr = 10 ** 30
	for j in range(len(b)):
		if abs(ai - b[j]) < summr:
			summr = abs(ai - b[j])
			index = j
	return index

def func(a, b, n):
	result = 10 ** 30
	List_1 = [0, find_best_index(a[0], b), n - 1]
	List_2 = [0, find_best_index(a[n - 1], b), n - 1]
	for i in List_1:
		for j in List_2:
			sum = abs(a[0] - b[i]) + abs(a[n - 1] - b[j])
			if i > 0 and j > 0:
				sum += abs(b[0] - a[find_best_index(b[0], a)])
			if i < n - 1 and j < n - 1:
				sum += abs(b[n - 1] - a[find_best_index(b[n - 1], a)])
			result = min(result, sum)
	return result
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	print(func(a, b, n))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	(p, q, r, s) = [10 ** 9] * 4
	for i in range(n):
		p = min(p, abs(a[0] - b[i]))
		q = min(q, abs(a[-1] - b[i]))
		r = min(r, abs(b[0] - a[i]))
		s = min(s, abs(b[-1] - a[i]))
	t = abs(a[0] - b[0])
	u = abs(a[-1] - b[-1])
	v = abs(a[0] - b[-1])
	w = abs(a[-1] - b[0])
	print(min(t + u, v + w, t + q + s, p + r + u, w + p + s, v + q + r, p + q + r + s))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a1 = 1000000000
	a2 = 1000000000
	a3 = 1000000000
	a4 = 1000000000
	for i in range(n):
		a1 = min(abs(a[0] - b[i]), a1)
		a2 = min(abs(a[n - 1] - b[i]), a2)
		a3 = min(abs(b[0] - a[i]), a3)
		a4 = min(abs(b[n - 1] - a[i]), a4)
	ans = min(abs(a[0] - b[0]), a1 + a3) + min(abs(a[n - 1] - b[n - 1]), a2 + a4)
	ans2 = min(abs(a[0] - b[n - 1]), a1 + a4) + min(abs(a[n - 1] - b[0]), a2 + a3)
	print(min(ans, ans2))


INF = pow(10, 9)

def best(arr, val):
	bst = INF + 10
	pos = -1
	for i in range(n):
		if bst > abs(val - arr[i]):
			bst = abs(val - arr[i])
			pos = i
	return pos
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	bst = INF * 10
	cds1 = [0, best(b, a[0]), n - 1]
	cds2 = [0, best(b, a[n - 1]), n - 1]
	for i in cds1:
		for j in cds2:
			ans = abs(a[0] - b[i]) + abs(a[n - 1] - b[j])
			if i > 0 and j > 0:
				ans += abs(b[0] - a[best(a, b[0])])
			if i < n - 1 and j < n - 1:
				ans += abs(b[n - 1] - a[best(a, b[n - 1])])
			bst = min(bst, ans)
	print(bst)


t = int(input())
for case in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	af = at = bf = bt = 0
	for i in range(n):
		if abs(a[0] - b[af]) > abs(a[0] - b[i]):
			af = i
		if abs(a[-1] - b[at]) > abs(a[-1] - b[i]):
			at = i
		if abs(b[0] - a[bf]) > abs(b[0] - a[i]):
			bf = i
		if abs(b[-1] - a[bt]) > abs(b[-1] - a[i]):
			bt = i
	ans = abs(a[0] - b[af]) + abs(a[-1] - b[at]) + abs(b[0] - a[bf]) + abs(b[-1] - a[bt])
	ans = min(ans, abs(a[0] - b[0]) + abs(a[-1] - b[-1]), abs(a[-1] - b[0]) + abs(b[-1] - a[0]))
	ans = min(ans, abs(a[0] - b[0]) + abs(a[-1] - b[at]) + abs(b[-1] - a[bt]))
	ans = min(ans, abs(a[-1] - b[-1]) + abs(a[0] - b[af]) + abs(b[0] - a[bf]))
	ans = min(ans, abs(a[-1] - b[0]) + abs(a[0] - b[af]) + abs(b[-1] - a[bt]))
	ans = min(ans, abs(b[-1] - a[0]) + abs(a[-1] - b[at]) + abs(b[0] - a[bf]))
	print(ans)


t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	mi_a_1 = 1e+18
	mi_a_n = 1e+18
	mi_b_1 = 1e+18
	mi_b_n = 1e+18
	for i in range(n):
		mi_a_1 = min(abs(a[0] - b[i]), mi_a_1)
		mi_a_n = min(abs(a[-1] - b[i]), mi_a_n)
		mi_b_1 = min(abs(b[0] - a[i]), mi_b_1)
		mi_b_n = min(abs(b[-1] - a[i]), mi_b_n)
	print(min(abs(a[0] - b[0]) + abs(a[-1] - b[-1]), abs(a[0] - b[0]) + mi_a_n + mi_b_n, mi_a_1 + mi_a_n + mi_b_1 + mi_b_n, abs(a[-1] - b[-1]) + mi_a_1 + mi_b_1, abs(a[0] - b[-1]) + abs(a[-1] - b[0]), abs(a[0] - b[-1]) + mi_a_n + mi_b_1, abs(a[-1] - b[0]) + mi_b_n + mi_a_1))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	bi = [float('inf'), float('inf')]
	ai = [float('inf'), float('inf')]
	for (i, k) in enumerate(a):
		bi[0] = min(bi[0], abs(k - b[0]))
		bi[1] = min(bi[1], abs(k - b[-1]))
	for (i, k) in enumerate(b):
		ai[0] = min(ai[0], abs(k - a[0]))
		ai[1] = min(ai[1], abs(k - a[-1]))
	res = min(sum(bi) + sum(ai), ai[0] + bi[0] + abs(a[-1] - b[-1]), ai[-1] + bi[-1] + abs(a[0] - b[0]), ai[0] + bi[-1] + abs(a[-1] - b[0]), ai[-1] + bi[0] + abs(a[0] - b[-1]), abs(a[0] - b[0]) + abs(a[-1] - b[-1]), abs(a[0] - b[-1]) + abs(a[-1] - b[0]))
	print(res)


t = eval(input())
for i in range(t):
	n = eval(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	p = []
	k = 1000000000000.0
	for i in range(n):
		k = min(abs(a[0] - b[i]), k)
	p.append(k)
	k = 1000000000000.0
	for i in range(n):
		k = min(abs(a[-1] - b[i]), k)
	p.append(k)
	k = 1000000000000.0
	for i in range(n):
		k = min(abs(a[i] - b[0]), k)
	p.append(k)
	k = 1000000000000.0
	for i in range(n):
		k = min(abs(a[i] - b[-1]), k)
	p.append(k)
	(la, lb, ra, rb) = (p[0], p[2], p[1], p[3])
	ll = abs(a[0] - b[0])
	lr = abs(a[0] - b[n - 1])
	rl = abs(a[n - 1] - b[0])
	rr = abs(a[n - 1] - b[n - 1])
	ans = min(la + lb + ra + rb, ll + rr, lr + rl, ll + ra + rb, lr + ra + lb, rl + la + rb, rr + la + lb)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = (*map(int, input().split()),)
	b = (*map(int, input().split()),)
	ans1 = abs(a[0] - b[0]) + abs(a[-1] - b[-1])
	ans2 = abs(a[0] - b[-1]) + abs(a[-1] - b[0])
	ans3 = abs(a[0] - b[0]) + min((abs(a[-1] - b[i]) for i in range(n))) + min((abs(a[i] - b[-1]) for i in range(n)))
	ans4 = abs(a[-1] - b[-1]) + min((abs(a[0] - b[i]) for i in range(n))) + min((abs(a[i] - b[0]) for i in range(n)))
	ans5 = abs(a[0] - b[-1]) + min((abs(a[-1] - b[i]) for i in range(n))) + min((abs(a[i] - b[0]) for i in range(n)))
	ans6 = abs(a[-1] - b[0]) + min((abs(a[0] - b[i]) for i in range(n))) + min((abs(a[i] - b[-1]) for i in range(n)))
	ans7 = min((abs(a[0] - b[i]) for i in range(n))) + min((abs(a[i] - b[0]) for i in range(n))) + min((abs(a[-1] - b[i]) for i in range(n))) + min((abs(a[i] - b[-1]) for i in range(n)))
	print(min(ans1, ans2, ans3, ans4, ans5, ans6, ans7))


t = int(input())
while t:
	t = t - 1
	n = int(input())
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	a = abs(arr[0] - brr[n - 1])
	b = abs(arr[0] - brr[0])
	d = abs(brr[n - 1] - arr[n - 1])
	c = abs(brr[0] - arr[n - 1])
	ans = min(a + c, b + d)
	ans = min(ans, a + b + d)
	ans = min(ans, c + b + d)
	a0 = abs(arr[0] - brr[0])
	b0 = abs(brr[0] - arr[0])
	an = abs(arr[n - 1] - brr[n - 1])
	bn = abs(arr[n - 1] - brr[n - 1])
	for i in range(n):
		if abs(arr[0] - brr[i]) < a0:
			a0 = abs(arr[0] - brr[i])
		if abs(arr[n - 1] - brr[i]) < an:
			an = abs(arr[n - 1] - brr[i])
		if abs(brr[0] - arr[i]) < b0:
			b0 = abs(brr[0] - arr[i])
		if abs(brr[n - 1] - arr[i]) < bn:
			bn = abs(brr[n - 1] - arr[i])
		ans = min(ans, b + an + bn)
		ans = min(ans, a + b0 + an)
		ans = min(ans, c + a0 + bn)
		ans = min(ans, a0 + d + b0)
		ans = min(ans, a0 + b0 + an + bn)
	print(ans)