You have a total of 3N balls of colours Red, Blue and Green. There are exactly N balls of each colour.

These 3N balls are now distributed to 3 boxes such that each box contains exactly N balls. You are given the contents of each box.

You would like the 1^{st} box to contain all the red balls, the 2^{nd} box to contain all the green balls, and the 3^{rd} box to contain all the blue balls. Note that the given order of boxes is important here — it is not enough for each box to contain only balls of a single colour.

To achieve this, you can perform the following operation several (possibly, zero) times:
Pick any two distinct boxes, pick any one ball from each of these two boxes, and swap them.

Determine the minimum number of operations required to satisfy the given condition.

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

- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- Each test case consists of 4 lines of input.
- The first line of each test case contains a single integer N, denoting the number of balls of each colour.
- The i-th of the next three lines contains three space-separated integers R_{i}, G_{i} and B_{i} — the number of red, green, and blue balls in the i-th box respectively.

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

For each test case, output a single line containing one integer — the minimum number of operations required such that the given condition is satisfied.

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

$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 1000$
$0 ≤ R_{i}, G_{i}, B_{i} ≤ N$
$R_{1} + G_{1} + B_{1} = R_{2} + G_{2} + B_{2} = R_{3} + G_{3} + B_{3} = N$
$R_{1} + R_{2} + R_{3} = G_{1} + G_{2} + G_{3} = B_{1} + B_{2} + B_{3} = N$

------ subtasks ------ 

Subtask #1 (100 points): Original constraints

----- Sample Input 1 ------ 
2
3
3 0 0
0 3 0
0 0 3
5
2 1 2
1 4 0
2 0 3
----- Sample Output 1 ------ 
0
3
----- explanation 1 ------ 
Test case $1$: Initially,
- The first box has $3$ red balls and none of any other colours
- The second box has $3$ green balls and none of any other colours
- The third box has $3$ blue balls and none of any other colours

The condition is already satisfied, and so no moves are required.

Test case $2$: One sequence of moves is as follows:
- Swap a green ball from the first box with a red ball from the second box.
- Swap a blue ball from the first box with a red ball from the third box.
- Once again, swap a blue ball from the first box with a red ball from the third box.

Now the first box has only red balls, the second has only green balls, and the third has only blue  ones — as required.
It can be verified that no sequence of less than three moves achieves this result.

Source: codechef | Difficulty: MEDIUM


Solutions:
for i in range(int(input())):
	n = int(input())
	r = list(map(int, input().split()))
	g = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(max(r[1] + r[2] + g[2], g[0] + b[0] + b[1]))


t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	z = list(map(int, input().split()))
	m = x[1] + x[2] + y[2]
	n = y[0] + z[0] + z[1]
	if m > n:
		print(m)
	else:
		print(n)


n = int(input())
for i in range(n):
	k = int(input())
	arr1 = [int(x) for x in input().split()]
	arr2 = [int(x) for x in input().split()]
	arr3 = [int(x) for x in input().split()]
	steps = k - arr1[0]
	if arr2[0] >= arr1[1]:
		steps += k - (arr1[1] + arr2[1])
	else:
		steps += k - (arr2[1] + arr2[0])
	print(steps)


for _ in range(int(input())):
	n = int(input())
	(r1, g1, b1) = map(int, input().split())
	(r2, g2, b2) = map(int, input().split())
	(r3, g3, b3) = map(int, input().split())
	if r1 == g2 and g2 == b3 and (b3 == n):
		print(0)
		continue
	a = r2 + r3 + g3
	b = g1 + b1 + b2
	m = max(a, b)
	print(m)


def rin():
	return list(map(int, input().split()))
for _ in range(int(input())):
	N = int(input())
	R = rin()
	G = rin()
	B = rin()
	print(max(R[1] + R[2] + G[2], G[0] + B[0] + B[1]))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = list(map(int, input().split()))
	print(max(a[1] + a[2] + b[2], b[0] + c[0] + c[1]))


tests = int(input())
for i in range(tests):
	n = int(input())
	balls_arr = []
	for j in range(3):
		box = list(map(int, input().split()))
		balls_arr.append(box)
	ans = 0
	red_balls = balls_arr[0][0]
	green_balls = balls_arr[1][1]
	yellow_balls = balls_arr[2][2]
	red_balls_in_second_box = balls_arr[1][0]
	green_balls_in_first_box = balls_arr[0][1]
	if red_balls_in_second_box < green_balls_in_first_box:
		green_balls += red_balls_in_second_box
	else:
		green_balls += green_balls_in_first_box
	print(n - red_balls + (n - green_balls))


