You are given an array A of size N. In one operation, you can:
Choose an index i (1≤ i ≤ N) and increase A_{i} by 1.

Find the minimum number of operations required to convert the array A into a *permutation* of size N. If it is impossible to do so, print -1.

Note that a *permutation* of size N contains each element from 1 to N exactly once.

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

- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains the integer N — the size of the array.
- The next line contains N space-separated integers, the elements of the array A.

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

For each test case, output on a new line, the minimum number of operations required to convert the array A into a *permutation* of size N.  
If it is impossible to do so, print -1.

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

$1 ≤ T ≤ 500$
$1 ≤ N ≤ 1000$
$0 ≤ A_{i} ≤ 1000$

----- Sample Input 1 ------ 
4
4
3 1 1 2
3
0 3 3
3
3 2 1
3
2 0 1

----- Sample Output 1 ------ 
3
-1
0
3
----- explanation 1 ------ 
Test case $1$: We can convert the array $A$ into a permutation using $3$ operations:
- Operation $1$: Choose $i = 3$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 1, 2, 2]$.
- Operation $2$: Choose $i = 3$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 1, 3, 2]$.
- Operation $3$: Choose $i = 3$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 1, 4, 2]$.

It can be shown that this is the minimum number of operations required to convert $A$ into a permutation.

Test case $2$: The given array cannot be converted into a permutation using any number of operations.

Test case $3$: The given array is already a permutation. Thus, we require $0$ operations.

Test case $4$: We can convert the array $A$ into a permutation using $3$ operations:
- Operation $1$: Choose $i = 1$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 0, 1]$.
- Operation $2$: Choose $i = 2$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 1, 1]$.
- Operation $3$: Choose $i = 3$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 1, 2]$.

It can be shown that this is the minimum number of operations required to convert $A$ into a permutation.

Source: codechef | Difficulty: EASY
Tags: Sorting


Solutions:
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()][:n]
	a.sort()
	max_money = [int(i) for i in range(1, n + 1)]
	count = 0
	flag = True
	for (i, j) in zip(a, max_money):
		if i == j:
			pass
		elif i < j:
			count += j - i
		else:
			flag = False
	if flag:
		print(count)
	else:
		print(-1)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	a = []
	for i in range(1, n + 1):
		a.append(i)
	flag = 1
	c = 0
	for i in range(n):
		t = a[i] - l[i]
		if t >= 0:
			c += t
		else:
			flag = 0
	if flag == 1:
		print(c)
	else:
		print('-1')


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split(' ')))
	l.sort()
	l3 = []
	for j in range(1, n + 1):
		l3.append(j)
	flag = 1
	sum = 0
	for j in range(n):
		o = l3[j] - l[j]
		if o >= 0:
			sum = sum + o
		else:
			flag = 0
	if flag == 1:
		print(sum)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	integers = list(map(int, input().split()))
	integers.sort()
	count = 0
	for i in range(n):
		if integers[i] == i + 1:
			continue
		elif integers[i] < i + 1:
			count += i + 1 - integers[i]
		else:
			count = -1
			break
	print(count)


for t in range(int(input())):
	N = int(input())
	A = [int(i) for i in input().split()]
	A.sort()
	k = 0
	flag = 0
	for i in range(N):
		if A[i] > i + 1:
			flag = -1
			print(-1)
			break
		k += i + 1 - A[i]
	if flag == 0:
		print(k)


for i in range(int(input())):
	m = int(input())
	l = list(map(int, input().split(' ')))
	l.sort()
	l2 = []
	for j in range(1, m + 1):
		l2.append(j)
	flag = 1
	sum = 0
	for j in range(m):
		t = l2[j] - l[j]
		if t >= 0:
			sum = sum + t
		else:
			flag = 0
	if flag == 1:
		print(sum)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	a = a[::-1]
	(l, i) = ([], 0)
	while n != 0:
		temp = n - a[i]
		if temp < 0:
			print(-1)
			break
		else:
			l.append(temp)
			i += 1
			n -= 1
	else:
		print(sum(l))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	b = []
	for j in range(1, n + 1):
		b.append(j)
	flag = 1
	c = 0
	for k in range(n):
		d = b[k] - a[k]
		if d >= 0:
			c += d
		else:
			flag = 0
	if flag == 1:
		print(c)
	else:
		print(-1)


for i in range(int(input())):
	a = int(input())
	l = list(map(int, input().split(' ')))
	l.sort()
	k = []
	for j in range(1, a + 1):
		k.append(j)
	flag = 1
	count = 0
	for h in range(a):
		t = k[h] - l[h]
		if t >= 0:
			count = count + t
		else:
			flag = 0
	if flag == 1:
		print(count)
	else:
		print('-1')


