You are given two arrays of length $n$: $a_1, a_2, \dots, a_n$ and $b_1, b_2, \dots, b_n$.

You can perform the following operation any number of times:

Choose integer index $i$ ($1 \le i \le n$);

Swap $a_i$ and $b_i$.

What is the minimum possible sum $|a_1 - a_2| + |a_2 - a_3| + \dots + |a_{n-1} - a_n|$ $+$ $|b_1 - b_2| + |b_2 - b_3| + \dots + |b_{n-1} - b_n|$ (in other words, $\sum\limits_{i=1}^{n - 1}{\left(|a_i - a_{i+1}| + |b_i - b_{i+1}|\right)}$) you can achieve after performing several (possibly, zero) operations?


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 4000$) — the number of test cases. Then, $t$ test cases follow.

The first line of each test case contains the single integer $n$ ($2 \le n \le 25$) — the length of arrays $a$ and $b$.

The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array $a$.

The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the array $b$.


-----Output-----

For each test case, print one integer — the minimum possible sum $\sum\limits_{i=1}^{n-1}{\left(|a_i - a_{i+1}| + |b_i - b_{i+1}|\right)}$.


-----Examples-----

Input
3
4
3 3 10 10
10 10 3 3
5
1 2 3 4 5
6 7 8 9 10
6
72 101 108 108 111 44
10 87 111 114 108 100
Output
0
8
218


-----Note-----

In the first test case, we can, for example, swap $a_3$ with $b_3$ and $a_4$ with $b_4$. We'll get arrays $a = [3, 3, 3, 3]$ and $b = [10, 10, 10, 10]$ with sum $3 \cdot |3 - 3| + 3 \cdot |10 - 10| = 0$.

In the second test case, arrays already have minimum sum (described above) equal to $|1 - 2| + \dots + |4 - 5| + |6 - 7| + \dots + |9 - 10|$ $= 4 + 4 = 8$.

In the third test case, we can, for example, swap $a_5$ and $b_5$.

Difficulty: EASY
Tags: Mathematics

Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
def q(n, a, b):
	for i in range(n):
		if b[i] < a[i]:
			(b[i], a[i]) = (a[i], b[i])
	w = 0
	for i in range(n - 1):
		w += abs(a[i] - a[i + 1]) + abs(b[i] - b[i + 1])
	return w
t = int(input())
w = []
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	w.append(q(n, a, b))
print('\n'.join(map(str, w)))


import math
import sys
from collections import deque
import heapq
from collections import Counter

def inp():
	return sys.stdin.readline().rstrip()

def mpp():
	return map(int, inp().split())

def lis():
	return list(mpp())

def sol(a, b, n):
	for i in range(n):
		if a[i] < b[i]:
			(a[i], b[i]) = (b[i], a[i])
	sum = 0
	for i in range(1, n):
		sum += abs(a[i] - a[i - 1]) + abs(b[i] - b[i - 1])
	return sum

def main():
	t = int(inp())
	for _ in range(t):
		n = int(inp())
		a = lis()
		b = lis()
		print(sol(a, b, n))
main()


def swap(a, b, l):
	for i in range(l):
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
	return (a, b)

def minsum(a, b, l):
	av = 0
	bv = 0
	for i in range(0, l - 1):
		av += abs(a[i] - a[i + 1])
		bv += abs(b[i] - b[i + 1])
	s = abs(av + bv)
	return s
tc = int(input())
fa = []
for i in range(tc):
	l = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	(x, y) = swap(a, b, l)
	r = minsum(x, y, l)
	fa.append(r)
for i in fa:
	print(i)


import sys
t = int(sys.stdin.readline())
for _ in range(0, t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().strip().split()))
	b = list(map(int, sys.stdin.readline().strip().split()))
	for i in range(0, n):
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
	sum = 0
	for i in range(0, n - 1):
		sum = sum + abs(a[i] - a[i + 1]) + abs(b[i] - b[i + 1])
	print(sum)


def slve():
	n = int(input())
	(a, b) = ([], [])
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	sum = 0
	if a[0] < b[0]:
		temp = a[0]
		a[0] = b[0]
		b[0] = temp
	for i in range(1, n):
		if a[i] < b[i]:
			temp = a[i]
			a[i] = b[i]
			b[i] = temp
		sum = sum + abs(a[i - 1] - a[i]) + abs(b[i - 1] - b[i])
	print(sum)