import os, sys, io, math
from array import array
from math import *
I = lambda : [*map(int, sys.stdin.readline().split())]
IS = lambda : input()
IN = lambda : int(input())
IF = lambda : float(input())
for _ in range(IN()):
	(n, l, c) = (IN(), [], 0)
	for i in range(3):
		l.append(I())
	(r, g, b) = (l[0][0], l[1][1], l[2][2])
	(rb, gb) = (l[1][0], l[0][1])
	if rb < gb:
		g += rb
	else:
		g += gb
	print(n * 2 - r - g)


for _ in range(int(input())):
	n = int(input())
	(r1, g1, b1) = map(int, input().split())
	(r2, g2, b2) = map(int, input().split())
	(r3, g3, b3) = map(int, input().split())
	print(max(g1 + b1 + b2, r2 + r3 + g3))


for _ in range(int(input())):
	msn = int(input())
	(msx1, msy1, msz1) = map(int, input().split())
	(msx2, msy2, msz2) = map(int, input().split())
	(msx3, msy3, msz3) = map(int, input().split())
	a1 = msy1 + msz1 + msz2
	a2 = msx2 + msz2 + msx3
	a3 = msy1 + msz1 + msy3
	print(max(a1, a2, a3))


t = int(input())
for i in range(t):
	n = int(input())
	(r1, g1, b1) = map(int, input().split())
	(r2, g2, b2) = map(int, input().split())
	(r3, g3, b3) = map(int, input().split())
	s1 = g1 + b1 + b2
	s2 = r2 + r3 + g3
	if s1 > s2:
		print(s1)
	else:
		print(s2)


for _ in range(int(input())):
	N = int(input())
	(R1, G1, B1) = map(int, input().split())
	(R2, G2, B2) = map(int, input().split())
	(R3, G3, B3) = map(int, input().split())
	print(max(G1 + B1 + B2, R2 + G3 + R3))


for i in range(int(input())):
	N = int(input())
	(A, B, C) = map(int, input().split())
	(X, Y, Z) = map(int, input().split())
	(P, Q, R) = map(int, input().split())
	print(max(B + C + Z, X + P + Q))


for _ in range(int(input())):
	n = int(input())
	box1 = list(map(int, input().split()))
	box2 = list(map(int, input().split()))
	box3 = list(map(int, input().split()))
	(counter1, counter2) = (0, 0)
	counter1 = sum([box1[1] + box1[2] + box2[2]])
	counter2 = sum([box2[0] + box3[0] + box3[1]])
	print(max(counter1, counter2))


for _ in range(int(input())):
	n = int(input())
	(r1, g1, b1) = map(int, input().split())
	(r2, g2, b2) = map(int, input().split())
	(r3, g3, b3) = map(int, input().split())
	if (r1 != 0 and b1 == 0 and (g1 == 0)) and (r1 == 0 and b1 != 0 and (g1 == 0)) and (r1 == 0 and b1 == 0 and (g1 != 0)):
		print('0')
	else:
		t1 = g1 + b1 + b2
		t2 = r2 + r3 + g3
		print(max(t1, t2))


from itertools import *
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

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

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	n = inp()
	r = []
	for i in range(3):
		r.append(lis())
	swaps = 0
	red_green = min(r[0][1], r[1][0])
	green_blue = min(r[1][2], r[2][1])
	red_blue = min(r[2][0], r[0][2])
	swaps += red_green + green_blue + red_blue
	r[0][0] += red_green
	r[0][1] -= red_green
	r[1][0] -= red_green
	r[1][1] += red_green
	r[1][2] -= green_blue
	r[1][1] += green_blue
	r[2][1] -= green_blue
	r[2][2] += green_blue
	r[2][0] -= red_blue
	r[2][2] += red_blue
	r[0][2] -= red_blue
	r[0][0] += red_blue
	swaps += 2 * (n - r[0][0])
	print(swaps)


class Solution:

	def crycolr(self, n, arr1, arr2, arr3):
		Sum = arr1[1] + arr1[2] + arr2[2]
		Sum1 = arr2[0] + arr3[0] + arr3[1]
		return max(Sum, Sum1)
tc = int(input())
while tc > 0:
	n = int(input())
	arr1 = [int(x) for x in input().split()]
	arr2 = [int(x) for x in input().split()]
	arr3 = [int(x) for x in input().split()]
	Sol = Solution()
	print(Sol.crycolr(n, arr1, arr2, arr3))
	tc -= 1