T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	A.sort()
	R = 0
	for (k, a) in enumerate(A):
		if a > k + 1:
			R = -1
			break
		else:
			R += k + 1 - a
	print(R)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	per = 0
	for i in range(0, n):
		if a[i] > i + 1:
			per = -1
			break
		else:
			per += i - a[i] + 1
	print(per)


t = int(input())
for j in range(t):
	n = int(input())
	perm = input()
	data = perm.split(' ')
	data = [int(i) for i in data]
	data.sort()
	ans = 0
	flag = True
	for i in range(n):
		if int(data[i]) > i + 1:
			flag = False
			break
		else:
			ans += i + 1 - int(data[i])
	if flag:
		print(ans)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	target = [i for i in range(1, n + 1)]
	impossible = False
	count = 0
	for i in range(n):
		diff = target[i] - l[i]
		if diff < 0:
			impossible = True
			break
		else:
			count += diff
	if impossible:
		print(-1)
	else:
		print(count)


x = int(input())
final = []
for th in range(0, x):
	p = int(input())
	jk = str(input())
	bcf = jk.split(' ')
	uup = []
	for hhd in bcf:
		uup.append(int(hhd))
	final.append(uup)
for i in final:
	i.sort()
	rew = len(i)
	ued = []
	ap = []
	for mmc in i:
		if mmc not in ued and mmc != 0 and (mmc <= rew):
			ued.append(mmc)
		else:
			ap.append(mmc)
	k = []
	for yye in range(1, len(i) + 1):
		if yye not in ued:
			k.append(yye)
	if len(k) > len(ap) or len(ap) > len(k):
		print(-1)
	else:
		ans = 0
		mtr = 0
		for iije in range(0, len(k)):
			upr = k[iije] - ap[iije]
			if upr < 0:
				mtr = 1
				break
			else:
				ans += upr
		if mtr == 1:
			print(-1)
		else:
			print(ans)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = []
	s = 0
	k = False
	for i in range(n):
		m.append(i + 1)
	l.sort()
	for i in range(n):
		if m[i] - l[i] < 0:
			k = True
			break
		else:
			s += m[i] - l[i]
	if k == True:
		print(-1)
	else:
		print(s)


for _ in range(0, int(input())):
	x = int(input())
	l = list(map(int, input().split()))
	l.sort()
	l1 = []
	for i in range(1, x + 1):
		l1.append(i)
	flag = 1
	sum = 0
	for j in range(x):
		t = l1[j] - l[j]
		if t >= 0:
			sum = sum + t
		else:
			flag = 0
	if flag == 1:
		print(sum)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	sum1 = 0
	for i in range(n):
		if arr[i] == i + 1:
			continue
		elif arr[i] < i + 1:
			sum1 += i + 1 - arr[i]
		else:
			sum1 = -1
			break
	print(sum1)


t = int(input())
for i in range(t):
	n = int(input())
	arr = input().split(' ')
	for j in range(n):
		arr[j] = int(arr[j])
	arr.sort()
	steps = 0
	for x in range(n):
		if arr[x] > x + 1:
			print(-1)
			break
		elif arr[x] < x + 1:
			steps += x + 1 - arr[x]
	else:
		print(steps)


try:
	t = int(input())
	for r in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		b = sorted(a)
		c = 0
		d = len(b)
		for i in range(d):
			if b[i] != i + 1 and b[i] < i + 1:
				c += abs(b[i] - (i + 1))
			elif b[i] > i + 1:
				c = -1
				break
		print(c)
except:
	pass


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	l2 = []
	flag = True
	sum = 0
	for i in range(1, n + 1):
		l2.append(i)
	for i in range(n):
		t = l2[i] - l[i]
		if t >= 0:
			sum += t
		else:
			flag = False
	if flag:
		print(sum)
	else:
		print(-1)


t = int(input())
for tc in range(t):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	b = []
	for j in range(1, n + 1):
		b.append(j)
	c = 1
	s = 0
	for j in range(n):
		m = b[j] - a[j]
		if m >= 0:
			s += m
		else:
			c = 0
	if c == 0:
		print('-1')
	else:
		print(s)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	if arr.count(n) > 1 or max(arr) > n:
		print(-1)
	else:
		count = 0
		val = True
		for i in range(n):
			if arr[i] > i + 1:
				val = False
				break
			else:
				count += i + 1 - arr[i]
		if val:
			print(count)
		else:
			print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	flag = 0
	sum = 0
	for j in range(len(a)):
		if a[j] > j + 1:
			flag = 1
			break
	for j in range(len(a)):
		sum = sum + j + 1 - a[j]
	if flag == 1:
		print(-1)
	else:
		print(sum)