t = int(input())
while t > 0:
	slve()
	t = t - 1


import sys
input = sys.stdin.readline

def ii():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
for _ in range(ii()):
	_ = ii()
	ls1 = inlt()
	ls2 = inlt()
	for i in range(len(ls1)):
		if ls1[i] > ls2[i]:
			(ls1[i], ls2[i]) = (ls2[i], ls1[i])
	print(sum((abs(ls1[i] - ls1[i + 1]) for i in range(len(ls1) - 1))) + sum((abs(ls2[i] - ls2[i + 1]) for i in range(len(ls1) - 1))))


from sys import stdin
from math import log, floor, ceil, gcd
from collections import defaultdict as dd
input = stdin.readline
inp = lambda : int(stdin.readline())
rs = lambda : stdin.readline().strip()

def ra(typ):
	return list(map(typ, stdin.readline().split()))

def rv(typ):
	return map(typ, stdin.readline().split())

def main():
	for _ in range(inp()):
		n = inp()
		a = ra(int)
		b = ra(int)
		x = 0
		for i in range(n):
			if a[i] > b[i]:
				(a[i], b[i]) = (b[i], a[i])
		for i in range(n - 1):
			x += abs(a[i] - a[i + 1])
			x += abs(b[i] - b[i + 1])
		print(x)
main()


def function(testCases):
	while testCases != 0:
		n = int(input())
		list1 = list(map(int, input().split(' ')))
		list2 = list(map(int, input().split(' ')))
		for i in range(n):
			if list1[i] > list2[i]:
				(list1[i], list2[i]) = (list2[i], list1[i])
		sum1 = 0
		sum2 = 0
		for j in range(n):
			try:
				sum1 += abs(list1[j] - list1[j + 1])
				sum2 += abs(list2[j] - list2[j + 1])
			except:
				pass
		print(sum1 + sum2)
		testCases -= 1
function(int(input()))


import math
rrr = int(input())
rr = []
for j in range(rrr):
	k = int(input())
	lst = list(map(int, input().split()))
	lst1 = list(map(int, input().split()))
	lst3 = []
	s = 0
	for i in range(k):
		if lst1[i] < lst[i]:
			(lst[i], lst1[i]) = (lst1[i], lst[i])
	for i in range(k - 1):
		s += abs(lst[i] - lst[i + 1])
		s += abs(lst1[i] - lst1[i + 1])
	rr.append(str(s))
print('\n'.join(rr))


R = lambda : map(int, input().split())
(t,) = R()
while t:
	t -= 1
	(n,) = R()
	print(sum((abs(y - x) for a in zip(*map(sorted, zip(R(), R()))) for (x, y) in zip(a, a[1:]))))


def solve(n):
	l1 = []
	l2 = []
	for i in range(n):
		if arr[i] <= arr2[i]:
			l1.append(arr[i])
			l2.append(arr2[i])
		else:
			l1.append(arr2[i])
			l2.append(arr[i])
	s = 0
	for i in range(n - 1):
		s += abs(l1[i] - l1[i + 1]) + abs(l2[i] - l2[i + 1])
	return s
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr2 = [int(x) for x in input().split()]
	print(solve(n))


import sys

def solve():
	inp = sys.stdin.readline
	n = int(inp())
	a = list(map(int, inp().split()))
	b = list(map(int, inp().split()))
	r = 0
	for i in range(1, n):
		x = abs(a[i] - a[i - 1]) + abs(b[i] - b[i - 1])
		r += min(x, abs(a[i] - b[i - 1]) + abs(b[i] - a[i - 1]))
	print(r)

def main():
	for i in range(int(sys.stdin.readline())):
		solve()
main()


t = int(input())
res = []
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(n):
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
	sum = 0
	for i in range(n - 1):
		sum += abs(a[i] - a[i + 1]) + abs(b[i] - b[i + 1])
	res.append(sum)
for el in res:
	print(el)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	(aa, bb) = (a[0], b[0])
	sum = 0
	for i in range(1, n):
		x = abs(a[i] - aa) + abs(b[i] - bb)
		y = abs(b[i] - aa) + abs(a[i] - bb)
		if x > y:
			sum += y
			(aa, bb) = (b[i], a[i])
		else:
			sum += x
			(aa, bb) = (a[i], b[i])
	print(sum)