for _ in range(int(input())):
	no = int(input())
	r = list(map(int, input().split()))
	g = list(map(int, input().split()))
	b = list(map(int, input().split()))
	task = 0
	if r[0] == g[1] and g[1] == b[2] and (b[2] == no):
		print(0)
	else:
		ls = b[0] + b[1] + g[0]
		rs = r[1] + r[2] + g[2]
		print(max(ls, rs))


a = int(input())
for _ in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	d = list(map(int, input().split()))
	e = list(map(int, input().split()))
	f = c[1] + c[2] + d[2]
	g = d[0] + e[0] + e[1]
	if f > g:
		print(f)
	else:
		print(g)


t = int(input())
for i in range(t):
	n = int(input())
	(r1, g1, b1) = map(int, input().split())
	(r2, g2, b2) = map(int, input().split())
	(r3, g3, b3) = map(int, input().split())
	if r1 == n and g1 == n and (b1 == n):
		print(0)
		continue
	print(max(r2 + r3 + g3, g1 + b1 + b2))


def swp(x, i, y, j):
	diff = min(x[i], y[j])
	x[i] -= diff
	x[j] += diff
	y[i] += diff
	y[j] -= diff
	return diff
for _ in range(int(input())):
	N = int(input())
	R = [int(x) for x in input().split()]
	G = [int(x) for x in input().split()]
	B = [int(x) for x in input().split()]
	ans = 0
	if G[0]:
		if R[1]:
			ans += swp(G, 0, R, 1)
		if R[2]:
			ans += swp(G, 0, R, 2)
	if B[0]:
		if R[1]:
			ans += swp(B, 0, R, 1)
		if R[2]:
			ans += swp(B, 0, R, 2)
	if B[1]:
		ans += swp(B, 1, G, 2)
	print(ans)


try:
	t = int(input())
	while t:
		n = int(input())
		r = list(map(int, input().split()))
		g = list(map(int, input().split()))
		b = list(map(int, input().split()))
		if r[0] == g[1] and r[0] == b[2] and (r[0] == n):
			print(0)
		else:
			x = g[0] + b[0] + b[1]
			y = r[1] + r[2] + g[2]
			print(max(x, y))
		t -= 1
except:
	pass1


tc = int(input())
for _ in range(tc):
	(n, mat) = (int(input()), [])
	for i in range(3):
		x = list(map(int, input().split()))
		mat.append(x)
	r_g = min(mat[0][1], mat[1][0])
	g_b = min(mat[1][2], mat[2][1])
	b_r = min(mat[0][2], mat[2][0])
	swaps = r_g + g_b + b_r
	mat[0][0] += r_g
	mat[0][1] -= r_g
	mat[1][0] -= r_g
	mat[1][1] += r_g
	mat[1][1] += g_b
	mat[1][2] -= g_b
	mat[2][1] -= g_b
	mat[2][2] += g_b
	mat[2][2] += b_r
	mat[2][0] -= b_r
	mat[0][2] -= b_r
	mat[0][0] += b_r
	x = n - mat[0][0]
	swaps += 2 * x
	print(swaps)


for _ in range(int(input())):
	N = int(input())
	arr = []
	for i in range(3):
		b = list(map(int, input().split()))
		arr.append(b)
	if arr[0][0] == N and arr[1][1] == N and (arr[2][2] == N):
		print(0)
		continue
	c = arr[1][0] + arr[2][0] + arr[2][1]
	b = arr[0][1] + arr[0][2] + arr[1][2]
	print(max(c, b))


t = int(input())
for _ in range(t):
	n = int(input())
	m = []
	for i in range(3):
		m.append(list(map(int, input().split())))
	sum1 = sum2 = 0
	for i in range(3):
		for j in range(3):
			if i > j:
				sum1 += m[i][j]
			elif i < j:
				sum2 += m[i][j]
	print(max(sum1, sum2))