def result(l1):
	(diff, count) = (0, 0)
	for i in range(1, len(l1) + 1):
		if i > l1[i - 1]:
			diff = i - l1[i - 1]
			count += diff
		elif i < l1[i - 1]:
			count = -1
			break
	return count
for i in range(int(input())):
	n = int(input())
	l = list(sorted(map(int, input().split())))
	print(result(l))


t = int(input())
for i in range(t):
	n = int(input())
	arr = sorted(list(map(int, input().split())))
	req = 1
	(count, flag) = (0, 0)
	for ele in range(n):
		if arr[ele] < req:
			count += req - arr[ele]
		elif arr[ele] > req:
			flag = 1
			print(-1)
			break
		req += 1
	if flag == 0:
		print(count)


h = int(input())
for _ in range(h):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	req = 1
	c = 0
	flag = 0
	for i in range(len(l)):
		if l[i] < req:
			c += req - l[i]
		elif l[i] > req:
			flag = 1
			print(-1)
			break
		req += 1
	if flag == 0:
		print(c)


def convertToPermutation(lst):
	count = 0
	for i in range(N):
		if A[i] <= i + 1:
			count += i + 1 - A[i]
		else:
			return -1
	return count
T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	A.sort()
	print(convertToPermutation(A))


t = int(input())
for i in range(t):
	n = int(input())
	a = sorted([int(i) for i in input().split()])
	p = [int(i) for i in range(1, n + 1)]
	for i in range(n):
		if a[i] > i + 1:
			print(-1)
			break
		if i == n - 1:
			print(sum(p) - sum(a))


def sn(n):
	p = 0
	for i in range(1, n + 1):
		p += i
	return p
t = int(input())
for i in range(t):
	n = int(input())
	a = sorted([int(i) for i in input().split()])
	p = False
	for i in range(n):
		if a[i] > i + 1:
			p = True
			print(-1)
			break
	if p == False:
		print(sn(n) - sum(a))


for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	b.sort()
	l = [int(x) for x in range(1, a + 1)]
	flag = 1
	sum = 0
	for j in range(a):
		t = l[j] - b[j]
		if t >= 0:
			sum = sum + t
		else:
			flag = 0
	if flag == 1:
		print(sum)
	else:
		print(-1)


def ans():
	n = int(input())
	a = [int(A) for A in input().split()]
	a.sort()
	count = 0
	for i in range(len(a)):
		if a[i] <= i + 1:
			count += i + 1 - a[i]
		else:
			return -1
	return count
for t in range(int(input())):
	print(ans())


t = int(input())
for i in range(t):
	n = int(input())
	L = sorted(list(map(int, input().split())))
	count = 0
	for j in range(len(L)):
		if L[j] > j + 1:
			count = -1
			break
		elif L[j] <= j + 1:
			count += j + 1 - L[j]
	print(count)


for i in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	l = [j for j in range(1, n + 1)]
	flag = 1
	sum = 0
	for k in range(0, len(A)):
		t = l[k] - A[k]
		if t >= 0:
			sum = sum + t
		else:
			flag = 0
	if flag == 1:
		print(sum)
	else:
		print(-1)


def solve(A, n):
	if A.count(n) >= 2:
		return -1
	A.sort()
	(n1, j) = (0, 0)
	for i in A:
		j += 1
		if i > j:
			return -1
		n1 += abs(i - j)
	return n1
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))[:n]
	print(solve(A, n))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	count = 0
	for x in range(n):
		if a[x] > x + 1:
			print('-1')
			break
		elif a[x] == x + 1:
			if x == n - 1:
				print(count)
			continue
		else:
			while a[x] < x + 1:
				a[x] += 1
				count += 1
			if x == n - 1:
				print(count)


def make(l, N):
	ref = []
	for i in range(1, N + 1):
		ref.append(i)
		l.sort()
	for j in range(N):
		if ref[j] < l[j]:
			return -1
	for j in range(N):
		ref[j] = ref[j] - l[j]
	return sum(ref)

def check(l, N):
	ref = []
	for i in range(1, N + 1):
		ref.append(i)
		l = sorted(l)
	return ref == l
