Chef is given two arrays A and B, each having N elements.

In one move, Chef can choose an index i (1 ≤ i ≤ N-1), get (A_{i} - A_{i+1}) points, and then swap A_{i} and A_{i+1}.

For example: If Chef has the array - [10, 7,5] and Chef chooses index 1 during his move, he will get 10 - 7 = 3 points and the new array will become [7 , 10 ,5]

Can you help Chef in finding the maximum number of points he can get while converting the array A into array B?

\textbf{Note:} It is guaranteed in the input that there exists a sequence of moves which can convert array A into B.

------ Input Format ------ 

- The first line of input contains a single integer T, denoting the number of testcases. The description of the T testcases follows.
- The first line of each test case contains a single integer N denoting the number of elements in A and B.
- The second line of each test case contains N space separated integers A_{1}, A_{2},..., A_{N}.
- The third line of each test case contains N space separated integers B_{1}, B_{2},..., B_{N}.

------ Output Format ------ 

For each test case, print a single line containing one integer, which is the maximum number of points that Chef can get while converting the array A into array B

------ Constraints ------ 

$1 ≤ T ≤ 5 \cdot 10^{4}$
$2 ≤ N ≤ 5 \cdot 10^{5}$
$1 ≤ A_{i} ≤ 10^{5}$
$1 ≤ B_{i} ≤ 10^{5}$
- The sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$

----- Sample Input 1 ------ 
3
2
1 2
2 1
3
3 2 1
1 2 3
3
1 2 3
1 2 3
----- Sample Output 1 ------ 
-1
4
0
----- explanation 1 ------ 
Test Case $1$: Chef can choose $i = 1$ in the first move, get $1 - 2 = -1$ points and swap $A_{1}$ and $A_{2}$. After this move, $A$ is converted into $B$. We can prove that there exists no other sequence of moves which can result in higher score while converting $A$ into $B$.

Test Case $2$: One possible sequence of moves is the following:
- Choose $i = 2$. Total points $= 1$, and $A$ will become $[3, 1, 2]$.
- Choose $i = 1$. Total points $= 1 + 2$, and $A$ will become $[1, 3, 2]$.
- Choose $i = 2$. Total points $= 1 + 2 + 1$, and $A$ will become $[1, 2, 3]$.

There are other possible sequences of moves which can result in total $4$ points, but no such sequence exists which can result in more than $4$ points, and simultaneously convert $A$ into $B$.

Difficulty: VERY_HARD

Solutions:
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	aind = []
	bind = []
	for i in range(n):
		aind.append([a[i], i])
	for i in range(n):
		bind.append([b[i], i])
	aind.sort()
	bind.sort()
	c = 0
	for i in range(n):
		if aind[i][1] > bind[i][1]:
			c -= aind[i][0] * (aind[i][1] - bind[i][1])
		else:
			c += aind[i][0] * (bind[i][1] - aind[i][1])
	print(c)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		if b[i] in d:
			d[b[i]].append(i)
		else:
			d[b[i]] = [i]
	c = 0
	for i in range(n):
		ind = d[a[i]][-1]
		d[a[i]] = d[a[i]][:-1]
		c += (ind - i) * a[i]
	print(c)


import sys
from math import *
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		ans -= l[i] * (i + 1)
		ans += p[i] * (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()))
	a = [[a[x], x] for x in range(n)]
	b = [[b[x], x] for x in range(n)]
	a = sorted(a)
	b = sorted(b)
	ans = 0
	for j in range(n):
		if b[j][1] != a[j][1]:
			ans += a[j][0] * (b[j][1] - a[j][1])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a = [[a[i], i] for i in range(n)]
	b = [[b[i], i] for i in range(n)]
	a = sorted(a)
	b = sorted(b)
	ans = 0
	for i in range(n):
		if b[i][1] != a[i][1]:
			ans += a[i][0] * (b[i][1] - a[i][1])
	print(ans)


t = int(input())
for tc in range(t):
	n = int(input())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	res = 0
	for i in range(1, n + 1):
		res -= i * arr1[i - 1]
	for i in range(1, n + 1):
		res += i * arr2[i - 1]
	print(res)


for t in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	c = 0
	for i in range(n):
		c += b[i] * (i + 1) - a[i] * (i + 1)
	print(c)