for _ in range(int(input())):
	n = int(input())
	a = [[int(x) for x in input().split()] for y in range(3)]
	ans = 0
	tmp1 = a[0][1]
	tmp2 = a[1][0]
	a[0][1] -= min(tmp1, tmp2)
	a[1][0] -= min(tmp1, tmp2)
	a[0][0] += min(tmp1, tmp2)
	a[1][1] += min(tmp1, tmp2)
	ans += min(tmp1, tmp2)
	tmp1 = a[0][2]
	tmp2 = a[2][0]
	a[0][2] -= min(tmp1, tmp2)
	a[2][0] -= min(tmp1, tmp2)
	a[0][0] += min(tmp1, tmp2)
	a[2][2] += min(tmp1, tmp2)
	ans += min(tmp1, tmp2)
	tmp1 = a[1][2]
	tmp2 = a[2][1]
	a[1][2] -= min(tmp1, tmp2)
	a[2][1] -= min(tmp1, tmp2)
	a[1][1] += min(tmp1, tmp2)
	a[2][2] += min(tmp1, tmp2)
	ans += min(tmp1, tmp2)
	ans += 2 * (n - a[0][0])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(3):
		b = list(map(int, input().split()))
		a.append(b)
	if a[0][0] == n and a[1][1] == n and (a[2][2] == n):
		print(0)
		continue
	c = a[1][0] + a[2][0] + a[2][1]
	b = a[0][1] + a[0][2] + a[1][2]
	print(max(c, b))


T = int(input())
for i in range(T):
	N = int(input())
	(r1, g1, b1) = map(int, input().strip().split())
	(r2, g2, b2) = map(int, input().strip().split())
	(r3, g3, b3) = map(int, input().strip().split())
	if g1 + b1 + b2 >= r2 + r3 + g3:
		print(g1 + b1 + b2)
	else:
		print(r2 + r3 + g3)


for i in range(int(input())):
	n = int(input())
	(a1, b1, c1) = list(map(int, input().split()))
	(a2, b2, c2) = list(map(int, input().split()))
	(a3, b3, c3) = list(map(int, input().split()))
	c = 0
	if a1 == n and b1 == n and (c1 == n):
		print(c)
		continue
	x = a2 + a3 + b3
	y = b1 + c1 + c2
	print(max(x, y))


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	list1 = list(map(int, input().split()))
	list2 = list(map(int, input().split()))
	list3 = list(map(int, input().split()))
	ans1 = list1[1] + list1[2] + list2[2]
	ans2 = list2[0] + list3[0] + list3[1]
	realans = max(ans2, ans1)
	print(realans)


t = int(input())
for i in range(t):
	N = int(input())
	COUNT = 0
	R = [int(x) for x in input().split()]
	G = [int(x) for x in input().split()]
	B = [int(x) for x in input().split()]
	sum_lower = G[0] + B[0] + B[1]
	sum_upper = R[1] + R[2] + G[2]
	ANS = max(sum_upper, sum_lower)
	print(ANS)


t = int(input())
for i in range(t):
	n = map(int, input().split())
	R = list(map(int, input().split()))
	G = list(map(int, input().split()))
	B = list(map(int, input().split()))
	if R[0] == n and B[1] == n and (G[2] == n):
		print('0')
	U = R[1] + R[2] + G[2]
	L = G[0] + B[0] + B[1]
	Result = max(U, L)
	print(Result)


p = int(input())
while p > 0:
	n = int(input())
	r = list(map(int, input().split()))
	g = list(map(int, input().split()))
	b = list(map(int, input().split()))
	sm1 = r[1] + g[2] + r[2]
	sm2 = g[0] + b[0] + b[1]
	if sm1 > sm2:
		print(sm1)
	else:
		print(sm2)
	p -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = list(map(int, input().split()))
	z = []
	z.append(a)
	z.append(b)
	z.append(c)
	if z[0][0] == n and z[1][1] == n and (z[2][2] == n):
		print(0)
		continue
	x = z[0][1] + z[0][2] + z[1][2]
	y = z[1][0] + z[2][0] + z[2][1]
	print(max(x, y))


T = int(input())
for i in range(T):
	N = int(input())
	(r1, g1, b1) = map(int, input().split())
	(r2, g2, b2) = map(int, input().split())
	(r3, g3, b3) = map(int, input().split())
	print(max(g1 + b1 + b2, r2 + r3 + g3))


for _ in range(int(input())):
	n = int(input())
	B1 = list(map(int, input().split()))
	B2 = list(map(int, input().split()))
	B3 = list(map(int, input().split()))
	lower_mat_tri = B2[0] + B3[0] + B3[1]
	upper_mat_tri = B1[1] + B1[2] + B2[2]
	if lower_mat_tri > upper_mat_tri:
		print(lower_mat_tri)
	else:
		print(upper_mat_tri)


for i in range(int(input())):
	n = int(input())
	F = list(map(int, input().split()))
	S = list(map(int, input().split()))
	T = list(map(int, input().split()))
	if F[1] >= S[0]:
		S[1] = S[1] + S[0]
	else:
		S[1] = F[1] + S[1]
	print(n - F[0] + (n - S[1]))