t = int(input())
for i in range(int(t)):
	solve()


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	sum = 0
	for i in range(len(a)):
		if a[i] > b[i]:
			(b[i], a[i]) = (a[i], b[i])
	for i in range(len(a) - 1):
		sum += abs(a[i] - a[i + 1])
		sum += abs(b[i] - b[i + 1])
	print(sum)
tst = int(input())
for i in range(tst):
	solve()


import math
for i in range(int(input())):
	n = int(input())
	A = [int(k) for k in input().split()]
	B = [int(k) for k in input().split()]
	for k in range(n):
		if A[k] > B[k]:
			s = A[k]
			A[k] = B[k]
			B[k] = s
	S = 0
	for k in range(1, n):
		S += abs(A[k] - A[k - 1]) + abs(B[k] - B[k - 1])
	print(S)


def main():
	_ = int(input())
	xs = list(map(int, input().split()))
	ys = list(map(int, input().split()))
	res = 0
	(px, py) = (xs[0], ys[0])
	for (x, y) in zip(xs[1:], ys[1:]):
		res += min(abs(px - x) + abs(py - y), abs(px - y) + abs(py - x))
		(px, py) = (x, y)
	print(res)
t = int(input())
for _ in range(t):
	main()


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


for i in range(int(input())):
	r = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	g = 0
	for i in range(r):
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
	for i in range(r - 1):
		g += abs(a[i] - a[i + 1])
		g += abs(b[i] - b[i + 1])
	print(g)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	s = 0
	for j in range(n):
		if a[j] > b[j]:
			x = a[j]
			a[j] = b[j]
			b[j] = x
	for j in range(n - 1):
		s += abs(a[j] - a[j + 1]) + abs(b[j] - b[j + 1])
	print(s)


import math

def read_list():
	return list(map(int, input().split()))

def read_2_v():
	return map(int, input().split())

def main():
	n = int(input())
	l1 = read_list()
	l2 = read_list()
	for i in range(n):
		if l1[i] > l2[i]:
			t = l1[i]
			l1[i] = l2[i]
			l2[i] = t
	sum_ = 0
	for i in range(n - 1):
		asum = abs(l1[i] - l1[i + 1])
		bsum = abs(l2[i] - l2[i + 1])
		sum_ += asum + bsum
	print(sum_)
t = int(input())
while t > 0:
	t -= 1
	main()


def solvecff():
	n = int(input())
	lis = [int(i) for i in input().split()]
	lis2 = [int(i) for i in input().split()]
	for i in range(n):
		if lis[i] > lis2[i]:
			(lis[i], lis2[i]) = (lis2[i], lis[i])
	ans = 0
	for i in range(len(lis) - 1):
		ans += abs(lis[i] - lis[i + 1])
	for i in range(len(lis2) - 1):
		ans += abs(lis2[i] - lis2[i + 1])
	print(ans)
tt = int(input())
while tt:
	tt -= 1
	solvecff()


from sys import stdin, stdout
from collections import Counter, defaultdict
input = stdin.readline
t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	b = list(map(int, input().strip().split()))
	for i in range(n):
		if a[i] > b[i]:
			temp = a[i]
			a[i] = b[i]
			b[i] = temp
	ans = 0
	for i in range(1, n):
		ans += abs(a[i] - a[i - 1])
	for i in range(1, n):
		ans += abs(b[i] - b[i - 1])
	print(ans)


for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	c = list(map(int, input().split()))
	for j in range(a):
		if b[j] > c[j]:
			t = b[j]
			b[j] = c[j]
			c[j] = t
	s = 0
	d = 0
	for i in range(a - 1):
		s += abs(b[i + 1] - b[i])
		d += abs(c[i + 1] - c[i])
	print(s + d)


for _ in range(int(input())):
	_ = input()
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(len(a)):
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
	print(sum((abs(a[i] - a[i + 1]) + abs(b[i] - b[i + 1]) for i in range(0, len(a) - 1))))


def var(a, b):
	for i in range(len(a)):
		if a[i] > b[i]:
			h = a[i]
			a[i] = b[i]
			b[i] = h
	su = 0
	sm = 0
	for i in range(len(a) - 1):
		su = su + abs(a[i + 1] - a[i])
		sm = sm + abs(b[i + 1] - b[i])
	print(su + sm)
