You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1.
Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead.

-----Input-----

- The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N.
- N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai.

-----Output-----
For each test case, print a single line containing one integer — the maximum sum of picked elements.

-----Constraints-----
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 700
- 1 ≤ sum of N in all test-cases ≤ 3700
- 1 ≤ Aij ≤ 109 for each valid i, j

-----Subtasks-----
Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j
Subtask #2 (82 points): original constraints

-----Example-----
Input:

1
3
1 2 3
4 5 6
7 8 9

Output:

18

-----Explanation-----
Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.

Difficulty: MEDIUM

Solutions:
for j in range(int(input())):
	n = int(input())
	x = []
	for i in range(n):
		y = list(map(int, input().split()))
		y.sort()
		x.append(y)
	x.reverse()
	z = []
	a = max(x[0])
	z.append(a)
	v = 1
	for i in range(1, n):
		x[i].append(a)
		x[i].sort()
		b = x[i].index(a)
		if b - 1 >= 0:
			z.append(x[i][b - 1])
			a = x[i][b - 1]
		else:
			v = 0
			print(-1)
			break
	if v != 0:
		print(sum(z))


def score(qn):
	import math
	maximum = math.inf
	score = 0
	for i in reversed(range(0, len(qn))):
		temp = -1
		for j in qn[i]:
			if j > temp and j < maximum:
				temp = j
		if temp == -1:
			return -1
		else:
			score += temp
			maximum = temp
	return score
import sys
c = 0
for line in sys.stdin:
	if c == 0:
		t = int(line.strip())
		c = 2
	elif c % 2 == 0:
		new = line.split()
		N = int(new[0].strip())
		qn = []
		c = 5
		count = 1
	elif c == 5:
		a = list(map(int, line.split()))
		qn.append(a)
		count += 1
		if count == N + 1:
			c = 2
			print(score(qn))


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	for _ in range(n):
		h = list(map(int, input().split()))
		h.sort()
		a.append(h)
	c = 1
	k = max(a[n - 1])
	s = k
	for i in range(n - 2, -1, -1):
		for j in range(n - 1, -1, -1):
			if k > a[i][j]:
				k = a[i][j]
				c += 1
				s += k
				break
	if c == n:
		print(s)
	else:
		print('-1')


for _ in range(int(input())):
	n = int(input())
	l = []
	while n > 0:
		k = []
		k = [int(i) for i in input().split()]
		k = sorted(k)
		l.append(k)
		n -= 1
	count = [l[-1][-1]]
	flag = 0
	for i in range(len(l) - 2, -1, -1):
		for j in range(len(l) - 1, -1, -1):
			if l[i][j] < count[-1]:
				count.append(l[i][j])
				break
		else:
			flag = 1
			break
		if flag == 1:
			break
	if flag == 1:
		print(-1)
	else:
		print(sum(count))


for _ in range(int(input())):
	a = []
	n = int(input())
	for t in range(n):
		p = list(map(int, input().split()))
		a.append(p)
	c = [0] * n
	c[n - 1] = max(a[n - 1])
	f = 0
	s = 0
	for i in range(n - 2, -1, -1):
		c[i] = 0
		for j in range(n):
			if a[i][j] < c[i + 1]:
				c[i] = max(c[i], a[i][j])
		if c[i] == 0:
			f = 1
			break
		else:
			s += c[i]
	if f == 1:
		print(-1)
	else:
		print(s + c[n - 1])


for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		x = list(map(int, input().split()))
		x.sort()
		a.append(x)
	m = max(a[-1])
	s = m
	c = 1
	for i in range(n - 2, -1, -1):
		for j in range(len(a[i]) - 1, -1, -1):
			if a[i][j] < m:
				m = a[i][j]
				s += m
				c += 1
				break
	if c == n:
		print(s)
	else:
		print(-1)


T = int(input())
for _ in range(T):
	n = int(input())
	seq = []
	score_list = []
	for _ in range(n):
		res = list(map(int, input().split()))
		seq.append(res)
	seq.reverse()
	for l in seq:
		if len(score_list) == 0:
			score_list.append(max(l))
		else:
			l.sort(reverse=True)
			for i in l:
				if i < score_list[len(score_list) - 1]:
					score_list.append(i)
					break
				elif i == l[len(l) - 1] and i >= score_list[len(score_list) - 1]:
					score_list.append(-1)
	if min(score_list) == -1:
		print(-1)
	else:
		print(sum(score_list))


t = int(input())
for T in range(t):
	l = []
	n = int(input())
	c = 1
	for i in range(n):
		a = [int(x) for x in input().split()]
		a.sort()
		l.append(a)
	ans = l[n - 1][n - 1]
	ele = ans
	for i in range(len(l) - 2, -1, -1):
		c = 1
		for j in range(n - 1, -1, -1):
			if l[i][j] < ele:
				ele = l[i][j]
				ans += ele
				c = 0
				break
		if c == 1:
			ans = -1
			break
	print(ans)


for i in range(int(input())):
	a = int(input())
	b = []
	c = []
	cnt = 0
	fl = 0
	for i in range(a):
		b = list(map(int, input().split()))
		c.append(b)
	mx = max(c[-1])
	sm = mx
	for i in range(a - 2, -1, -1):
		d = []
		for j in c[i]:
			if j < mx:
				d.append(j)
		if len(d) > 0:
			mx = max(d)
		else:
			fl = -1
		sm += mx
	if fl == 0:
		print(sm)
	else:
		print(-1)