n = int(input())
for _ in range(n):
	N = int(input())
	l = list(map(int, input().split()))
	if check(l, N):
		print(0)
	else:
		print(make(l, N))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	l = [x for x in range(1, n + 1)]
	l1 = []
	c = 0
	if max(a) > n:
		print(-1)
	else:
		for i in range(n):
			if c >= 0:
				c = l[i] - a[i]
				l1.append(c)
			else:
				l1 = [-1]
		print(sum(l1))


for i in range(int(input())):
	num = int(input())
	A = list(map(int, input().split()))
	A.sort()
	count = 0
	for i in range(num):
		if i + 1 != A[i]:
			if i + 1 - A[i] > 0:
				count += i + 1 - A[i]
			else:
				count = -1
				break
	print(count)


for _ in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	num.sort()
	steps = 0
	for i in range(1, n + 1):
		if i - num[i - 1] < 0:
			print(-1)
			break
		steps += i - num[i - 1]
	else:
		print(steps)


def check(n, l):
	count = [0] * (n + 1)
	for i in l:
		if i > n:
			return False
		count[i] += 1
		if count[i] > n - i + 1:
			return False
	ff = count[0]
	for i in range(1, n + 1):
		ff += count[i]
		ff -= 1
		if ff < 0:
			return False
	return True
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().strip().split()))
	x = check(n, l)
	ans = sum(l)
	if x == False:
		print(-1)
	else:
		print(n * (n + 1) // 2 - ans)


def sgcd(a, b):
	if a == b:
		return a
	if a == 0:
		return b
	if b == 0:
		return a
	if a % 2 == 0:
		if b % 2 == 0:
			return 2 * sgcd(a / 2, b / 2)
		else:
			return sgcd(a / 2, b)
	if b % 2 == 0:
		return sgcd(a, b / 2)
	else:
		return sgcd(abs(a - b) / 2, min(a, b))

def egcd(a, b):
	if b == 0:
		return a
	else:
		A = egcd(b, a % b)
	return A

def lcm(a, b):
	try:
		return int(a * b / sgcd(a, b))
	except:
		return a * b / egcd(a, b)

def I(a):
	return map(int, a.split(' '))

def main():
	for i in range(int(input())):
		a = int(input())
		l = list(I(input()))
		l.sort()
		f = 0
		for i in range(a):
			if i + 1 < l[i]:
				print(-1)
				f = -1
				break
			f += i + 1 - l[i]
		if f != -1:
			print(f)
main()


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


T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	c = 0
	for i in range(n):
		if A[i] > i + 1:
			c = -1
			break
		c += i + 1 - A[i]
	print(c)


def function(a, n):
	if a[-1] > n:
		return -1
	else:
		count = 0
		for i in range(n):
			k = i + 1
			j = k - a[i]
			if j >= 0:
				count += j
			else:
				return -1
		return count
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	print(function(a, n))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	c = 0
	flag = 0
	for i in range(n):
		if l[i] == i + 1:
			continue
		if l[i] < i + 1:
			c += i + 1 - l[i]
		else:
			c = -1
			break
	print(c)


import numpy as np
for _ in range(int(input())):
	N = int(input())
	A = np.array(list(map(int, input().split())))
	A.sort()
	count = 0
	for i in range(N):
		diff = i + 1 - A[i]
		if diff < 0:
			count = -1
			break
		count += diff
	print(count)


for i in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	lst.sort()
	x = 0
	for i in range(1, n + 1):
		if lst[i - 1] <= i:
			x += i - lst[i - 1]
		else:
			print(-1)
			break
	else:
		print(x)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	count = 0
	i = 0
	while i < n:
		if a[i] < i + 1:
			count += i + 1 - a[i]
		if a[i] > i + 1:
			count = -1
			break
		i += 1
	print(count)


def ConverttoPermutation(arr, n):
	arr.sort()
	result = 0
	for _ in range(0, N):
		if arr[_] == _ + 1:
			continue
		elif arr[_] < _ + 1:
			result += _ + 1 - arr[_]
		else:
			result = -1
			break
	return result
for _ in range(int(input())):
	N = int(input())
	Array = list(map(int, input().split()))[:N]
	print(ConverttoPermutation(Array, N))


t = int(input())
while t > 0:
	n = input()
	a = list(map(int, input().split(' ')))
	a.sort()
	count = 0
	for i in range(len(a)):
		if a[i] <= i + 1:
			count += abs(a[i] - (i + 1))
		else:
			count = -1
			break
	print(count)
	t -= 1


t = int(input())
while t != 0:
	n = int(input())
	l = list(map(int, input().strip().split()))[:n]
	l.sort()
	if l.count(n) > 1:
		print('-1')
	else:
		n1 = j = 0
		for i in l:
			j += 1
			if i > j:
				n1 = -1
				break
			else:
				n1 += abs(i - j)
		print(n1)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))[:n]
	c = 0
	l.sort()
	for i in range(n):
		if l[i] < i + 1:
			c += i + 1 - l[i]
		elif l[i] > i + 1:
			c = -1
			break
	print(c)