m = int(input())
for i in range(m):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	var(a, b)


T = int(input())
ans = []
for t in range(T):
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	for i in range(n):
		if l1[i] > l2[i]:
			(l1[i], l2[i]) = (l2[i], l1[i])
	s = 0
	for i in range(n - 1):
		s += abs(l1[i] - l1[i + 1]) + abs(l2[i] - l2[i + 1])
	ans.append(s)
for t in range(T):
	print(ans[t])


def voraz(A, B, N):
	sum = 0
	for i in range(1, N):
		difA = abs(A[i] - A[i - 1])
		difB = abs(B[i] - B[i - 1])
		difC1 = abs(A[i] - B[i - 1])
		difC2 = abs(B[i] - A[i - 1])
		sum += min(difA + difB, difC1 + difC2)
	return sum
T = int(input())
for t in range(T):
	N = int(input())
	A = [int(elem) for elem in input().split(' ')]
	B = [int(elem) for elem in input().split(' ')]
	print(voraz(A, B, N))


t = int(input())

def solve():
	size = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	min_dif = 0
	for i in range(1, len(a)):
		first_a_dif = abs(a[i] - a[i - 1])
		first_b_dif = abs(b[i] - b[i - 1])
		sum_first = first_a_dif + first_b_dif
		second_a_dif = abs(b[i] - a[i - 1])
		second_b_dif = abs(a[i] - b[i - 1])
		sum_second = second_a_dif + second_b_dif
		min_dif += sum_first if sum_first < sum_second else sum_second
	print(min_dif)
for i in range(t):
	solve()


R = lambda : map(int, input().split())
(t,) = R()
while t:
	t -= 1
	(n,) = R()
	print(sum((abs(a[i] - a[i + 1]) for a in zip(*map(sorted, zip(R(), R()))) for i in range(n - 1))))


def Method(n, a, b):
	(i, j) = (a[0], b[0])
	res = 0
	for k in range(1, n):
		s1 = abs(a[k] - i) + abs(b[k] - j)
		z = 0
		hrj = 0
		s2 = abs(b[k] - i) + abs(a[k] - j)
		hbfhrbf = 0
		fejrf = 0
		if s1 <= s2:
			jvke = 0
			res += s1
			fhbfb = 0
			(i, j) = (a[k], b[k])
			fbefh = 0
		else:
			hhcbd = 0
			res += s2
			ekhbeh = 0
			(i, j) = (b[k], a[k])
			fjrfj = 0
	return res
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	res = Method(n, a, b)
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(n):
		if a[i] <= b[i]:
			continue
		else:
			(a[i], b[i]) = (b[i], a[i])
	count = 0
	for i in range(n - 1):
		count += abs(a[i + 1] - a[i]) + abs(b[i + 1] - b[i])
	print(count)


num = int(input())
for i in range(num):
	k = int(input())
	arr1 = input()
	arr2 = input()
	arr1 = arr1.split(' ')
	arr1 = list(map(int, arr1))
	arr2 = arr2.split(' ')
	arr2 = list(map(int, arr2))
	for m in range(k):
		if arr1[m] > arr2[m]:
			temp = arr1[m]
			arr1[m] = arr2[m]
			arr2[m] = temp
	sum = 0
	for j in range(k - 1):
		sum += abs(arr1[j + 1] - arr1[j]) + abs(arr2[j + 1] - arr2[j])
	print(sum)


for _ in range(int(input())):
	n = int(input())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	for i in range(n):
		if arr1[i] > arr2[i]:
			(arr1[i], arr2[i]) = (arr2[i], arr1[i])
	sm = 0
	for i in range(1, n):
		sm += abs(arr1[i] - arr1[i - 1]) + abs(arr2[i] - arr2[i - 1])
	print(sm)


for i in range(int(input())):
	sum = 0
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	for i in range(n):
		if l1[i] > l2[i]:
			(l1[i], l2[i]) = (l2[i], l1[i])
	for i in range(1, n):
		sum = sum + abs(l1[i - 1] - l1[i]) + abs(l2[i - 1] - l2[i])
	print(sum)


def solution(n, a, b):
	for i in range(n):
		if a[i] > b[i]:
			tmp = a[i]
			a[i] = b[i]
			b[i] = tmp
	summ = 0
	for i in range(n - 1):
		summ += abs(a[i] - a[i + 1])
		summ += abs(b[i] - b[i + 1])
	return summ