for i in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		x = list(map(int, input().split()))
		l.append(x)
	max1 = max(l[len(l) - 1])
	sum1 = max1
	c = len(l) - 2
	while c >= 0:
		z = l[c]
		z.sort(reverse=True)
		p = 0
		for i in z:
			if i < max1:
				sum1 += i
				max1 = i
				p = 1
				break
		if p == 0:
			sum1 = -1
			break
		c -= 1
	print(sum1)


t = int(input())
while t:
	n = int(input())
	l = []
	for i in range(n):
		l1 = list(map(int, input().split()))
		l1 = sorted(l1)
		l.append(l1)
	count = l[n - 1][n - 1]
	m = l[n - 1][n - 1]
	c = 0
	k = 0
	for i in range(n - 2, -1, -1):
		for j in range(n - 1, -1, -1):
			if l[i][j] < m:
				count += l[i][j]
				m = l[i][j]
				c += 1
				break
		if c == 0:
			print(-1)
			k += 1
			break
		else:
			c -= 1
	if k == 0:
		print(count)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = []
	if n == 1:
		s = int(input())
		print(s)
	else:
		for i in range(n):
			a = list(map(int, input().split()))
			a.sort()
			l.append(a)
		m = l[-1][-1]
		flag = 1
		sum = m
		for j in range(n - 2, -1, -1):
			b = False
			for k in range(n - 1, -1, -1):
				if l[j][k] < m:
					m = l[j][k]
					sum += m
					b = True
					break
			if b == False:
				break
		if not b:
			print(-1)
		else:
			print(sum)


for _ in range(int(input())):
	n = int(input())
	lis = []
	for i in range(n):
		a = list(map(int, input().split()))
		lis.append(a)
	for i in range(n):
		lis[i].sort()
	sum = lis[n - 1][n - 1]
	tmp = lis[n - 1][n - 1]
	f = 0
	g = 0
	i = n - 2
	while i >= 0:
		j = n - 1
		f = 0
		while j >= 0:
			if lis[i][j] < tmp:
				sum += lis[i][j]
				tmp = lis[i][j]
				f = 1
				break
			j -= 1
		if f == 0:
			g = 1
			break
		i -= 1
	if g == 0:
		print(sum)
	else:
		print('-1')


def function(a, N):
	acc = max(a[N - 1])
	en = acc
	for i in range(N - 2, -1, -1):
		max_yet = -1
		for elem in a[i]:
			if elem > max_yet and elem < en:
				max_yet = elem
		if max_yet == -1:
			print(-1)
			return
		en = max_yet
		acc += en
	print(acc)
T = int(input())
for testcase in range(T):
	N = int(input())
	a = []
	for i in range(N):
		ai = list(map(int, input().split(' ')))
		ai.sort()
		a.append(ai)
	function(a, N)


T = int(input())
for _ in range(T):
	N = int(input())
	l = []
	S = 0
	f = 0
	for __ in range(N):
		l.append(list(map(int, input().split())))
	y = max(l[N - 1])
	S += y
	for i in range(N - 2, -1, -1):
		c = 0
		for j in range(0, N):
			if l[i][j] > c and l[i][j] < y:
				c = l[i][j]
		if c == 0:
			f += 1
			break
		else:
			S += c
			y = c
	if f > 0:
		print('-1')
	else:
		print(S)


t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	for k in range(n):
		a = [int(z) for z in input().split(' ')]
		a.sort()
		arr.append(a)
	old = 10 ** 9 + 1
	c = 0
	sumo = 0
	for k in range(n - 1, -1, -1):
		for z in range(n - 1, -1, -1):
			val = arr[k][z]
			if val < old:
				sumo += val
				c += 1
				old = val
				break
	if c == n:
		print(sumo)
	else:
		print(-1)


T = int(input())
for i in range(T):
	N = int(input())
	c = []
	for j in range(N):
		s = list(map(int, input().split()))
		s.sort()
		c.append(s)
	t = 1
	k = max(c[N - 1])
	r = k
	for i in range(N - 2, -1, -1):
		for j in range(N - 1, -1, -1):
			if k > c[i][j]:
				k = c[i][j]
				t += 1
				r += k
				break
	if t == N:
		print(r)
	else:
		print(-1)


def fun(a, n):
	t = max(a[n - 1])
	s = 0
	s += t
	for i in range(n - 2, -1, -1):
		l = []
		for j in range(n):
			if t > a[i][j]:
				l.append(a[i][j])
		if l == []:
			return -1
		t = max(l)
		s += t
	return s
t = int(input())
for p in range(t):
	n = int(input())
	a = []
	for i in range(n):
		a.append(list(map(int, input().strip().split()))[:n])
	print(fun(a, n))


for u in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l.append(sorted(list(map(int, input().split())), reverse=True))
	m = max(l[n - 1])
	s = m
	d = 0
	for i in range(n - 2, -1, -1):
		f = 0
		for j in range(n):
			if l[i][j] < m:
				s += l[i][j]
				m = l[i][j]
				f = 1
				break
		if f == 0:
			d = 1
			break
	if d == 0:
		print(s)
	else:
		print(-1)