n = int(input())
for i in range(n):
	length = int(input())
	ins = sorted(list(map(int, input().split(' '))))
	nums = []
	changes = 0
	perm = True
	for i in range(1, length + 1):
		nums.append(i)
	for i in range(length):
		diff = nums[i] - ins[i]
		if diff >= 0:
			ins[i] = nums[i]
			changes += diff
		else:
			perm = False
	if perm:
		print(changes)
	else:
		print(-1)


t = int(input(''))
for itera in range(t):
	n = int(input(''))
	a1 = input().split()
	a = []
	sum = flag = 0
	for i in a1:
		a.append(int(i))
	for i in range(1, n + 1):
		if flag == 0:
			if i in a:
				a.remove(i)
			else:
				for j in a:
					if j < i:
						sum += i - j
						a.remove(j)
						break
				else:
					flag = 1
		else:
			break
	if flag == 0:
		print(sum)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	permute = list(map(int, input().split()))
	permute.sort()
	ans = 0
	flag = 0
	for i in range(1, n + 1):
		if i == permute[i - 1]:
			continue
		elif i > permute[i - 1]:
			ans += i - permute[i - 1]
		elif permute[i - 1] > i:
			flag = 1
			break
	if flag == 0:
		print(ans)
	elif flag == 1:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	flag = 0
	for i in range(len(a)):
		if a[i] > i + 1:
			flag = 1
			break
	if flag:
		print(-1)
	else:
		print(n * (n + 1) // 2 - sum(a))


T = int(input())
while T:
	T -= 1
	n = int(input())
	a = list(map(int, input().split()))
	a.sort(reverse=True)
	m = n
	c = 0
	for i in a:
		if i < m:
			c += m - i
		elif i > m:
			print(-1)
			break
		m -= 1
	else:
		print(c)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	f = 1
	for i in range(n):
		if l[i] > i + 1:
			f = 0
			break
	print(n * (n + 1) // 2 - sum(l)) if f else print(-1)


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


for i in range(int(input())):
	n = int(input())
	a = [int(num) for num in input().split()]
	count = 0
	a.sort()
	for l in range(n):
		if a[l] == l + 1:
			pass
		elif a[l] < l + 1:
			count = count + l + 1 - a[l]
		else:
			count = -1
			break
	print(count)


for i in range(int(input())):
	n = int(input())
	l = [int(num) for num in input().split()]
	num = -1
	c = 0
	l.sort()
	flag = 't'
	for j in range(0, len(l)):
		if l[j] == j + 1:
			pass
		elif l[j] > j + 1:
			print(num)
			flag = 'f'
			break
		elif l[j] < j + 1:
			c = c + (j + 1 - l[j])
	if flag == 't':
		print(c)


T = int(input())
for t in range(T):
	N = int(input())
	A = sorted(list(map(int, input().split())))
	count = 0
	for (i, k) in zip(range(1, N + 1), A):
		if k > i:
			count = -1
			break
		elif k < i:
			count += i - k
		else:
			count += 0
	print(count)


for i in range(int(input())):
	n = int(input())
	count = 0
	flag = 0
	arr = list(map(int, input().split()))
	arr.sort()
	for i in range(len(arr)):
		if arr[i] != i + 1:
			t = arr[i]
			if i + 1 - t < 0:
				arr[i] += 1
			else:
				arr[i] = i + 1
			count += i + 1 - t
	for i in range(len(arr)):
		if arr[i] != i + 1:
			flag = 1
			break
	if flag == 1:
		print(-1)
	else:
		print(count)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	result = 0
	for i in range(n):
		if a[i] > i + 1:
			print(-1)
			break
		result += max(0, i + 1 - a[i])
	else:
		print(result)


n = int(input())
for i in range(n):
	x = int(input())
	list_1 = list(map(int, input().split()))
	list_1.sort()
	count = 0
	for j in range(1, x + 1):
		if j - list_1[j - 1] >= 0:
			count += j - list_1[j - 1]
		else:
			count = -1
			break
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	k = n * (n + 1) // 2
	req = 1
	for j in l:
		if j > req:
			print(-1)
			break
		req += 1
	else:
		print(k - sum(l))