t = int(input())
while t:
	n = int(input())
	a = [int(x) for x in input().strip().split(' ')]
	b = [int(x) for x in input().strip().split(' ')]
	print(solution(n, a, b))
	t -= 1


result = ''
for r in range(0, int(input())):
	n = int(input())
	a = input()
	b = input()
	a = list(map(int, a.split()))
	b = list(map(int, b.split()))
	s = 0
	for i in range(0, n):
		if b[i] < a[i]:
			temp = a[i]
			a[i] = b[i]
			b[i] = temp
		if i == 0:
			continue
		else:
			if a[i - 1] > a[i]:
				s += a[i - 1] - a[i]
			else:
				s += a[i] - a[i - 1]
			if b[i - 1] > b[i]:
				s += b[i - 1] - b[i]
			else:
				s += b[i] - b[i - 1]
	result += str(s) + '\n'
print(result.rstrip('\n'))


T = int(input())
for _ in range(T):
	l = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(l):
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
	ans = 0
	for i in range(1, l):
		ans += abs(a[i] - a[i - 1]) + abs(b[i] - b[i - 1])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split(' ')))
	k = list(map(int, input().split(' ')))
	for i in range(len(l)):
		if l[i] > k[i]:
			h = l[i]
			l[i] = k[i]
			k[i] = h
	z = 0
	x = 0
	for i in range(len(l) - 1):
		z = z + abs(l[i + 1] - l[i])
		x = x + abs(k[i + 1] - k[i])
	print(z + x)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(n):
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
	s1 = 0
	s2 = 0
	for i in range(1, n):
		d1 = abs(a[i] - a[i - 1])
		d2 = abs(b[i] - b[i - 1])
		s1 += d1
		s2 += d2
	print(s1 + s2)


f = int(input())
for _ in range(f):
	n = int(input())
	mas = [int(i) for i in input().split()]
	mas2 = [int(i) for i in input().split()]
	count = 0
	for i in range(n):
		if mas[i] > mas2[i]:
			k = mas[i]
			mas[i] = mas2[i]
			mas2[i] = k
		if i > 0:
			count += abs(mas[i] - mas[i - 1])
			count += abs(mas2[i] - mas2[i - 1])
	print(count)


def getMin(a, b, c, d):
	return min(abs(a - b) + abs(c - d), abs(a - d) + abs(c - b))

def solve(a, b):
	ans = 0
	for i in range(1, len(a)):
		ans += getMin(a[i], a[i - 1], b[i], b[i - 1])
	return ans
for t in range(int(input())):
	[n] = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	print(solve(a, b))


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	for i in range(n):
		if A[i] > B[i]:
			(A[i], B[i]) = (B[i], A[i])
	res = 0
	for i in range(n - 1):
		res += abs(B[i] - B[i + 1]) + abs(A[i] - A[i + 1])
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = list(map(int, input().split()))
	for i in range(n):
		if l[i] > d[i]:
			(l[i], d[i]) = (d[i], l[i])
	cnt = 0
	for i in range(n - 1):
		cnt += abs(l[i] - l[i + 1])
		cnt += abs(d[i] - d[i + 1])
	print(cnt)


t = int(input())
for i in range(t):
	(sum1, sum2, sum3, sum4) = (0, 0, 0, 0)
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	for i in range(n):
		if a[i] < b[i]:
			(a[i], b[i]) = (b[i], a[i])
	for i in range(n - 1):
		sum1 += abs(a[i] - a[i + 1])
		sum2 += abs(b[i] - b[i + 1])
	print(sum1 + sum2)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(n):
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
	s = 0
	for i in range(n - 1):
		s += abs(a[i] - a[i + 1])
	for i in range(n - 1):
		s += abs(b[i] - b[i + 1])
	print(s)


for cases in range(int(input())):
	inp = lambda : list(map(int, input().split()))
	l = int(input())
	(a, b) = ([], [])
	a = inp()
	b = inp()
	c = list(map(sorted, tuple(zip(a, b))))
	print(sum([abs(x[0] - y[0]) + abs(x[1] - y[1]) for (x, y) in zip(c, c[1:])]))