T = int(input())
for _ in range(T):
	N = int(input())
	arr = []
	for i in range(3):
		A1 = []
		A1 = [int(i) for i in input().split()]
		arr.append(A1)
	sum1 = arr[0][1] + arr[0][2] + arr[1][2]
	sum2 = arr[1][0] + arr[2][0] + arr[2][1]
	print(max(sum1, sum2))


t = int(input())
for i in range(t):
	d = int(input())
	a = [int(n) for n in input().split()]
	b = [int(m) for m in input().split()]
	c = [int(o) for o in input().split()]
	p = a[1] + a[2] + b[2]
	q = c[0] + c[1] + b[0]
	print(max(p, q))


t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	z = list(map(int, input().split()))
	print(max(x[1] + x[2] + y[2], y[0] + z[0] + z[1]))


for i in range(int(input())):
	n = int(input())
	(a, b, c) = map(int, input().split())
	(d, e, f) = map(int, input().split())
	(g, h, i) = map(int, input().split())
	s1 = b + c + f
	s2 = d + g + h
	if s1 > s2:
		print(s1)
	else:
		print(s2)


for t in range(int(input())):
	N = int(input())
	(R, G, B) = (list(map(int, input().split(' '))), list(map(int, input().split(' '))), list(map(int, input().split(' '))))
	count = 0
	x = min(R[1], G[0])
	G[0] -= x
	R[0] += x
	R[1] -= x
	G[1] += x
	count += x
	x = min(R[2], B[0])
	B[0] -= x
	R[0] += x
	R[2] -= x
	B[2] += x
	count += x
	x = min(G[2], B[1])
	B[1] -= x
	G[1] += x
	G[2] -= x
	B[2] += x
	count += x
	count += 2 * max(R[1], R[2], G[2])
	print(count)


num = int(input())
for i in range(num):
	tb = int(input())
	b1 = list(map(int, input().split(' ')))
	b2 = list(map(int, input().split(' ')))
	b3 = list(map(int, input().split(' ')))
	a = b1[1] + b1[2] + b2[2]
	b = b2[0] + b3[1] + b3[0]
	print(max(a, b))


for i in range(int(input())):
	n = int(input())
	r = []
	for i in input().split():
		r.append(int(i))
	g = []
	for i in input().split():
		g.append(int(i))
	b = []
	for i in input().split():
		b.append(int(i))
	if r[0] == n and g[1] == n and (b[2] == n):
		print(0)
	else:
		print(max(r[1] + r[2] + g[2], b[0] + b[1] + g[0]))


for i in range(int(input())):
	n = int(input())
	r = []
	for i in range(3):
		r.append(list(map(int, input().split())))
	swaps = 0
	red_green = min(r[0][1], r[1][0])
	green_blue = min(r[1][2], r[2][1])
	blue_red = min(r[2][0], r[0][2])
	swaps += red_green + green_blue + blue_red
	r[0][1] -= red_green
	r[1][0] -= red_green
	r[0][0] += red_green
	r[1][1] += red_green
	r[1][2] -= green_blue
	r[1][1] += green_blue
	r[2][1] -= green_blue
	r[2][2] += green_blue
	r[2][0] -= blue_red
	r[2][2] += blue_red
	r[0][2] -= blue_red
	r[0][0] += blue_red
	swaps += 2 * (n - r[0][0])
	print(swaps)


t = int(input())
r = []
g = []
b = []
l = []
for i in range(t):
	n = int(input())
	for j in range(3):
		(r1, g1, b1) = map(int, input().split())
		r.append(r1)
		g.append(g1)
		b.append(b1)
	x1 = g[0] + b[0] + b[1]
	x2 = r[1] + r[2] + g[2]
	l.append(x1)
	l.append(x2)
	print(max(l))
	r.clear()
	g.clear()
	b.clear()
	l.clear()


for i in range(int(input())):
	n = int(input())
	mat = []
	for i in range(3):
		ar = [int(x) for x in input().split()]
		mat.append(ar)
	s1 = min(mat[0][1], mat[1][0])
	mat[0][1] -= s1
	mat[1][0] -= s1
	s2 = min(mat[0][2], mat[2][0])
	mat[0][2] -= s2
	mat[2][0] -= s2
	s3 = min(mat[1][2], mat[2][1])
	mat[1][2] -= s3
	mat[2][1] -= s3
	s = s1 + s2 + s3 + mat[0][1] * 2 + mat[0][2] * 2
	print(s)