ans = ''
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	s = 0
	for i in range(n):
		s -= a[i] * i
	for i in range(n):
		s += b[i] * i
	ans += str(s) + '\n'
print(ans)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	mo_left = 0
	mo_right = 0
	for i in range(n):
		mo_left += -1 * (i + 1) * a[i]
		mo_right += (i + 1) * b[i]
	print(mo_right + mo_left)


test = int(input())
for t in range(test):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	C = [(A[i], i) for i in range(N)]
	D = [(B[i], i) for i in range(N)]
	C.sort(key=lambda x: x[0])
	D.sort(key=lambda x: x[0])
	cost = 0
	for i in range(N):
		cost += C[i][0] * (D[i][1] - C[i][1])
	print(cost)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	A = sum((ele * (i + 1) for (i, ele) in enumerate(a)))
	B = sum((ele * (i + 1) for (i, ele) in enumerate(b)))
	print(B - A)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	sa = sum([b[i] * (i + 1) - a[i] * (i + 1) for i in range(n)])
	print(sa)


for h in range(int(input())):
	art = input()
	a = input().split(' ')
	a = list(map(int, a))
	b = input().split(' ')
	b = list(map(int, b))
	summer = 0
	s2 = 0
	c = 1
	for i in a:
		summer += i * c
		c += 1
	c = 1
	for i in b:
		s2 += i * c
		c += 1
	print(s2 - summer)


from sys import stdin
inp = stdin.readline
T = int(inp())
ans = []
for tx in range(T):
	N = int(inp())
	A_score = sum((p * a for (p, a) in enumerate(map(int, inp().split()))))
	B_score = sum((p * b for (p, b) in enumerate(map(int, inp().split()))))
	ans.append(B_score - A_score)
print(*ans, sep='\n')


from sys import stdin
inp = stdin.readline

def arrange_cost(arr1, arr2):
	stval = sum((p * a for (p, a) in enumerate(arr1)))
	enval = sum((p * a for (p, a) in enumerate(arr2)))
	return enval - stval
T = int(inp())
ans = []
for tx in range(T):
	N = int(inp())
	Ays = map(int, inp().split())
	Bs = map(int, inp().split())
	ans.append(arrange_cost(Ays, Bs))
print(*ans, sep='\n')


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

def inInt():
	return int(input())

def outList(lst):
	for data in lst:
		print(data)

def solveA():
	for _ in range(inInt()):
		(M, N, K) = inIntList()
		ans.append('YES' if M > N * K else 'NO')

def solveB():
	for _ in range(inInt()):
		(N, P, X, Y) = inIntList()
		A = inIntList()
		child = A[:P].count(0)
		elder = P - child
		ans.append(X * child + Y * elder)

def solveC():
	for _ in range(inInt()):
		S = input()
		old = S[0]
		wand = 1 if old == '0' else 0
		for new in S[1:]:
			if old != new:
				wand += 1
				old = new
		ans.append(wand)