k = int(input())
while k:
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	sum = 0
	for i in range(n):
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
	for i in range(n - 1):
		sum += abs(a[i] - a[i + 1])
		sum += abs(b[i] - b[i + 1])
	print(sum)
	k -= 1


n = int(input())
for i in range(n):
	l = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	(sum1, sum2) = (0, 0)
	for j in range(l):
		if a[j] < b[j]:
			(a[j], b[j]) = (b[j], a[j])
	for k in range(0, l - 1):
		sum1 += abs(a[k] - a[k + 1])
		sum2 += abs(b[k] - b[k + 1])
	print(sum1 + sum2)


import sys
import math
from sys import stdin, stdout

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(input())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def main():
	for t in range(get_int()):
		n = int(input())
		a = get_ints_in_list()
		b = get_ints_in_list()
		ans = 0
		for i in range(0, n - 1):
			diff1 = abs(a[i] - a[i + 1]) + abs(b[i] - b[i + 1])
			diff2 = abs(b[i] - a[i + 1]) + abs(a[i] - b[i + 1])
			if diff1 <= diff2:
				ans += diff1
			else:
				ans += diff2
				tp = a[i]
				a[i] = b[i]
				b[i] = tp
		print(ans)
main()


def solve(tc: dict):
	number_of_length = tc['length']
	list_a = tc['a']
	list_b = tc['b']
	total = 0
	for i in range(1, number_of_length):
		old_abs = abs(list_a[i - 1] - list_a[i]) + abs(list_b[i - 1] - list_b[i])
		new_abs = abs(list_a[i - 1] - list_b[i]) + abs(list_b[i - 1] - list_a[i])
		if old_abs > new_abs:
			temp = list_a[i]
			list_a[i] = list_b[i]
			list_b[i] = temp
			total += new_abs
		else:
			total += old_abs
	print(total)
testcases = []
for _ in range(int(input())):
	length = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	testcases.append({'a': a, 'b': b, 'length': length})
for testcase in testcases:
	solve(testcase)


n = int(input())

def convert_int(x, a):
	i = 0
	while i < a:
		b = int(x[i])
		x[i] = b
		i = i + 1
	return x

def ans(b, a):
	diff_sum = 0
	i = 1
	while i < a:
		diff_sum = diff_sum + abs(b[i] - b[i - 1])
		i = i + 1
	return diff_sum
for i in range(n):
	a = int(input())
	b = list(map(int, input().strip().split()))[:a]
	c = list(map(int, input().strip().split()))[:a]
	for j in range(a):
		if c[j] > b[j]:
			(c[j], b[j]) = (b[j], c[j])
	sum = ans(c, a) + ans(b, a)
	print(sum)


def balance(n, a, b):
	result = 0
	for i in range(1, n):
		sum1 = abs(a[i] - a[i - 1]) + abs(b[i] - b[i - 1])
		sum2 = abs(b[i] - a[i - 1]) + abs(a[i] - b[i - 1])
		if sum2 < sum1:
			(b[i], a[i]) = (a[i], b[i])
		result += min(sum1, sum2)
	return result
T = int(input())
for i in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(balance(n, a, b))


import fileinput

def getanswer(a, b):
	answer = 0
	for j in range(len(a) - 1):
		asum = abs(a[j] - a[j + 1])
		bsum = abs(b[j] - b[j + 1])
		asum_swapped = abs(a[j] - b[j + 1])
		bsum_swapped = abs(b[j] - a[j + 1])
		if asum_swapped + bsum_swapped < asum + bsum:
			answer += asum_swapped + bsum_swapped
		else:
			answer += asum + bsum
	return answer

def solve():
	ins = []
	for line in fileinput.input():
		ins.append(line.strip('\n'))
	num_tests = int(ins[0])
	test_cases = ins[1:]
	for i in range(num_tests):
		(a, b) = test_cases[i * 3 + 1:i * 3 + 3]
		a = [int(j) for j in a.split(' ')]
		b = [int(j) for j in b.split(' ')]
		print(getanswer(a, b))
solve()


def sumar(a, b):
	suma = 0
	for i in range(1, n):
		suma += abs(a[i] - a[i - 1]) + abs(b[i] - b[i - 1])
	return suma
t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(n):
		if a[i] > b[i]:
			aux = a[i]
			a[i] = b[i]
			b[i] = aux
	print(sumar(a, b))


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	sum = 0
	if a[0] < b[0]:
		(b[0], a[0]) = (a[0], b[0])
	for i in range(1, n):
		if a[i] < b[i]:
			(b[i], a[i]) = (a[i], b[i])
		sum += abs(a[i] - a[i - 1]) + abs(b[i] - b[i - 1])
	print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for k in range(len(a)):
		if a[k] > b[k]:
			el = a[k]
			a[k] = b[k]
			b[k] = el
	sum1 = 0
	for j in range(len(a) - 1):
		sum1 += abs(a[j] - a[j + 1])
	for j in range(len(b) - 1):
		sum1 += abs(b[j] - b[j + 1])
	print(sum1)


if True:
	tests_num = int(input())
	for _ in range(tests_num):
		points_num = int(input())
		a = list(map(int, input().split()))
		b = list(map(int, input().split()))
		if a[0] > b[0]:
			(a, b) = (b, a)
		a_ = a[0]
		b_ = b[0]
		s = 0
		for i in range(1, points_num):
			if a[i] > b[i]:
				s += abs(b[i] - a_) + abs(a[i] - b_)
				a_ = b[i]
				b_ = a[i]
			else:
				s += abs(b[i] - b_) + abs(a[i] - a_)
				a_ = a[i]
				b_ = b[i]
		print(s)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	ans = 0
	for i in range(n):
		if a[i] > b[i]:
			c = a[i]
			a[i] = b[i]
			b[i] = c
		if i > 0:
			ans += abs(a[i] - a[i - 1]) + abs(b[i] - b[i - 1])
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(n):
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
	sum1 = 0
	sum2 = 0
	for i in range(1, n):
		sum1 += abs(a[i] - a[i - 1])
		sum2 += abs(b[i] - b[i - 1])
	print(sum2 + sum1)


for t in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	b = [*map(int, input().split())]
	e = 0
	for (x1, x2, y1, y2) in zip(a, a[1:], b, b[1:]):
		e += min(abs(x1 - x2) + abs(y1 - y2), abs(x1 - y2) + abs(y1 - x2))
	print(e)


import collections
import math

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))
for _ in range(ii()):
	n = ii()
	a = li()
	b = li()
	for i in range(n):
		if a[i] < b[i]:
			(a[i], b[i]) = (b[i], a[i])
	ans = 0
	for i in range(n - 1):
		ans += abs(a[i] - a[i + 1]) + abs(b[i] - b[i + 1])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	for i in range(n):
		if arr1[i] > arr2[i]:
			(arr1[i], arr2[i]) = (arr2[i], arr1[i])
	arr = arr1 + arr2
	f = arr[0]
	res = 0
	for (index, i) in enumerate(arr[:n - 1]):
		res += abs(i - arr[index + 1])
	for i in range(n, 2 * n - 1):
		res += abs(arr[i] - arr[i + 1])
	print(res)


test = int(input())
for _ in range(0, test):
	n = int(input())
	arr1 = list(map(int, input().split(' ')))
	arr2 = list(map(int, input().split(' ')))
	for i in range(n):
		if arr1[i] > arr2[i]:
			(arr1[i], arr2[i]) = (arr2[i], arr1[i])
	sum1 = 0
	sum2 = 0
	for i in range(n - 1):
		sum1 += abs(arr1[i] - arr1[i + 1])
		sum2 += abs(arr2[i] - arr2[i + 1])
	print(sum1 + sum2)


t = int(input())
while t > 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	if a[0] > b[0]:
		for i in range(1, n):
			if b[i] > a[i]:
				(a[i], b[i]) = (b[i], a[i])
	else:
		for i in range(1, n):
			if b[i] < a[i]:
				(a[i], b[i]) = (b[i], a[i])
	sum = 0
	for i in range(n - 1):
		if a[i + 1] > a[i]:
			sum += a[i + 1] - a[i]
		else:
			sum += a[i] - a[i + 1]
	for i in range(n - 1):
		if b[i + 1] > b[i]:
			sum += b[i + 1] - b[i]
		else:
			sum += b[i] - b[i + 1]
	print(sum)
	t = t - 1