def solveD():
	for _ in range(inInt()):
		(P, a, b, c, x, y) = inIntList()
		ans.append(P // min(b + a * x, c + a * y))

def solveE():

	def gcd(x, y):
		return gcd(y, x % y) if x % y else y
	for _ in range(inInt()):
		(a, b) = inIntList()
		common = gcd(a, b)
		while common != 1:
			a //= common
			common = gcd(a, b)
		ans.append('YES' if a == 1 else 'NO')

def solveF():
	for _ in range(inInt()):
		N = inInt()
		(A, B) = (inIntList(), inIntList())
		ans.append(sum(((N - i) * (A[i] - B[i]) for i in range(N))))
ans = []
solveF()
outList(ans)


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

def inInt():
	return int(input())

def outList(lst):
	for data in lst:
		print(data)

def solveA():
	for _ in range(inInt()):
		(M, N, K) = inIntList()
		ans.append('YES' if M > N * K else 'NO')

def solveB():
	for _ in range(inInt()):
		(N, P, X, Y) = inIntList()
		A = inIntList()
		child = A[:P].count(0)
		elder = P - child
		ans.append(X * child + Y * elder)

def solveC():
	for _ in range(inInt()):
		S = input()
		old = S[0]
		wand = 1 if old == '0' else 0
		for new in S[1:]:
			if old != new:
				wand += 1
				old = new
		ans.append(wand)

def solveD():
	for _ in range(inInt()):
		(P, a, b, c, x, y) = inIntList()
		ans.append(P // min(b + a * x, c + a * y))

def solveE():

	def gcd(x, y):
		return gcd(y, x % y) if x % y else y
	for _ in range(inInt()):
		(a, b) = inIntList()
		common = gcd(a, b)
		while common != 1:
			a //= common
			common = gcd(a, b)
		ans.append('YES' if a == 1 else 'NO')

def solveF():
	for _ in range(inInt()):
		N = inInt()
		(A, B) = (inIntList(), inIntList())
		result = 0
		dic = {}
		for (i, a) in enumerate(A):
			if a in dic:
				dic[a].add(i)
			else:
				dic[a] = set([i])
		for (i, b) in enumerate(B):
			if b == A[i]:
				dic[b].remove(i)
				continue
			ai = dic[b].pop()
			dic[A[i]].remove(i)
			dic[A[i]].add(ai)
			A[ai] = A[i]
			result += (ai - i) * (A[i] - b)
		ans.append(result)
ans = []
solveF()
outList(ans)


import math
for q in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	arra = []
	arrb = []
	for i in range(n):
		temp = [a[i], i]
		arra.append(temp)
		temp = [b[i], i]
		arrb.append(temp)
	arra.sort()
	arrb.sort()
	ans = 0
	for i in range(n):
		ans += arra[i][0] * (arrb[i][1] - arra[i][1])
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	score = 0
	for i in range(n):
		score -= (i + 1) * a[i]
	for j in range(n):
		score += (j + 1) * b[j]
	print(score)


def check(l1, l2, n):
	a = b = c = 0
	for i in range(n):
		a += l1[i]
		b += l2[i]
		c += a - b
	return c
t = int(input())
for i in range(t):
	n = int(input())
	l1 = [int(i) for i in input().split()]
	l2 = [int(i) for i in input().split()]
	print(check(l1, l2, n))


for r in range(int(input())):
	n = list(map(int, input().split()))
	a = [0]
	a += list(map(int, input().split()))
	b = [0]
	b += list(map(int, input().split()))
	score = 0
	for i in range(1, len(a)):
		score -= i * a[i]
	a = []
	for i in range(1, len(b)):
		score += i * b[i]
	print(score)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = 0
	for i in range(1, len(a)):
		ans -= a[i] * i
	for i in range(1, len(b)):
		ans += b[i] * i
	print(ans)


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


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	B = 0
	A = 0
	for i in range(1, n + 1):
		A += a[i - 1] * i
	for i in range(1, n + 1):
		B += b[i - 1] * i
	print(B - A)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		ans += i * (b[i] - a[i])
	sys.stdout.write(str(ans) + '\n')


def mainFunc():
	length = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	count = 0
	for i in range(length):
		count -= A[i] * i
	for j in range(length):
		count += B[j] * j
	print(count)
tests = int(input())
while tests:
	mainFunc()
	tests -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	counta = 0
	countb = 0
	for i in range(n):
		counta += (i + 1) * a[i]
		countb += (i + 1) * b[i]
	ans = countb - counta
	print(ans)


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	q = list(map(int, input().split()))
	(a, b, s1, s2) = ([], [], 0, 0)
	for i in range(len(p)):
		s1 += p[i]
		a.append(s1)
	for i in range(len(q)):
		s2 += q[i]
		b.append(s2)
	ans = 0
	for i in range(len(a)):
		ans += a[i] - b[i]
	print(ans)


from collections import defaultdict

def solve(n, a, b):
	index_map = defaultdict(list)
	for i in range(n):
		index_map[a[i]].append(i)
	cost = 0
	for i in range(n):
		start = index_map[b[i]].pop()
		cost = cost + b[i] * (i - start)
	return cost
t = int(input().strip())
inputs = []
for _ in range(t):
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	b = list(map(int, input().strip().split()))
	inputs.append((n, a, b))
for (n, a, b) in inputs:
	print(solve(n, a, b))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	cnt = x = y = 0
	for i in range(n):
		x += a[i]
		y += b[i]
		cnt += x - y
	print(cnt)


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(' ')]
	x = y = z = 0
	for j in range(n):
		x += a[j]
		y += b[j]
		z += x - y
	print(z)


t = int(input())
while t != 0:
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	ans = 0
	for i in range(n):
		ans -= a[i] * (i + 1)
		ans += b[i] * (i + 1)
	print(ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	new = []
	tar = []
	for i in range(n):
		new.append([a[i], i])
		tar.append([b[i], i])
	new.sort()
	tar.sort()
	ans = 0
	for i in range(n):
		if new[i][-1] < tar[i][-1]:
			ans -= new[i][0] * abs(new[i][-1] - tar[i][-1])
		else:
			ans += new[i][0] * abs(new[i][-1] - tar[i][-1])
	print(-ans)


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


a = input()
for i in range(int(a)):
	n = int(input())
	arr1 = [int(x) for x in input().split()]
	arr2 = [int(x) for x in input().split()]
	aa = 0
	bb = 0
	ans = 0
	for i in range(n):
		aa += arr1[i]
		bb += arr2[i]
		ans += aa - bb
	print(ans)


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = 0
	aa = 0
	bb = 0
	for i in range(n):
		aa += a[i]
		bb += b[i]
		ans += aa - bb
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	s1 = 0
	s2 = 0
	for i in range(n):
		s1 += a[i] * i
		s2 += b[i] * i
	print(s2 - s1)


def solution():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	points = 0
	a.insert(0, 0)
	for i in range(n + 1):
		points -= i * a[i]
	for i in range(n):
		points += (i + 1) * b[i]
	print(points)
for _ in range(int(input())):
	solution()


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(' ')]
	assert N == len(A)
	assert N == len(B)
	x = y = z = 0
	for j in range(N):
		x += A[j]
		y += B[j]
		z += x - y
	print(z)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	brr = [int(i) for i in input().split()]
	score = 0
	for i in range(n):
		score += -(i + 1) * arr[i]
	for i in range(n):
		score += (i + 1) * brr[i]
	print(score)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	s = 0
	for i in range(N):
		s += B[i] * (i + 1) - A[i] * (i + 1)
	print(s)


inf = float('inf')
import sys
import pprint
import logging
from logging import getLogger
import array

def input():
	return sys.stdin.readline().rstrip('\r\n')

def maps():
	return [int(i) for i in input().split()]
logging.basicConfig(format='%(message)s', level=logging.WARNING)
logger = getLogger(__name__)
logger.setLevel(logging.INFO)

def debug(msg, *args):
	logger.info(f'{msg}={pprint.pformat(args)}')

def sol1():
	from itertools import accumulate
	for i in range(*maps()):
		(n,) = maps()
		a = list(accumulate(maps()))
		b = list(accumulate(maps()))
		s = 0
		for i in range(n):
			s += a[i] - b[i]
		print(s)

def sol2():
	for __ in range(*maps()):
		(n,) = maps()
		a = [*maps()]
		b = [*maps()]
		ans = 0
		for i in range(n):
			ans -= (i + 1) * a[i]
			ans += (i + 1) * b[i]
		print(ans)
sol2()


def main():
	tc = int(input())
	while tc > 0:
		n = int(input())
		a1 = input().split()
		a2 = input().split()
		sum_a = [0] * n
		sum_b = [0] * n
		sum_a[0] = int(a1[0])
		sum_b[0] = int(a2[0])
		power = sum_a[0] - sum_b[0]
		for i in range(1, n):
			sum_a[i] = sum_a[i - 1] + int(a1[i])
			sum_b[i] = sum_b[i - 1] + int(a2[i])
			val = sum_a[i] - sum_b[i]
			power += val
		print(power)
		tc -= 1
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	count = 0
	l1 = l2 = 0
	for i in range(n):
		l1 += a[i]
		l2 += b[i]
		count += l1 - l2
	print(count)


for j in range(int(input())):
	a = int(input())
	lst1 = list(map(int, input().split(' ')))
	lst2 = list(map(int, input().split(' ')))
	c = 0
	s = 0
	b = 0
	if lst1 == lst2:
		print(0)
	else:
		for i in range(a):
			s += lst1[i]
			b += lst2[i]
			c += s - b
		print(c)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	x = a[-1]
	y = 1
	for i in range(n - 2, -1, -1):
		xx = x + a[i]
		a[i] = y * a[i] - x
		x = xx
		y += 1
	x = b[-1]
	y = 1
	for i in range(n - 2, -1, -1):
		xx = x + b[i]
		b[i] = y * b[i] - x
		x = xx
		y += 1
	(ans, a[-1], b[-1]) = (0, 0, 0)
	for i in range(n):
		ans += a[i]
		ans -= b[i]
	print(ans // 2)


from math import gcd
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	temp = arr[n - 1]
	sz = 1
	for i in range(n - 2, -1, -1):
		tempp = temp + arr[i]
		arr[i] = sz * arr[i] - temp
		temp = tempp
		sz += 1
	temp = brr[n - 1]
	sz = 1
	for i in range(n - 2, -1, -1):
		tempp = temp + brr[i]
		brr[i] = sz * brr[i] - temp
		temp = tempp
		sz += 1
	ans = 0
	arr[n - 1] = 0
	brr[n - 1] = 0
	for i in range(n):
		ans += arr[i]
		ans -= brr[i]
	print(ans // 2)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	temp = a[-1]
	size = 1
	for i in range(n - 2, -1, -1):
		t = temp + a[i]
		a[i] = size * a[i] - temp
		temp = t
		size += 1
	temp = b[-1]
	size = 1
	for i in range(n - 2, -1, -1):
		t = temp + b[i]
		b[i] = size * b[i] - temp
		temp = t
		size += 1
	ans = 0
	(a[-1], b[-1]) = (0, 0)
	for i in range(n):
		ans += a[i]
		ans -= b[i]
	print(ans // 2)


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	pre_ind = {}
	for i in range(n):
		pre_ind[brr[i]] = []
	for i in range(n):
		pre_ind[brr[i]].append(i)
	total = 0
	for i in range(n):
		next = pre_ind[arr[i]].pop()
		total += (next - i) * arr[i]
	print(total)
t = 1
t = int(input())
for test in range(t):
	main()


def main():
	alpha = 'abcdefghijklmnopqrstuvwxyz'
	ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	inf = 1e+17
	mod = 10 ** 9 + 7

	def factorial(n):
		f = 1
		for i in range(1, n + 1):
			f = f * i % mod
		return f

	def ncr(n, r):
		num = den = 1
		for i in range(r):
			num = num * (n - i) % mod
			den = den * (i + 1) % mod
		return num * pow(den, mod - 2, mod) % mod

	def solve(n, a, b):
		b_ind = dict()
		for i in range(n):
			if b[i] in b_ind:
				b_ind[b[i]] += [i]
			else:
				b_ind[b[i]] = [i]
		for ele in b_ind:
			b_ind[ele] = sorted(b_ind[ele], reverse=True)
		cnt = 0
		for i in range(n):
			best = b_ind[a[i]].pop()
			cnt += (best - i) * a[i]
		return cnt
		pass
	t = int(input())
	ans = []
	for _ in range(t):
		n = int(input())
		a = [int(x) for x in input().split()]
		b = [int(x) for x in input().split()]
		ans.append(solve(n, a, b))
	p = 1
	for answer in ans:
		print(answer)
		p += 1
import sys, threading
import bisect
import math
import itertools
from sys import stdout
import heapq
from queue import PriorityQueue
input = sys.stdin.readline
thread = threading.Thread(target=main)
thread.start()
thread.join()


def flex_it(arr, lex, size):
	j = 0
	ans = 0
	k = 0
	rem = 0
	while j < len(arr):
		if k == 0:
			rem = j
		if k < size:
			if ans < lex:
				j = rem + 1
				ans = 0
				k = 0
				continue
			j += 1
			k += 1
		if k == size:
			return True
	else:
		if k == size and ans >= lex:
			return True
		return False
for ii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	for i in range(n):
		d[b[i]] = []
	for i in range(n):
		d[b[i]].append(i)
	ans = 0
	for i in range(n):
		j = d[a[i]].pop()
		ans += (j - i) * a[i]
	print(ans)


from sys import stdin
input = stdin.readline

def answer():
	prefix = [0]
	for i in range(n):
		prefix.append(prefix[-1] + a[i])
	d = dict()
	for i in range(n):
		try:
			d[a[i]].append(i + 1)
		except:
			d[a[i]] = [i + 1]
	(ans, sub) = (0, 0)
	for i in range(n - 1, -1, -1):
		j = d[b[i]].pop()
		ans += (n - j) * a[j - 1] - (prefix[n] - prefix[j])
		ans -= (n - j) * a[j - 1] - sub
		sub += b[i]
	return ans
for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(answer())