def abs(n):
	if n >= 0:
		return n
	return -n
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	b = [int(s) for s in input().split()]
	for i in range(n):
		if a[i] < b[i]:
			(a[i], b[i]) = (b[i], a[i])
	total = 0
	for i in range(1, n):
		total += abs(a[i] - a[i - 1]) + abs(b[i] - b[i - 1])
	print(total)


totalcasos = int(input())
for i in range(totalcasos):
	datosporcaso = int(input())
	arraya = list(map(int, input().split(' ')))
	arrayb = list(map(int, input().split(' ')))
	for (i, (n, z)) in enumerate(zip(arraya, arrayb)):
		if arraya[i] < arrayb[i]:
			arraya[i] = z
			arrayb[i] = n
	sumatoria = 0
	for (k, j) in zip(arraya, arraya[1:]):
		sumatoria += abs(k - j)
	for (k, j) in zip(arrayb, arrayb[1:]):
		sumatoria += abs(k - j)
	print(sumatoria)


n = int(input())
ans = []
for j in range(n):
	x = int(input())
	l = list(map(int, input().split()))
	l1 = list(map(int, input().split()))
	c = 0
	for i in range(x - 1):
		cur = abs(l[i + 1] - l[i]) + abs(l1[i + 1] - l1[i])
		cha = abs(l1[i + 1] - l[i]) + abs(l[i + 1] - l1[i])
		if cha < cur:
			(l[i + 1], l1[i + 1]) = (l1[i + 1], l[i + 1])
			c += cha
		else:
			c += cur
	ans.append(c)
for i in ans:
	print(i)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	for i in range(n):
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
	s = 0
	for i in range(n - 1):
		d = a[i] - a[i + 1]
		d2 = b[i] - b[i + 1]
		if d >= 0 and d2 >= 0:
			s += d + d2
		elif d >= 0 and d2 < 0:
			s += d + -d2
		elif d < 0 and d2 >= 0:
			s += -d + d2
		else:
			s += -(d + d2)
	print(s)


test = int(input())
for i in range(test):
	n = int(input())
	a_arr = [int(x) for x in input().split()]
	b_arr = [int(x) for x in input().split()]
	for i in range(n):
		x = a_arr[i]
		y = b_arr[i]
		if x > y:
			(a_arr[i], b_arr[i]) = (b_arr[i], a_arr[i])
	sum = 0
	for j in range(n - 1):
		sum += abs(a_arr[j] - a_arr[j + 1])
		sum += abs(b_arr[j] - b_arr[j + 1])
	print(sum)


for i in range(int(input())):
	n = int(input())
	l = input()
	k = input()
	w = list(map(int, l.split()))
	m = list(map(int, k.split()))
	for x in range(n):
		if w[x] > m[x]:
			check = w[x]
			w[x] = m[x]
			m[x] = check
	s = 0
	for j in range(n - 1):
		s = s + abs(w[j] - w[j + 1]) + abs(m[j] - m[j + 1])
	print(s)


a = int(input())
for _ in range(a):
	b = int(input())
	k = 0
	c = list(map(int, input().split()))
	d = list(map(int, input().split()))
	for i in range(b):
		if c[i] <= d[i]:
			(c[i], d[i]) = (d[i], c[i])
	for i in range(b - 1):
		k += abs(c[i] - c[i + 1]) + abs(d[i] - d[i + 1])
	print(k)


for _ in range(int(input())):
	a = int(input())
	m = list(map(int, input().split()))
	n = list(map(int, input().split()))
	for i in range(a):
		if m[i] > n[i]:
			t = m[i]
			m[i] = n[i]
			n[i] = t
	res = 0
	for i in range(1, a):
		res += abs(m[i] - m[i - 1])
		res += abs(n[i] - n[i - 1])
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	i = 0
	while i < n:
		if a[i] > b[i]:
			(a[i], b[i]) = (b[i], a[i])
		i += 1
	(i, total) = (0, 0)
	while i < n - 1:
		(n1, n2) = (abs(a[i] - a[i + 1]), abs(b[i] - b[i + 1]))
		total += n1 + n2
		i += 1
	print(total)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a_sum = 0
	b_sum = 0
	for i in range(n):
		if b[i] > a[i]:
			temp = a[i]
			a[i] = b[i]
			b[i] = temp
	for i in range(n - 1):
		a_sum += abs(a[i] - a[i + 1])
		b_sum += abs(b[i] - b[i + 1])
	sum = a_sum + b_sum
	print(sum)