Paprika loves permutations. She has an array $a_1, a_2, \dots, a_n$. She wants to make the array a permutation of integers $1$ to $n$.

In order to achieve this goal, she can perform operations on the array. In each operation she can choose two integers $i$ ($1 \le i \le n$) and $x$ ($x > 0$), then perform $a_i := a_i mod x$ (that is, replace $a_i$ by the remainder of $a_i$ divided by $x$). In different operations, the chosen $i$ and $x$ can be different.

Determine the minimum number of operations needed to make the array a permutation of integers $1$ to $n$. If it is impossible, output $-1$.

A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).


-----Input-----

Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer $n$ ($1 \le n \le 10^5$).

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

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.


-----Output-----

For each test case, output the minimum number of operations needed to make the array a permutation of integers $1$ to $n$, or $-1$ if it is impossible.


-----Examples-----

Input
4
2
1 7
3
1 5 4
4
12345678 87654321 20211218 23571113
9
1 2 3 4 18 19 5 6 7
Output
1
-1
4
2


-----Note-----

For the first test, the only possible sequence of operations which minimizes the number of operations is:

Choose $i=2$, $x=5$. Perform $a_2 := a_2 mod 5 = 2$.

For the second test, it is impossible to obtain a permutation of integers from $1$ to $n$.

Source: codeforces | Difficulty: EASY
Tags: Sorting

Mathematics

Greedy algorithms


Solutions:
import sys
from math import *
from math import *
from functools import *
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split())) + [0]
	b = [-1] * (n + 1)
	a.sort()
	for i in a:
		if i <= n:
			b[i] += 1
	c = 0
	j = 1
	i = 1
	while j <= n:
		if b[j] < 0:
			if a[i] > n or b[a[i]]:
				if a[i] <= n:
					b[a[i]] -= 1
				if a[i] > 2 * j:
					c += 1
					j += 1
					i += 1
				else:
					print(-1)
					break
			elif b[a[i]] == 0:
				i += 1
		else:
			j += 1
	else:
		print(c)


for _ in range(int(input())):
	n = int(input())
	state = {i: False for i in range(1, n + 1)}
	toform = []
	for num in (*map(int, input().split()),):
		if num in state and (not state[num]):
			state[num] = True
		else:
			toform.append(num)
	if any((y >= x / 2 for (y, x) in zip((i for i in range(1, n + 1) if not state[i]), sorted(toform)))):
		print(-1)
	else:
		print(len(toform))


T = int(input())
for x in range(T):
	N = int(input())
	A = [int(d) for d in input().split()]
	used = [0] * (N + 1)
	pt = 1
	A.sort()
	ans = 0
	done = False
	for a in A:
		while used[pt] == 1:
			pt += 1
		if a <= N and used[a] == 0:
			used[a] = 1
		elif int((a - 1) / 2) >= pt:
			used[pt] = 1
			ans += 1
		else:
			print('-1')
			done = True
			break
	if not done:
		print(ans)


def f(a):
	bad = []
	n = len(a)
	vis = [-1] * (len(a) + 1)
	nt = []
	for i in a:
		if i > n or vis[i] != -1:
			bad.append(i)
		else:
			vis[i] = 1
	for i in range(1, n + 1):
		if vis[i] == -1:
			nt.append(i)
	nt.sort(reverse=True)
	bad.sort(reverse=True)
	ans = len(bad)
	for val in bad:
		id = None
		for i in range(len(nt)):
			if val % (val - nt[i]) == nt[i]:
				id = i
				break
		if id == None:
			return -1
		nt.pop(id)
	return ans
for _ in range(int(input())):
	a = input()
	l = list(map(int, input().strip().split()))
	print(f(l))


from __future__ import print_function
from math import *
from collections import deque
import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

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

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

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

def invr():
	return map(int, input().split())
for _ in range(inp()):
	n = inp()
	l = inlt()
	s = SortedList()
	for i in l:
		s.add(i)
	f = 1
	ans = 0
	v = [0] * n
	for i in range(1, n + 1):
		if i in s:
			s.remove(i)
			v[i - 1] = 1
	for i in range(1, n + 1):
		if v[i - 1] == 1:
			continue
		x = 2 * i + 1
		a = s.bisect_left(x)
		if a == len(s):
			f = 0
			break
		ans += 1
		s.remove(s[a])
	if not f:
		print(-1)
	else:
		print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
import io
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
from collections import Counter
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=10 ** 18, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree1:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
MOD = 10 ** 9 + 7

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(10001)]
prime[0] = prime[1] = False

def SieveOfEratosthenes(n=10000):
	p = 2
	c = 0
	while p <= n:
		if prime[p] == True:
			c += 1
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1

class DSU:

	def __init__(self, R, C):
		self.par = range(R * C + 1)
		self.rnk = [0] * (R * C + 1)
		self.sz = [1] * (R * C + 1)

	def find(self, x):
		if self.par[x] != x:
			self.par[x] = self.find(self.par[x])
		return self.par[x]

	def union(self, x, y):
		(xr, yr) = (self.find(x), self.find(y))
		if xr == yr:
			return
		if self.rnk[xr] < self.rnk[yr]:
			(xr, yr) = (yr, xr)
		if self.rnk[xr] == self.rnk[yr]:
			self.rnk[xr] += 1
		self.par[yr] = xr
		self.sz[xr] += self.sz[yr]

	def size(self, x):
		return self.sz[self.find(x)]

	def top(self):
		return self.size(len(self.sz) - 1) - 1

class LazySegTree:

	def __init__(self, _op, _e, _mapping, _composition, _id, v):

		def set(p, x):
			assert 0 <= p < _n
			p += _size
			for i in range(_log, 0, -1):
				_push(p >> i)
			_d[p] = x
			for i in range(1, _log + 1):
				_update(p >> i)

		def get(p):
			assert 0 <= p < _n
			p += _size
			for i in range(_log, 0, -1):
				_push(p >> i)
			return _d[p]

		def prod(l, r):
			assert 0 <= l <= r <= _n
			if l == r:
				return _e
			l += _size
			r += _size
			for i in range(_log, 0, -1):
				if l >> i << i != l:
					_push(l >> i)
				if r >> i << i != r:
					_push(r >> i)
			sml = _e
			smr = _e
			while l < r:
				if l & 1:
					sml = _op(sml, _d[l])
					l += 1
				if r & 1:
					r -= 1
					smr = _op(_d[r], smr)
				l >>= 1
				r >>= 1
			return _op(sml, smr)

		def apply(l, r, f):
			assert 0 <= l <= r <= _n
			if l == r:
				return
			l += _size
			r += _size
			for i in range(_log, 0, -1):
				if l >> i << i != l:
					_push(l >> i)
				if r >> i << i != r:
					_push(r - 1 >> i)
			l2 = l
			r2 = r
			while l < r:
				if l & 1:
					_all_apply(l, f)
					l += 1
				if r & 1:
					r -= 1
					_all_apply(r, f)
				l >>= 1
				r >>= 1
			l = l2
			r = r2
			for i in range(1, _log + 1):
				if l >> i << i != l:
					_update(l >> i)
				if r >> i << i != r:
					_update(r - 1 >> i)

		def _update(k):
			_d[k] = _op(_d[2 * k], _d[2 * k + 1])

		def _all_apply(k, f):
			_d[k] = _mapping(f, _d[k])
			if k < _size:
				_lz[k] = _composition(f, _lz[k])

		def _push(k):
			_all_apply(2 * k, _lz[k])
			_all_apply(2 * k + 1, _lz[k])
			_lz[k] = _id
		_n = len(v)
		_log = _n.bit_length()
		_size = 1 << _log
		_d = [_e] * (2 * _size)
		_lz = [_id] * _size
		for i in range(_n):
			_d[_size + i] = v[i]
		for i in range(_size - 1, 0, -1):
			_update(i)
		self.set = set
		self.get = get
		self.prod = prod
		self.apply = apply
MIL = 1 << 20

def makeNode(total, count):
	return total * MIL + count

def getTotal(node):
	return math.floor(node / MIL)

def getCount(node):
	return node - getTotal(node) * MIL
nodeIdentity = makeNode(0.0, 0.0)

def nodeOp(node1, node2):
	return node1 + node2
	return makeNode(getTotal(node1) + getTotal(node2), getCount(node1) + getCount(node2))
identityMapping = -1

def mapping(tag, node):
	if tag == identityMapping:
		return node
	count = getCount(node)
	return makeNode(tag * count, count)

def composition(mapping1, mapping2):
	return mapping1 if mapping1 != identityMapping else mapping2

class LazySegUpdate:

	def __init__(self, N):
		self.N = N
		n = 4 * N
		self.tree = [0] * n
		self.lazy = [0] * n

	def queryHelper(self, index, s, e, l, r):
		if self.lazy[index]:
			update = self.lazy[index]
			self.lazy[index] = 0
			self.tree[index] = max(self.tree[index], update)
			if s != e:
				self.lazy[2 * index] = max(self.lazy[2 * index], update)
				self.lazy[2 * index + 1] = max(self.lazy[2 * index + 1], update)
		if s > e or s > r or e < l:
			return 0
		if s >= l and e <= r:
			return self.tree[index]
		mid = (s + e) // 2
		return max(self.queryHelper(2 * index, s, mid, l, r), self.queryHelper(2 * index + 1, mid + 1, e, l, r))

	def updateHelper(self, index, s, e, l, r, h):
		if self.lazy[index]:
			update = self.lazy[index]
			self.lazy[index] = 0
			self.tree[index] = max(self.tree[index], update)
			if s != e:
				self.lazy[2 * index] = max(self.lazy[2 * index], update)
				self.lazy[2 * index + 1] = max(self.lazy[2 * index + 1], update)
		if s > e or s > r or e < l:
			return
		if s >= l and e <= r:
			self.tree[index] = max(self.tree[index], h)
			if s != e:
				self.lazy[2 * index] = max(self.lazy[2 * index], h)
				self.lazy[2 * index + 1] = max(self.lazy[2 * index + 1], h)
			return
		mid = (s + e) // 2
		self.updateHelper(2 * index, s, mid, l, r, h)
		self.updateHelper(2 * index + 1, mid + 1, e, l, r, h)
		self.tree[index] = max(self.tree[2 * index], self.tree[2 * index + 1])

	def query(self, l, r):
		return self.queryHelper(1, 0, self.N - 1, l, r - 1)

	def update(self, l, r, h):
		self.updateHelper(1, 0, self.N - 1, l, r - 1, h)

def memodict(f):

	class memodict(dict):

		def __missing__(self, key):
			ret = self[key] = f(key)
			return ret
	return memodict().__getitem__

def pollard_rho(n):
	if n & 1 == 0:
		return 2
	if n % 3 == 0:
		return 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			prev = p
			p = p * p % n
			if p == 1:
				return math.gcd(prev - 1, n)
			if p == n - 1:
				break
		else:
			for i in range(2, n):
				(x, y) = (i, (i * i + 1) % n)
				f = math.gcd(abs(x - y), n)
				while f == 1:
					(x, y) = ((x * x + 1) % n, (y * y + 1) % n)
					y = (y * y + 1) % n
					f = math.gcd(abs(x - y), n)
				if f != n:
					return f
	return n

@memodict
def prime_factors(n):
	if n <= 1:
		return Counter()
	f = pollard_rho(n)
	return Counter([n]) if f == n else prime_factors(f) + prime_factors(n // f)

def distinct_factors(n):
	factors = [1]
	for (p, exp) in prime_factors(n).items():
		factors += [p ** i * factor for factor in factors for i in range(1, exp + 1)]
	return factors

def all_factors(n):
	(small, large) = ([], [])
	for i in range(1, int(n ** 0.5) + 1, 2 if n & 1 else 1):
		if not n % i:
			small.append(i)
			large.append(n // i)
	if small[-1] == large[-1]:
		large.pop()
	large.reverse()
	small.extend(large)
	return small

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

def binarySearch(arr, n, key):
	left = 0
	right = n - 1
	mid = 0
	res = n
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] >= key:
			res = mid
			right = mid - 1
		else:
			left = mid + 1
	return res

def binarySearch1(arr, n, key):
	left = 0
	right = n - 1
	mid = 0
	res = -1
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] > key:
			right = mid - 1
		else:
			res = mid
			left = mid + 1
	return res
tt = 1
tt = int(input())
for _ in range(tt):
	n = int(input())
	a = list(map(int, input().split()))
	s = set()
	m = 0
	c = 0
	b = []
	for el in a:
		if el <= n and el not in s:
			s.add(el)
		else:
			c += 1
			b.append(el)
	b.sort()
	m = len(b) - 1
	for i in range(n, 0, -1):
		if i in s:
			continue
		if i > b[m] - b[m] // 2 - 1:
			c = -1
			break
		m -= 1
	print(c)


def half(v):
	h = v // 2
	if h * 2 == v:
		h -= 1
	return h
t = int(input())
for case in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(range(1, n + 1))
	mxs = []
	for v in a:
		if v in s:
			s.remove(v)
		else:
			mxs.append(half(v))
	mxs.sort()
	ok = True
	count = 0
	for i in range(n, 0, -1):
		if i in s:
			if mxs[-1] < i:
				ok = False
				break
			mxs.pop()
			count += 1
	if ok:
		print(count)
	else:
		print(-1)


from bisect import insort, bisect_right, bisect_left
from sys import stdout, stdin, setrecursionlimit
from heapq import heappush, heappop, heapify
from io import BytesIO, IOBase
from collections import *
from itertools import *
from random import *
from string import *
from queue import *
from math import *
from re import *
from os import *

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = read(self._fd, max(fstat(self._fd).st_size, 8192))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = read(self._fd, max(fstat(self._fd).st_size, 8192))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))
(graph, mod, szzz) = ({}, 10 ** 9 + 7, lambda : sorted(zzz()))

def getStr():
	return input()

def getInt():
	return int(input())

def listStr():
	return list(input())

def getStrs():
	return input().split()

def isInt(s):
	return '0' <= s[0] <= '9'

def input():
	return stdin.readline().strip()

def zzz():
	return [int(i) for i in input().split()]

def output(answer, end='\n'):
	stdout.write(str(answer) + end)

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def getPrimes(N=10 ** 5):
	SN = int(sqrt(N))
	sieve = [i for i in range(N + 1)]
	sieve[1] = 0
	for i in sieve:
		if i > SN:
			break
		if i == 0:
			continue
		for j in range(2 * i, N + 1, i):
			sieve[j] = 0
	prime = [i for i in range(N + 1) if sieve[i] != 0]
	return prime

def primeFactor(n, prime=getPrimes()):
	lst = []
	mx = int(sqrt(n)) + 1
	for i in prime:
		if i > mx:
			break
		while n % i == 0:
			lst.append(i)
			n //= i
	if n > 1:
		lst.append(n)
	return lst
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
daysInMounth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
num = getInt()
for _ in range(num):
	n = getInt()
	arr = szzz()
	cnt = [0] * (n + 1)
	unused = []
	for i in arr:
		if i <= n and cnt[i]:
			unused.append(i)
		elif i <= n:
			cnt[i] = 1
		else:
			unused.append(i)
	ans = 0
	rem = []
	for i in range(1, n + 1):
		if cnt[i] == 0:
			rem.append(i)
	while unused:
		x = unused.pop()
		y = rem.pop()
		if (x - 1) // 2 < y:
			ans = False
			break
		else:
			ans += 1
			cnt[y] = 1
	res = True
	for i in range(1, n + 1):
		if cnt[i] == 0:
			res = False
	if res:
		print(ans)
	else:
		print(-1)


import bisect
import collections
import itertools
import math
import sys
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.buffer.readline())

def MI():
	return map(int, sys.stdin.buffer.readline().split())

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def BI():
	return sys.stdin.buffer.readline().rstrip()

def SI():
	return sys.stdin.buffer.readline().rstrip().decode()

def li():
	return [int(i) for i in input().split()]

def lli(rows):
	return [li() for _ in range(rows)]

def si():
	return input()

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

def ins():
	return input().split()

def drank(d, processing, da, rank):
	tmp = 10 ** 9
	if len(d[da]) == 1:
		return 1
	for di in d[da]:
		if processing[di - 1] == 0:
			processing[di - 1] = 1
			tmp = min(tmp, drank(d, processing, di, rank))
			processing[di - 1] = 0
	rank[da - 1] = tmp + 1
	return tmp + 1

def binary_search(n, a):
	l = len(a)
	low = 0
	high = l - 1
	while high >= low:
		mid = (high + low) // 2
		if a[mid] == n:
			return mid
		elif a[mid] > n:
			high = mid - 1
		else:
			low = mid + 1
	return -1
is_prime = [True] * (10 ** 6 + 1)

def primeseive(n):
	is_prime[0] = is_prime[1] = False
	i = 2
	while i * i <= n:
		if is_prime[i] == True:
			for j in range(i * i, n + 1, i):
				is_prime[j] = False
		i += 1

def solve():
	n = ii()
	a = li()
	a.sort()
	x = 0
	ma = [0] * n
	pa = []
	for i in range(n):
		if a[i] < n + 1 and ma[a[i] - 1] == 0:
			ma[a[i] - 1] = 1
		else:
			pa.append(a[i])
	p = [i + 1 for i in range(n) if ma[i] == 0]
	pa.sort()
	for i in range(len(pa)):
		if pa[i] >= 2 * p[i] + 1:
			x += 1
			continue
		return -1
	return x

def main():
	for _ in range(ii()):
		sys.stdout.write(str(solve()) + '\n')
main()


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	lst.sort()
	lst1 = []
	for j in lst:
		if j > n:
			lst1.append(j)
	lstf = []
	se = set()
	lsta = []
	for h in range(1, n + 1):
		se.add(h)
		lsta.append(h)
	sa = set(lsta)
	for g in lst:
		if g in se:
			se.remove(g)
	lstreq = list(se)
	lsthave = []
	for b in lst:
		if b in sa:
			sa.remove(b)
		else:
			lsthave.append(b)
	c = 0
	err = 0
	for v in range(len(lsthave)):
		if lsthave[v] / 2 > lstreq[v]:
			c += 1
		else:
			err += 1
			break
	if err > 0:
		print(-1)
	else:
		print(c)


from sys import stdin, stdout, setrecursionlimit
from io import BytesIO, IOBase
from collections import Counter, deque as dq, defaultdict as dd
setrecursionlimit(10000)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None
		self.BUFSIZE = 8192

	def read(self):
		while True:
			a = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			if not a:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(a), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			a = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			self.newlines = a.count(b'\n') + (not a)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(a), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))
input = lambda : stdin.readline().rstrip('\r\n')
inp = lambda : int(input())
st = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
int_arr = lambda : list(map(int, input().strip().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().strip().split())
get_int = lambda : map(int, input().strip().split())
get_float = lambda : map(float, input().strip().split())
mod = 1000000007
for _ in range(inp()):
	n = inp()
	arr = int_arr()
	ans = []
	(pres, miss) = (dd(int), [])
	for i in range(n):
		if arr[i] > n:
			if arr[i] % 2 != 0:
				ans.append(arr[i] // 2)
			else:
				ans.append(arr[i] // 2 - 1)
		elif pres[arr[i]] == 1:
			if arr[i] % 2 != 0:
				ans.append(arr[i] // 2)
			else:
				ans.append(arr[i] // 2 - 1)
		else:
			pres[arr[i]] = 1
	ind = 0
	tot = 0
	cond = True
	ans.sort()
	l = len(ans)
	for i in range(1, n + 1):
		if not pres[i]:
			if ind < l and ans[ind] >= i:
				tot += 1
				ind += 1
			else:
				cond = False
				break
	if not cond:
		print(-1)
	else:
		print(tot)


import os, sys
from io import BytesIO, IOBase
from types import GeneratorType
from bisect import *
from collections import defaultdict, deque, Counter
import math, string
from heapq import *
from operator import add
from itertools import accumulate
BUFSIZE = 8192
sys.setrecursionlimit(10 ** 5)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
inf = float('inf')
en = lambda x: list(enumerate(x))
ceil_ = lambda a, b: (a + b - 1) // b
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())

def ok(a, b):
	d = a - b
	if d < 1:
		return False
	return a % d == b

def solve():
	n = ii()
	arr = rr()
	arr.sort(reverse=False)
	brr = Counter(arr)
	done = defaultdict(bool)
	for i in arr:
		if i <= n and (not done[i]):
			done[i] = True
			brr[i] -= 1
	crr = []
	for (i, j) in brr.items():
		crr.extend([i] * j)
	crr.sort()
	j = c = 0
	for i in range(1, n + 1):
		if not done[i]:
			if crr[j] <= 2 * i:
				print(-1)
				return
			else:
				done[i] = True
				c += 1
				j += 1
	print(c)
for _ in ' ' * ii():
	solve()


for iii in range(int(input())):
	n = int(input())
	q = list(map(int, input().split()))
	q1 = []
	q2 = [0 for i in range(n + 1)]
	for i in range(n):
		if q[i] <= n and q2[q[i]] == 0:
			q2[q[i]] = 1
		else:
			q1.append(q[i])
	q1.sort()
	if len(q1) == 0:
		print(0)
		continue
	cou = 0
	flag = False
	ind = 0
	for i in range(1, n + 1, 1):
		if q2[i] == 0:
			x = q1[ind] - i
			if q1[ind] % x != i:
				flag = True
			else:
				cou += 1
			ind += 1
		if flag:
			break
	if flag:
		print(-1)
		continue
	print(cou)


from sys import stdin
input = stdin.readline
rn = lambda : int(input())
rns = lambda : map(int, input().split())
rl = lambda : list(map(int, input().split()))
rs = lambda : input().strip()
YN = lambda x: print('YES') if x else print('NO')
ceil_div = lambda a, b: -(-a // b)
mod = 10 ** 9 + 7
for _ in range(rn()):
	n = rn()
	a = rl()
	s = set(range(1, n + 1))
	extra = []
	for i in a:
		if i in s:
			s.remove(i)
		else:
			extra.append(i)
	extra.sort()
	s = sorted(list(s))
	ans = 0
	for i in range(len(extra)):
		if extra[i] / 2 <= s[i]:
			ans = -1
			break
		ans += 1
	print(ans)


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = 1
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	arr = []
	b = []
	d = {}
	for i in a:
		if i > n or i in d:
			arr.append(i)
		d[i] = True
	for i in range(1, n + 1):
		if i not in d:
			b.append(i)
	b.sort()
	arr.sort()
	chk = True
	for (i, j) in zip(b, arr):
		if (j - 1) // 2 < i:
			chk = False
			break
	if chk:
		print(len(arr))
	else:
		print(-1)


import sys
from collections import deque
import heapq
import bisect
import copy
MOD = 1000000007
NO = lambda : print('NO')
YES = lambda : print('YES')
_1 = lambda : print(-1)
ari = lambda : [int(_) for _ in input().split()]
cin = lambda : int(input())
cis = lambda : input()
show = lambda x: print(x)
fast = lambda : sys.stdin.readline()
test_case = 1
test_case = int(input())
sys.setrecursionlimit(10 ** 6)

def ans():
	n = cin()
	arr = ari()
	arr.sort()
	count = 0
	st = set()
	for i in range(1, n + 1):
		st.add(i)
	take = set()
	for i in range(n):
		if arr[i] in st:
			take.add(i)
			st.remove(arr[i])
	newarr = []
	for i in range(n):
		if i not in take:
			newarr.append(arr[i])
	count = 0
	val = sorted(list(set(st)), reverse=True)
	for i in newarr:
		wewant = val.pop()
		if i < wewant:
			print(-1)
			return
		ok = i - wewant
		if i % ok != wewant:
			print(-1)
			return
		count += 1
	print(count)
	return
for _ in range(test_case):
	ans()


from collections import *
import sys
import io, os
import math
from heapq import *
gcd = math.gcd
sqrt = math.sqrt

def ceil(a, b):
	a = -a
	k = a // b
	k = -k
	return k
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def strinp(testcases):
	k = 5
	if testcases == -1 or testcases == 1:
		k = 1
	f = str(input())
	f = f[2:len(f) - k]
	return f

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		perm = [0] * (n + 1)
		perm[0] = 1
		useful = []
		for i in range(n):
			if arr[i] > n:
				useful.append(arr[i])
				continue
			if perm[arr[i]] != 0:
				useful.append(arr[i])
				continue
			perm[arr[i]] = 1
		c = 0
		if len(useful) == 0:
			print(0)
			continue
		useful.sort()
		pos = []
		for i in range(1, n + 1):
			if perm[i] == 0:
				pos.append(i)
		for j in range(len(useful)):
			if pos[j] >= useful[j] - pos[j]:
				c = -1
				break
			c += 1
		print(c)
main()


import sys, io, os
from os import path
from collections import Counter, defaultdict
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main():
	n = int(input())
	a = list(map(int, input().split()))
	dic = defaultdict(int)
	other = []
	for i in range(n):
		if 1 <= a[i] <= n and dic[a[i]] == 0:
			dic[a[i]] += 1
		else:
			other.append(a[i])
	rest = []
	for i in range(1, n + 1):
		if dic[i] == 0:
			rest.append(i)
	other.sort()
	flag = 1
	for i in range(len(other)):
		if other[i] - rest[i] <= rest[i]:
			flag = 0
			break
	if flag == 1:
		print(len(other))
	else:
		print(-1)
for _ in range(int(input())):
	main()


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	s.sort()
	s1 = [0] * (n + 1)
	s2 = []
	for i in range(len(s)):
		if s[i] <= len(s) and s1[s[i]] == 0:
			s1[s[i]] = 1
		else:
			s2.append(s[i])
	s2.sort()
	j = 0
	for i in range(1, n + 1):
		if s1[i] == 0:
			if s2[j] - s2[j] // 2 - 1 >= i:
				j += 1
			else:
				j = -1
				break
		if j >= len(s2):
			break
	print(j)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main(t):
	n = int(input())
	arr = list(map(int, input().split()))
	prepare = []
	dic = {}
	for num in arr:
		if num > n:
			prepare.append(num)
		elif num not in dic:
			dic[num] = 1
		else:
			prepare.append(num)
	ans = len(prepare)
	prepare.sort()
	index = 0
	for i in range(1, n + 1):
		if i in dic:
			continue
		if prepare[index] <= 2 * i:
			print(-1)
			return
		index += 1
	print(ans)
T = int(input())
t = 1
while t <= T:
	main(t)
	t += 1


import bisect
for i in range(int(input())):
	n = int(input())
	l = sorted(map(int, input().split()))
	x = set(l)
	q = 1
	w = {}
	r = set()
	for i in l:
		w[i] = w.get(i, 0) + 1
	for i in range(n):
		if q in x:
			w[q] -= 1
		else:
			r.add(q)
		q += 1
	l = []
	for i in w:
		l += [i] * w[i]
	l.sort()
	r = sorted(r)
	t = 1
	o = 0
	for i in range(len(r)):
		if r[i] * 2 + 1 > l[i]:
			t = 0
		else:
			o += 1
	print(o if t else -1)


import math, sys
from collections import defaultdict, deque
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')

def I():
	return int(sys.stdin.readline())

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

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

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		l = list(In())
		mx = []
		s = set([i for i in range(1, n + 1)])
		for x in l:
			if x <= n and x in s:
				s.remove(x)
			elif x & 1 == 1:
				mx.append((x // 2, x))
			else:
				mx.append((x // 2 - 1, x))
		if len(mx):
			mx.sort(key=lambda x: x[0])
		i = 0
		s = list(s)
		s.sort()
		ans = 0
		ok = True
		for i in range(len(s)):
			if s[i] <= mx[i][0]:
				ans += 1
				continue
			else:
				ok = False
				break
		if ok:
			print(ans)
		else:
			print(-1)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


import math
m = int(input())
import collections
while m > 0:
	n = int(input())
	a = input().split()
	for i in range(n):
		a[i] = int(a[i])
	d = collections.defaultdict(int)
	a.sort()
	fl = 1
	v = []
	for i in range(n):
		if i + 1 > a[i]:
			fl = -1
			break
		d[a[i]] += 1
		if a[i] <= n and d[a[i]] > 1:
			v.append(a[i])
		if a[i] > n:
			v.append(a[i])
	if fl == 1:
		s = []
		for i in range(n):
			if d[i + 1] == 0:
				s.append(i + 1)
		for i in range(len(s) - 1, -1, -1):
			if (v[i] - 1) // 2 < s[i]:
				fl = -1
				break
	if fl == 1:
		print(len(s))
	else:
		print('-1')
	m -= 1


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	temp = []
	x2 = []
	for i in range(n):
		if x[i] <= n:
			temp += [x[i]]
		else:
			x2 += [x[i]]
	if len(temp) > 0:
		temp = sorted(temp)
		x1 = [temp[0]]
		for i in range(1, len(temp)):
			if temp[i] != x1[-1]:
				x1 += [temp[i]]
			else:
				x2 += [temp[i]]
	else:
		x1 = []
	if len(x1) == n:
		ans = 0
	else:
		x1 = sorted(x1)
		x2 = sorted(x2)
		i = 0
		j = 0
		temp = 1
		ans = 0
		while temp <= n:
			if len(x1) > i and x1[i] == temp:
				temp += 1
				i += 1
			elif len(x2) > j and x2[j] >= 2 * temp + 1:
				ans += 1
				temp += 1
				j += 1
			else:
				ans = -1
				break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a1 = [-1] * n
	rest = []
	for i in range(n):
		if a[i] <= n:
			if a1[a[i] - 1] != a[i]:
				a1[a[i] - 1] = a[i]
			else:
				rest.append(a[i])
		else:
			rest.append(a[i])
	rest.sort()
	j = 0
	for i in range(n):
		if a1[i] == -1:
			a1[i] = rest[j]
			j += 1
	ans = 0
	a = a1
	for i in range(n):
		if a[i] > 2 * (i + 1):
			ans += 1
		elif a[i] == i + 1:
			ans += 0
		else:
			ans = -1
			break
	print(ans)


def solve(a, n):
	a.sort()
	check = [False for i in range(0, n + 1)]
	l = []
	for c in a:
		if 0 < c < n + 1 and (not check[c]):
			check[c] = True
		else:
			l.append(c)
	r = []
	for i in range(1, n + 1):
		if not check[i]:
			r.append(i)
	ans = len(l)
	for j in range(0, len(l)):
		if l[j] - (int(l[j] / 2) + 1) < r[j]:
			ans = -1
	print(ans)
	return ans
test = int(input())
for te in range(0, test):
	n = int(input())
	a = list(map(int, input().split()))
	solve(a, n)


from itertools import permutations as per
from math import factorial as fact
from difflib import SequenceMatcher
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	v = [0] * (n + 1)
	a.sort()
	c = 0
	j = 0
	for i in range(1, n + 1):
		while j < n and a[j] <= n and (v[a[j]] == 0):
			v[a[j]] = 1
			j += 1
		if v[i] == 0:
			if a[j] > 2 * i:
				j += 1
				c += 1
			else:
				c = -1
				break
	print(c)


t = int(input())
for e in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	h = {}
	for i in a:
		if i in h:
			h[i] += 1
		else:
			h[i] = 1
	al = []
	for i in range(1, len(a) + 1):
		if i in h:
			h[i] -= 1
			if h[i] == 0:
				del h[i]
		else:
			al.append(i)
	an = []
	for i in h:
		for k in range(h[i]):
			if i % 2 == 0:
				an.append(int((i - 1) / 2))
			else:
				an.append(int(i / 2))
	al.sort()
	an.sort()
	c = 0
	for i in range(len(al)):
		if al[i] <= an[i]:
			continue
		else:
			c = 1
			print(-1)
			break
	if c == 0:
		print(len(al))


for _ in range(int(input())):
	n = int(input())
	nums = sorted(list(map(int, input().split())))
	seen = [0] * (n + 1)
	(count, pos, first_empty) = (0, True, 1)
	for num in nums:
		if 1 <= num <= n and seen[num] == 0:
			seen[num] = 1
			while first_empty <= n and seen[first_empty] == 1:
				first_empty += 1
		elif num / 2 > first_empty:
			count += 1
			first_empty += 1
			while first_empty <= n and seen[first_empty] == 1:
				first_empty += 1
		else:
			pos = False
			break
	print(count if pos else -1)


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	k = 0
	a.sort()
	rep = True
	c = 0
	deja = [False] * n
	m = 1
	while k < n and rep and (m <= n):
		if not deja[m - 1]:
			if a[k] <= 2 * m and a[k] > n:
				rep = False
			elif a[k] > 2 * m and a[k] <= n and deja[a[k] - 1]:
				c += 1
				k += 1
				deja[m - 1] = True
				m += 1
			elif a[k] <= n:
				if deja[a[k] - 1]:
					rep = False
				else:
					deja[a[k] - 1] = True
					m = min(m, a[k])
				k += 1
			else:
				c += 1
				k += 1
				deja[m - 1] = True
				m += 1
		else:
			m += 1
	print(c if rep else -1)


import sys
from collections import Counter as cc
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	l = sorted(I())
	an = 0
	pos = 1
	vis = [0] * n
	visi = [0] * (n + 1)
	for i in range(n):
		if l[i] <= n and visi[l[i]] == 0:
			visi[l[i]] = vis[i] = 1
	lt = [i for i in range(1, n + 1) if not visi[i]]
	st = [(l[i] - 1) // 2 for i in range(n) if not vis[i]]
	pos = all((lt[i] <= st[i] for i in range(len(st))))
	print(len(st) if pos else -1)


t = int(input())
for p in range(t):
	n = int(input())
	lis = list(map(int, input().split()))
	ans = 0
	lis.sort()
	suset = set(lis)
	nono = set()
	for i in range(1, n + 1):
		if i in suset:
			suset.remove(i)
			nono.add(i)
	lolz = set(nono)
	cur = 1
	sus = True
	cur = 0
	for i in range(n):
		if lis[i] in lolz:
			lolz.remove(lis[i])
			lis[i] = 9999999999
	lis.sort()
	for i in range(0, n):
		if i + 1 in nono:
			continue
		if lis[cur] <= 2 * (i + 1):
			sus = False
			break
		cur += 1
	print(n - len(nono) if sus else -1)


t = int(input())
answers = []
for test in range(t):
	n = int(input())
	nums = set((i for i in range(1, n + 1)))
	arr = list(map(int, input().split()))
	check = [False] * n
	new_arr = []
	for i in arr:
		if i in nums:
			check[i - 1] = True
			nums.remove(i)
		else:
			new_arr.append(i)
	new_arr.sort()
	pointer = 0
	possible = True
	moves = 0
	for (i, value) in enumerate(check):
		if value == True:
			continue
		elif i + 1 >= new_arr[pointer] / 2:
			possible = False
			break
		else:
			pointer += 1
			moves += 1
	if not possible:
		answers.append(-1)
	else:
		answers.append(moves)
for num in answers:
	print(num)


import array
import bisect
import heapq
import math
import collections
import sys
import copy
from functools import reduce
import decimal
from io import BytesIO, IOBase
import os
import itertools
import functools
from types import GeneratorType
decimal.getcontext().rounding = decimal.ROUND_HALF_UP
graphDict = collections.defaultdict
queue = collections.deque

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		to = f(*args, **kwargs)
		if stack:
			return to
		else:
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						return to
					to = stack[-1].send(to)
	return wrappedfunc

class Graphs:

	def __init__(self):
		self.graph = graphDict(set)

	def add_edge(self, u, v):
		self.graph[u].add(v)
		self.graph[v].add(u)

	def dfs_utility(self, nodes, visited_nodes, colors, parity, level):
		global count
		if nodes == 1:
			colors[nodes] = -1
		elif len(self.graph[nodes]) == 1 and parity % 2 == 0:
			if q == 1:
				colors[nodes] = 1
			else:
				colors[nodes] = -1
				count += 1
		elif parity % 2 == 0:
			colors[nodes] = -1
		else:
			colors[nodes] = 1
		visited_nodes.add(nodes)
		for neighbour in self.graph[nodes]:
			new_level = level + 1
			if neighbour not in visited_nodes:
				self.dfs_utility(neighbour, visited_nodes, colors, level - 1, new_level)

	def dfs(self, node):
		Visited = set()
		color = collections.defaultdict()
		self.dfs_utility(node, Visited, color, 0, 0)
		return color

	def bfs(self, node, f_node):
		count = float('inf')
		visited = set()
		level = 0
		if node not in visited:
			queue.append([node, level])
			visited.add(node)
		flag = 0
		while queue:
			parent = queue.popleft()
			if parent[0] == f_node:
				flag = 1
				count = min(count, parent[1])
			level = parent[1] + 1
			for item in self.graph[parent[0]]:
				if item not in visited:
					queue.append([item, level])
					visited.add(item)
		return count if flag else -1
		return False

class Tree:

	def __init__(self, data):
		self.data = data
		self.left = None
		self.right = None

def inorder(node, lis):
	if node:
		inorder(node.left, lis)
		lis.append(node.data)
		inorder(node.right, lis)
	return lis

def leaf_node_sum(root):
	if root is None:
		return 0
	if root.left is None and root.right is None:
		return root.data
	return leaf_node_sum(root.left) + leaf_node_sum(root.right)

def hight(root):
	if root is None:
		return -1
	if root.left is None and root.right is None:
		return 0
	return max(hight(root.left), hight(root.right)) + 1

class UnionFind:
	parents = []
	sizes = []
	count = 0

	def __init__(self, n):
		self.count = n
		self.parents = [i for i in range(n)]
		self.sizes = [1 for i in range(n)]

	def find(self, i):
		if self.parents[i] == i:
			return i
		else:
			self.parents[i] = self.find(self.parents[i])
			return self.parents[i]

	def unite(self, i, j):
		root_i = self.find(i)
		root_j = self.find(j)
		if root_i == root_j:
			return
		elif root_i < root_j:
			self.parents[root_j] = root_i
			self.sizes[root_i] += self.sizes[root_j]
		else:
			self.parents[root_i] = root_j
			self.sizes[root_j] += self.sizes[root_i]

	def same(self, i, j):
		return self.find(i) == self.find(j)

	def size(self, i):
		return self.sizes[self.find(i)]

	def group_count(self):
		return len(set((self.find(i) for i in range(self.count))))

	def answer(self, extra, p, q):
		dic = collections.Counter()
		for q in range(n):
			dic[self.find(q)] = self.size(q)
		hq = list(dic.values())
		heapq._heapify_max(hq)
		ans = -1
		for z in range(extra + 1):
			if hq:
				ans += heapq._heappop_max(hq)
			else:
				break
		return ans

def rounding(n):
	return int(decimal.Decimal(f'{n}').to_integral_value())

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0 and n // i > 3)))

def p_sum(array):
	return list(itertools.accumulate(array))

def base_change(nn, bb):
	if nn == 0:
		return [0]
	digits = []
	while nn:
		digits.append(int(nn % bb))
		nn //= bb
	return digits[::-1]

def diophantine(a: int, b: int, c: int):
	(d, x, y) = extended_gcd(a, b)
	r = c // d
	return (r * x, r * y)

@bootstrap
def extended_gcd(a: int, b: int):
	if b == 0:
		(d, x, y) = (a, 1, 0)
	else:
		(d, p, q) = (yield extended_gcd(b, a % b))
		x = q
		y = p - q * (a // b)
	yield (d, x, y)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, **kwargs):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

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

def map_inp(v_type):
	return map(v_type, inp().split())

def list_inp(v_type):
	return list(map_inp(v_type))
for _ in range(int(inp())):
	n = int(inp())
	arr = list_inp(int)
	arr.sort()
	visited = set()
	start = 1
	count = 0
	for item in arr:
		if 1 <= item <= n and item not in visited:
			visited.add(item)
			continue
		while start in visited:
			start += 1
		if start <= (item - 1) // 2:
			count += 1
			visited.add(start)
	if len(visited) == n:
		print(count)
	else:
		print(-1)


from collections import Counter
from sys import stdin
input = stdin.readline
for test in range(int(input())):
	n = int(input())
	arr = sorted([int(i) for i in input().split()])
	prev = 1
	moves = 0
	bool = True
	c = Counter()
	for x in arr:
		while c[prev] == 1:
			prev += 1
		if x <= n and c[x] == 0:
			c[x] = 1
		elif (x - 1) // 2 >= prev:
			c[prev] = 1
			moves += 1
		else:
			bool = False
			break
	if bool:
		print(moves)
	else:
		print(-1)


import sys
import bisect
from bisect import bisect_left as lb
from bisect import bisect_right as rb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
sflo = lambda : list(map(float, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
sys.setrecursionlimit(3 * 10 ** 5)
mod = 10 ** 9 + 7
mod1 = 998244353
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs2(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)
ans = 0
a = []
(n, k) = (0, 0)
b = []
vis = []
tot = 0
time = []
time1 = []
adj = []
mx = -1
eps = 1e-07
x = 0
pref = []
lvl = []
gp = []
ans = []
f = 0
w = []

def dfs(i, prod, rem, n):
	if rem == 0:
		while i < len(n):
			prod *= int(n[i])
			i += 1
		return prod
	if i == len(n):
		return prod
	mx = 0
	for j in range(9 - int(n[i])):
		mx = max(dfs(i + 1, (int(n[i]) + min(j, rem)) * prod, rem - min(j, rem), n), mx)
	return mx

def hnbhai(tc):
	n = sb()
	a = sd()
	are = []
	last = 1
	a.sort()
	hogaya = [0] * (n + 2)
	ans = 0
	for i in a:
		if i >= 1 and i <= n and (hogaya[i] == 0):
			hogaya[i] = 1
			ans -= 1
	d = {}
	mark = {}
	for i in a:
		d[i] = d.get(i, 0) + 1
		if mark.get(i) is None:
			d[i] -= hogaya[min(n + 1, i)]
			mark[i] = 1
	are = list(d.keys())
	are.sort()
	last = 1
	tot = 0
	rem = 0
	mn = [0] * (n + 1)
	for i in range(len(are) - 1, -1, -1):
		kaha = are[i] // 2
		if are[i] % 2 == 1:
			kaha += 0
		else:
			kaha -= 1
		mn[i] = max(kaha, mn[i + 1])
	t = 0
	for i in are:
		t += 1
		while last <= n and hogaya[last]:
			last += 1
		ok = i if i >= 1 and i <= n else -1
		kaha = i // 2
		if i % 2 == 1:
			kaha += 0
		else:
			kaha -= 1
		while (last <= n and last <= kaha) and d[i] > 0:
			if hogaya[last]:
				last += 1
				continue
			rem -= 1
			d[i] -= 1
			last += 1
			tot += 1
		if d[i] > 0:
			print('-1')
			return
	while last <= n and hogaya[last] == 1:
		last += 1
	if last < n + 1:
		print(-1)
		return
	print(tot)
	return
for _ in range(sb()):
	hnbhai(_ + 1)


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import math
import bisect
t = int(input())
for a0 in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	l.sort()
	f = 0
	c = 0
	perm = [0] * n
	ind = 0
	l1 = []
	for i in range(n):
		if l[i] <= n:
			perm[l[i] - 1] += 1
			if perm[l[i] - 1] > 1:
				l1.append(l[i])
		else:
			ind = i
			break
	l1.extend(l[ind:])
	i = 0
	ind = 0
	while ind < n and i < n:
		if perm[i] == 0:
			if l1[ind] % 2 != 0:
				l1[ind] += 1
			if l1[ind] // 2 <= i + 1:
				f = 1
				break
			else:
				c += 1
				ind += 1
		i += 1
	if f == 1:
		print(-1)
	else:
		print(c)


def find(n, L):
	occupy = [0]
	left = []
	L.sort()
	L.reverse()
	for i in range(n):
		elem = L[-1]
		L.pop()
		if elem <= n and elem > occupy[-1]:
			occupy.append(elem)
		else:
			left.append(elem)
	occupy.append(n + 1)
	needed = []
	for i in range(len(occupy) - 1):
		for j in range(occupy[i] + 1, occupy[i + 1]):
			needed.append(j)
	for i in range(len(needed)):
		if left[i] > 2 * needed[i]:
			pass
		else:
			return -1
	return len(needed)
cases = int(input())
for wow in range(cases):
	n = int(input())
	line = input().split()
	L = []
	for elem in line:
		L.append(int(elem))
	print(find(n, L))


import math
x = int(input())
for i in range(x):
	y = int(input())
	a = input().split()
	for k in range(y):
		a[k] = int(a[k])
	d = dict()
	res = []
	for kk in a:
		if kk >= 1 and kk <= y:
			if kk not in d:
				d[kk] = 1
			else:
				res.append(kk)
		else:
			res.append(kk)
	res.sort()
	m = []
	for jj in range(1, y + 1):
		if jj not in d:
			m.append(jj)
	m.sort()
	ans = 0
	flag = True
	for ll in range(len(m)):
		if m[ll] * 2 < res[ll]:
			ans += 1
		else:
			flag = False
			break
	if flag:
		print(ans)
	else:
		print(-1)


t = int(input())

def maxRem(x):
	if int(x / 2) == x / 2:
		return int(x / 2) - 1
	return int(x / 2)
for i in range(t):
	large = []
	l = int(input())
	found = [0] * (l + 1)
	arr = [*map(int, input().split(' '))]
	for j in arr:
		if j <= l and found[j] == 0:
			found[j] = 1
		else:
			large += [j]
	large = sorted(large)
	ops = 0
	poss = True
	for j in range(1, l + 1):
		if found[j] == 1:
			continue
		if maxRem(large[ops]) >= j:
			ops += 1
		else:
			poss = False
			break
	if poss:
		print(ops)
	else:
		print(-1)


from sys import stdin, stdout
t = int(stdin.readline())
for tt in range(t):
	n = int(stdin.readline())
	a = [int(s) for s in stdin.readline().split()]
	has = [False for i in range(n + 1)]
	extra = [None for i in range(n)]
	el = 0
	for i in range(n):
		if 1 <= a[i] <= n and (not has[a[i]]):
			has[a[i]] = True
		else:
			extra[el] = a[i]
			el += 1
	extra = extra[:el]
	extra.sort()
	ei = 0
	impossible = False
	modnum = 0
	for i in range(1, n + 1):
		if not has[i]:
			if extra[ei] <= 2 * i:
				impossible = True
				break
			else:
				modnum += 1
				ei += 1
	stdout.write(str(-1 if impossible else modnum) + '\n')


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	a = []
	vis = [0] * (1 + n)
	for i in arr:
		if i <= n and (not vis[i]):
			vis[i] = 1
		else:
			a.append(i)
	a.sort()
	j = 0
	for i in range(1, n + 1):
		if not vis[i]:
			if a[j] < 2 * i + 1:
				return -1
			j += 1
	return j
t = int(input())
while t:
	print(solve())
	t -= 1


import sys
input = sys.stdin.readline
for __ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	visited = [0] * (n + 1)
	left = []
	for x in arr:
		if 1 <= x <= n:
			if not visited[x]:
				visited[x] = 1
			else:
				left.append(x)
		else:
			left.append(x)
	fill = [i for (i, x) in enumerate(visited) if not visited[i] and i != 0]
	left.sort()
	answer = len(fill)
	flag = 0
	while fill and left:
		x = left.pop()
		y = fill.pop()
		if x % 2 == 0:
			if x // 2 - 1 >= y:
				continue
			else:
				flag = 1
				break
		elif x // 2 >= y:
			continue
		else:
			flag = 1
			break
	print(answer if not flag else -1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	Set = set((i for i in range(1, n + 1)))
	flag = True
	cnt = 0
	for i in arr:
		if Set.__contains__(i):
			Set.remove(i)
		else:
			temp = Set.pop()
			if temp >= i / 2:
				flag = False
				print(-1)
				break
			else:
				cnt += 1
	if flag:
		print(cnt)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	check = False
	ck = [False] * n
	cks = [False] * n
	for i in range(n):
		if a[i] <= n and (not ck[a[i] - 1]):
			ck[a[i] - 1] = True
			cks[i] = True
	ind = 0
	ans = 0
	for i in range(n):
		if not ck[i]:
			for j in range(ind, n):
				if not cks[j] and a[j] >= 2 * (i + 1) + 1:
					ind = j + 1
					ck[i] = True
					cks[j] = True
					ans += 1
					break
	if False in ck:
		print(-1)
	else:
		print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	s = set((i for i in range(1, n + 1)))
	ans = 0
	dt = []
	for i in a:
		if i in s:
			s.remove(i)
		else:
			dt.append(i)
	dt = sorted(dt)
	s = sorted(list(s))
	for i in range(len(s)):
		if 2 * s[i] + 1 <= dt[i]:
			ans += 1
		else:
			ans = -1
			break
	print(ans)


from __future__ import division, print_function
import math
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def I():
	return int(input())

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

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
import itertools
from sys import maxsize, stdout, stdin, stderr
import sys

def S():
	return list(map(str, input().strip()))

def grid(r, c):
	return [lint() for i in range(r)]
from collections import defaultdict, Counter, deque
import math
import heapq
from heapq import heappop, heappush
import bisect
from math import factorial, inf
from itertools import groupby
from itertools import permutations as comb
mod = 998244353

def gcd(a, b):
	while b:
		a %= b
		tmp = a
		a = b
		b = tmp
	return a

def lcm(a, b):
	return a // gcd(a, b) * b

def check_prime(n):
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def primefactors(n):
	num = 0
	while n % 2 == 0:
		num += 1
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			num += 1
			n = n // i
	if n > 2:
		num += 1
	return num

def ask(a, b, c):
	print(c, a, b, flush=True)
	n = I()
	return n

def linear_sieve(n):
	is_composite = [False] * n
	prime = []
	for i in range(2, n):
		if not is_composite[i]:
			prime.append(i)
		for j in prime:
			is_composite[i * j] = True
			if i % prime == 0:
				break
	return prime

def dfs(i, p, d):
	(a, tmp) = (0, 0)
	for j in d[i]:
		if j != p:
			a += 1
			tmp += dfs(j, i)
	if a == 0:
		return 0
	return tmp / a + 1

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n // i
	if n > 2:
		l.append(n)
	return l

def binpow(a, b, c):
	res = 1
	a = a % c
	while b > 0:
		if b & 1:
			res = res * a % c
		b = b >> 1
		a = a * a % c
	return res - 1
d = []
primes = []
prim = [0] * (10 ** 6 + 1)

def sieve(n):
	for i in range(n):
		d.append(i)
	for i in range(2, n):
		if d[i] == i:
			prim[i] = 1
			primes.append(i)
		j = 0
		while j < len(primes) and primes[j] <= d[i] and (primes[j] * i < n):
			d[i * primes[j]] = primes[j]
			j += 1

def primeFactors(n):
	factors = []
	while n != 1:
		factors.append(d[n])
		n //= d[n]
	return factors
t = 1
t = I()
for _ in range(t):
	n = I()
	s = lint()
	s.sort()
	cnt = 0
	b = defaultdict(lambda : 0)
	c = [0] * n
	for i in range(n):
		if s[i] < n + 1 and b[s[i]] == 0:
			c[i] = 1
			b[s[i]] = 1
	l = []
	m = []
	for i in range(n):
		if c[i] == 0:
			l.append(s[i])
	for i in range(1, n + 1):
		if b[i] == 0:
			m.append(i)
	for i in range(len(l)):
		b = l[i] - m[i]
		if l[i] == m[i]:
			continue
		elif l[i] % b != m[i] or b <= 0:
			cnt = -1
			break
		else:
			cnt += 1
	print(cnt)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	need = set(range(n + 1))
	need.remove(0)
	remain = []
	for num in a:
		if num in need:
			need.remove(num)
		else:
			remain.append(num)
	remain.sort()
	need = sorted(list(need))
	count = 0
	for (k, l) in zip(remain, need):
		if l > (k - 1) / 2:
			print(-1)
			break
	else:
		print(len(need))


problem_num = int(input())
for i in range(problem_num):
	n = int(input())
	arr = list(map(int, input().split()))
	target = set(range(1, n + 1))
	target_arr = set()
	target_dup = []
	for i in range(n):
		if arr[i] in target and arr[i] not in target_arr:
			target_arr.add(arr[i])
		else:
			target_dup.append(arr[i])
	dum = list(target - target_arr)
	pr = 0
	dum.sort()
	target_dup.sort()
	for i in range(len(target_dup)):
		if (target_dup[i] - 1) // 2 < dum[i]:
			pr = -1
			break
	if pr:
		print(-1)
	else:
		print(len(target_dup))


def main():
	t = int(input())
	allans = []
	for _ in range(t):
		n = int(input())
		a = readIntArr()
		takenIndexes = set()
		vi = [False] * (n + 1)
		for (i, x) in enumerate(a):
			if 1 <= x <= n and vi[x] == False:
				vi[x] = True
				takenIndexes.add(i)
		remainingItems = []
		for i in range(n):
			if i not in takenIndexes:
				remainingItems.append(a[i])
		remainingItems.sort()
		requiredItems = []
		for x in range(1, n + 1):
			if vi[x] == False:
				requiredItems.append(x)
		m = len(remainingItems)
		possible = True
		for i in range(m):
			if requiredItems[i] * 2 >= remainingItems[i]:
				possible = False
		if possible:
			allans.append(m)
		else:
			allans.append(-1)
	multiLineArrayPrint(allans)
	return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

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

def makeArr(defaultValFactory, dimensionArr):
	dv = defaultValFactory
	da = dimensionArr
	if len(da) == 1:
		return [dv() for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(a, b, c):
	print('? {} {} {}'.format(a, b, c))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ansArr):
	print('! {}'.format(' '.join([str(x) for x in ansArr])))
	sys.stdout.flush()
inf = float('inf')
from math import gcd, floor, ceil
import math
for _abc in range(1):
	main()


t = int(input())
for tc in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	perms = {i for i in range(1, n + 1)}
	curr = set()
	rem = []
	for i in range(n):
		if arr[i] <= n and arr[i] not in curr:
			curr.add(arr[i])
		else:
			rem.append(arr[i])
	perms = list(perms - curr)
	rem.sort()
	perms.sort()
	ans = 0
	for i in range(len(perms)):
		if (rem[i] - 1) // 2 < perms[i]:
			ans = -1
			break
		else:
			ans += 1
	print(ans)


import sys
from collections import *
t = int(sys.stdin.readline())
ans = []
for _ in range(t):
	n = int(sys.stdin.readline())
	nums = Counter(map(int, sys.stdin.readline().split()))
	nd = set()
	arr = []
	for i in range(1, n + 1):
		if nums[i]:
			nums[i] -= 1
		else:
			nd.add(i)
	for x in nums:
		for _ in range(nums[x]):
			arr.append(x)
	ret = len(arr)
	arr.sort(reverse=True)
	for i in range(1, n + 1):
		if i in nd:
			if arr[-1] <= i * 2:
				break
			else:
				arr.pop()
	else:
		ans.append(str(ret))
		continue
	ans.append('-1')
print('\n'.join(ans))


kl = int(input())
for kkl in range(kl):
	n = int(input())
	sp = [int(i) for i in input().split()]
	A = {i for i in range(1, n + 1)}
	Msp = set()
	rsp = []
	for i in range(n):
		if sp[i] in A and sp[i] not in Msp:
			Msp.add(sp[i])
		else:
			rsp.append(sp[i])
	sp = list(A - Msp)
	pr = 0
	rsp.sort()
	sp.sort()
	for i in range(len(sp)):
		if (rsp[i] - 1) // 2 < sp[i]:
			pr = -1
			break
	if pr:
		print(-1)
	else:
		print(n - len(Msp))


from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	d = dd(int)
	l = list(map(int, input().split()))
	for i in range(1, n + 1):
		d[i] = 0
	ans = []
	for i in l:
		if i <= n and d[i] == 0:
			d[i] = 1
		else:
			ans.append(i)
	ans.sort(reverse=True)
	cnt = 0
	flag = 0
	for i in range(1, n + 1):
		if d[i] == 0:
			if ans[-1] <= 2 * i:
				print(-1)
				flag += 1
				break
			else:
				cnt += 1
				ans.pop()
	if flag == 0:
		print(cnt)


import sys
import os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
	sys.stderr = open('error.txt', 'w')
depth = 200001
mod = 1000000007
lim = mod * mod
sys.setrecursionlimit(depth)
linp = lambda : list(minp())
minp = lambda : map(int, input().split())
from math import inf, ceil, sqrt, log2, gcd
from collections import defaultdict, deque
dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]

def expo(a, b):
	if not b:
		return 1
	v = expo(a, b // 2)
	v = v * v % mod
	if b % 2:
		v = v * a % mod
	return v

def modinv(a, b):
	d = expo(b, mod - 2)
	v = a * d % mod
	return v

def checkPrime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	for i in range(3, int(sqrt(n)) + 1, 2):
		if n % i == 0:
			return False
	return True

def highestPowerof2(x):
	x |= x >> 1
	x |= x >> 2
	x |= x >> 4
	x |= x >> 8
	x |= x >> 16
	return x ^ x >> 1
for _ in range(int(input())):
	n = int(input())
	a = sorted(linp())
	b = [False for _ in range(n)]
	c = [True for _ in range(n)]
	for i in range(n):
		if a[i] <= n and (not b[a[i] - 1]):
			c[i] = False
			b[a[i] - 1] = True
	d = list()
	for i in range(n):
		if c[i]:
			d.append(a[i])
	e = list()
	for i in range(n):
		if not b[i]:
			e.append(i + 1)
	(a, b) = (d, e)
	(a.sort(), b.sort())
	(n, f) = (len(a), True)
	for i in range(n):
		if b[i] >= a[i] - b[i]:
			f = False
	if f:
		print(n)
	else:
		print(-1)


from math 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())

def check(ind, a, i):
	if ind >= len(a):
		return 0
	z = a[ind] - 1
	z = z // 2
	if z >= i + 1:
		return 1
t = inp()
while t:
	t -= 1
	n = inp()
	a = lis()
	a.sort()
	moves = 0
	vis = [0] * n
	greedy = []
	for i in a:
		if i <= n:
			if vis[i - 1] == 0:
				vis[i - 1] = 1
			else:
				greedy.append(i)
		else:
			greedy.append(i)
	greedy = greedy[::-1]
	flag = 0
	for i in range(1, n + 1):
		if vis[i - 1] == 0:
			z = greedy.pop()
			z = (z - 1) // 2
			if z >= i:
				vis[i - 1] = 1
				moves += 1
			else:
				flag = 1
				break
	if flag:
		print(-1)
	else:
		print(moves)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	S = set([i + 1 for i in range(n)])
	newarr = []
	for i in range(n):
		if arr[i] in S:
			S.remove(arr[i])
		else:
			newarr.append(arr[i])
	S = sorted(list(S), reverse=True)
	newarr.sort(reverse=True)
	count = 0
	poss = True
	for i in range(len(newarr)):
		if S[i] * 2 < newarr[i]:
			count += 1
		else:
			print(-1)
			poss = False
			break
	if poss:
		print(count)


import sys
from bisect import bisect_left as lb
from bisect import bisect_right as rb
from collections import deque
from queue import PriorityQueue as pq
from math import gcd
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
lii = lambda : list(map(int, list(ip())))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
inv = lambda x: pow(x, mod - 2, mod)
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
mod = 10 ** 9 + 7
mod1 = 998244353
for _ in range(ii()):
	n = ii()
	a = il()
	a.sort()
	b = [0 for i in range(n + 1)]
	c = []
	for i in range(n):
		if a[i] <= n:
			b[a[i]] += 1
	for i in range(1, 1 + n):
		if b[i] == 0:
			c.append(i)
	ans = len(c)
	for i in range(n - 1, -1, -1):
		if len(c) == 0:
			break
		fl = 1
		if a[i] <= n and b[a[i]] == 1:
			fl = 0
		if fl and a[i] > 2 * c[-1] and (c[-1] != 1):
			if a[i] <= n:
				b[a[i]] -= 1
			a[i] = c[-1]
			b[c[-1]] += 1
			c.pop()
		if len(c) > 0 and c[-1] == 1:
			ind = 0
			for i in range(n):
				if a[i] > n:
					ind = a[i]
					break
				elif b[a[i]] > 1:
					ind = a[i]
					break
			if ind != 2:
				c.pop()
	if len(c) == 0:
		print(ans)
	else:
		print(-1)


import math
for s in [*open(0)][2::2]:
	a = [*map(int, s.split())]
	op = 0
	n = len(a)
	a.sort()
	undone = [1] * (n + 1)
	greater = []
	for x in a:
		if x <= n and undone[x] == 1:
			undone[x] = 0
		else:
			greater.append(x)
	undone_collected = []
	for i in range(1, n + 1):
		if undone[i] == 1:
			undone_collected.append(i)
	for (ud, g) in zip(undone_collected, greater):
		if ud > math.ceil(g / 2) - 1:
			print(-1)
			break
	else:
		print(len(greater))


import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
try:
	(sys.stdin, sys.stdout) = (open('in.txt', 'r'), open('out.txt', 'w'))
except:
	pass
ii1 = lambda : int(sys.stdin.readline().strip())
is1 = lambda : sys.stdin.readline().strip()
iia = lambda : list(map(int, sys.stdin.readline().strip().split()))
isa = lambda : sys.stdin.readline().strip().split()
mod = int(1000000000.0 + 7)
from math import inf
from collections import defaultdict as dd
from heapq import heapify, heappop, heappush
from math import gcd

def main(tt):
	n = ii1()
	arr = iia()
	arr.sort()
	vis = [0] * (n + 1)
	heap = []
	ans = 0
	for i in range(n - 1, -1, -1):
		val = arr[i]
		if val <= n and vis[val] == 0:
			vis[val] = 1
			continue
		m = arr[i] // 2
		r = val % (m + 1)
		ans += 1
		heappush(heap, -r)
	for i in range(n, 0, -1):
		if vis[i] == 0:
			if heap:
				val = -heappop(heap)
				if val < i:
					print(-1)
					return
			else:
				print(-1)
				return
	print(ans)
t = 1
t = ii1()
for _ in range(t):
	main(_)


import sys
inpu = sys.stdin.readline
prin = sys.stdout.write
I = lambda : [*map(int, inpu().split())]
t = I()[0]
for _ in range(t):
	n = I()[0]
	a = sorted(I())
	count = 0
	covered = [0] * (n + 1)
	sml = 1
	bad = False
	for i in range(n):
		if a[i] <= n and covered[a[i]] == 0:
			count += 1
			covered[a[i]] = 1
			if sml == a[i]:
				while sml <= n and covered[sml] == 1:
					sml += 1
		elif sml < a[i] / 2:
			covered[sml] = 1
			while sml <= n and covered[sml] == 1:
				sml += 1
		else:
			bad = True
	if bad:
		print(-1)
	else:
		print(n - count)


from sys import stdin
from bisect import bisect
from math import gcd
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	d = [False for i in range(n + 1)]
	d[0] = True
	l = []
	for a in arr:
		if a <= n and d[a] == False:
			d[a] = True
		else:
			l.append(a)
	l.sort()
	ans = 0
	for i in range(1, n + 1):
		if d[i] == False:
			ans += 1
	i = 1
	j = 0
	while i < n + 1:
		if d[i] == False:
			x = bisect(l, 2 * i, j, len(l))
			if x == len(l) or j == len(l):
				ans = -1
				break
			j = x + 1
		i += 1
	print(ans)


n = int(input())
for i in range(n):
	k = int(input())
	L = input().split(' ')
	for j in range(len(L)):
		L[j] = int(L[j])
	L.sort()
	sum = 0
	OKDic = {}
	j = 1
	t = -1
	rem = -1
	while t < len(L) - 1:
		t += 1
		if L[t] == j:
			j += 1
		elif L[t] <= len(L) and L[t] != rem:
			OKDic[L[t]] = True
		elif L[t] > 2 * j:
			sum += 1
			j += 1
		else:
			sum = -1
			break
		while j in OKDic and OKDic[j]:
			OKDic[j] = False
			j += 1
		if j > len(L):
			break
		if t == len(L) - 1:
			break
		rem = L[t]
	print(sum)


for i in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	n_set = set(list(range(1, n + 1)))
	b = []
	for i in lst:
		if i in n_set:
			n_set.remove(i)
		else:
			d1 = (i - 1) // 2
			b.append(d1)
	b.sort()
	n_list = sorted(list(n_set))
	for (k, l) in zip(b, n_list):
		if l > k:
			print(-1)
			break
	else:
		print(len(n_set))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	ok = True
	che = [False] * n
	v = []
	for i in A:
		if i <= n and che[i - 1] == False:
			che[i - 1] = True
		else:
			v.append(i)
	v = v[::-1]
	c = 0
	for i in range(n):
		if not che[i]:
			k = v.pop()
			k = (k - 1) // 2
			if k < i + 1:
				ok = False
			c += 1
	if ok:
		print(c)
	else:
		print(-1)


import sys
import bisect
import math
from collections import deque
input = lambda : sys.stdin.readline().strip()
mod = 10 ** 9 + 7

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	arr = []
	visited = [False] * (1 + n)
	for i in a:
		if i <= n and (not visited[i]):
			visited[i] = True
		else:
			arr.append(i)
	arr.sort()
	j = 0
	for i in range(1, n + 1):
		if not visited[i]:
			if arr[j] < 2 * i + 1:
				return -1
			j += 1
	return j
t = 1
t = int(input())
for _ in range(t):
	print(solve())


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	ans = 0
	b = []
	for (i, x) in enumerate(a):
		if x > n or (i and x == a[i - 1]):
			b.append(x // 2 + x % 2 - 1)
			ans += 1
		else:
			b.append(x)
	b.sort()
	cur = -1
	flag = 0
	for (i, x) in enumerate(b):
		if x <= i:
			print(-1)
			flag = 1
			break
	if not flag:
		print(ans)
	t -= 1


import math
for i in range(int(input())):
	n = int(input())
	A = [int(k) for k in input().split()]
	d = []
	l = 0
	m = 0
	d = dict()
	A.sort()
	m = []
	b = []
	for k in range(1, n + 1):
		d[k] = 0
	for k in range(n):
		if A[k] in d:
			if d[A[k]] == 0:
				d[A[k]] += 1
			else:
				m.append(A[k])
		else:
			m.append(A[k])
	for k in d:
		if d[k] == 0:
			b.append(k)
	m.sort()
	b.sort()
	j = 0
	y = 0
	c = 0
	for k in range(len(m)):
		if m[k] % 2 == 0:
			m[k] -= 2
			if b[j] <= m[k] // 2:
				j += 1
				c += 1
			else:
				break
		else:
			m[k] -= 1
			if b[j] <= m[k] // 2:
				j += 1
				c += 1
			else:
				break
	if c == len(m):
		print(c)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	ex = []
	s = [0] * (n + 1)
	c = 0
	for i in range(n):
		if l[i] <= n and s[l[i]] == 0:
			s[l[i]] = l[i]
		else:
			ex.append(l[i])
	ex.sort(reverse=True)
	for i in range(1, n + 1):
		if s[i] == 0:
			if (ex[-1] + 1) // 2 <= i:
				print(-1)
				break
			else:
				c += 1
			ex.pop()
		else:
			continue
	else:
		print(c)


import sys
tokens = ''.join(sys.stdin.readlines()).split()[::-1]

def next():
	return tokens.pop()

def nextInt():
	return int(next())

def nextFloat():
	return float(next())

def getIntArray(n):
	return [nextInt() for _ in range(n)]

def getFloatArray(n):
	return [nextFloat() for _ in range(n)]

def getStringArray(n):
	return [next() for _ in range(n)]
testcase = True

def solve(testcase=1):
	N = nextInt()
	A = sorted(getIntArray(N))[::-1]
	(S, R) = (set(), [])
	for itr in A:
		if 1 <= itr <= N and itr not in S:
			S.add(itr)
		else:
			R.append(itr)
	k = 0
	for s in range(N, 0, -1):
		if s in S:
			continue
		if s * 2 < R[k]:
			k += 1
		else:
			print(-1)
			return
	print(N - len(S))
	pass
if testcase is None:
	testcaseCount = 1
	while tokens:
		solve(testcaseCount)
		testcaseCount += 1
else:
	testcaseCount = nextInt() if testcase else 1
	for tc in range(testcaseCount):
		solve(tc + 1)
	assert not tokens


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sp = [0] * n
	s = []
	for el in a:
		if el <= n:
			if sp[el - 1]:
				s.append(el)
			else:
				sp[el - 1] = 1
		else:
			s.append(el)
	s = sorted(s, reverse=True)
	j = 0
	for i in range(n - 1, -1, -1):
		if not sp[i]:
			if (s[j] - 1) // 2 < i + 1:
				print(-1)
				break
			else:
				j += 1
	else:
		print(len(s))


def solve(array, n):
	new_array = [-1 for i in range(n + 1)]
	temp = []
	for x in array:
		if x > n:
			temp.append(x)
		elif new_array[x] == 1:
			temp.append(x)
		else:
			new_array[x] = 1
	temp = sorted(temp)
	j = 0
	count = 0
	for i in range(1, n + 1, 1):
		if new_array[i] == 1:
			continue
		elif temp[j] - i > 0:
			if temp[j] % (temp[j] - i) == i:
				new_array[i] = 1
				count += 1
				j += 1
				continue
			else:
				return -1
		else:
			return -1
	return count
t = int(input())
for test_case in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	print(solve(array, n))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set([x for x in range(1, n + 1)])
	boo = [0 for i in range(n)]
	l.sort()
	l_new = []
	f = 0
	c = 0
	for i in range(len(l)):
		if l[i] in s:
			s.remove(l[i])
			boo[l[i] - 1] = 1
		else:
			x = i
			l_new.append(l[i])
	l_new.sort(reverse=True)
	ans = []
	for i in range(len(boo)):
		if boo[i] == 0:
			ans.append(i + 1)
	ans.sort(reverse=True)
	for i in range(len(l_new) - 1, -1, -1):
		if l_new[i] % 2 == 0:
			if ans[-1] <= l_new[i] // 2 - 1:
				z = ans.pop()
				c += 1
			else:
				f = 1
				break
		elif ans[-1] <= (l_new[i] - 1) // 2:
			z = ans.pop()
			c += 1
		else:
			f = 1
			break
	if len(ans) == 0 and f == 0:
		print(c)
	elif len(ans) != 0 or f == 1:
		print(-1)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	need = []
	pre = []
	s = set(arr)
	s1 = set()
	for i in range(1, n + 1):
		if i not in s:
			need.append(i)
	for i in range(len(arr)):
		if arr[i] > n or arr[i] in s1:
			pre.append(arr[i])
		else:
			s1.add(arr[i])
	pre.sort()
	need.sort()
	move = 0
	flag = 0
	for i in range(len(pre)):
		m = pre[i] // 2
		maxi = pre[i] % (m + 1)
		if maxi < need[i]:
			flag = 1
			break
		else:
			move += 1
	if flag:
		print(-1)
	else:
		print(move)


_ = int(input())
for __ in range(_):
	n = int(input())
	arr = sorted(list(map(int, input().split())))
	arr_ranges = []
	arr_done = [-1] * n
	out = 0
	for x in range(n):
		if arr[x] <= n and arr_done[arr[x] - 1] == -1:
			arr_done[arr[x] - 1] = 1
			continue
		tmp = arr[x]
		if tmp % 2 == 0:
			tmp -= 1
		tmp = tmp // 2
		arr_ranges.append(tmp)
	pointer = 0
	for x in range(0, n):
		if arr_done[x] == -1:
			if arr_ranges and pointer < len(arr_ranges) and (arr_ranges[pointer] >= x + 1):
				out += 1
				pointer += 1
			else:
				print(-1)
				break
	else:
		print(out)


from sys import stdin
for testcase in range(int(stdin.readline())):
	N = int(stdin.readline())
	values = list(map(int, stdin.readline().split()))
	additionalValues = []
	valuesMissing = set(range(1, N + 1))
	for value in values:
		if value in valuesMissing:
			valuesMissing.remove(value)
		else:
			additionalValues.append(value)
	additionalValues.sort()
	possibility = True
	additionalValuesIndex = 0
	for value in range(1, N + 1):
		if value in valuesMissing:
			if value > additionalValues[additionalValuesIndex] // 2 - (additionalValues[additionalValuesIndex] % 2 == 0):
				possibility = False
			additionalValuesIndex += 1
	print([-1, additionalValuesIndex][possibility])


def paprika(n, array):
	ans = 0
	l = []
	s = set()
	for x in range(n):
		if array[x] not in s and array[x] <= n:
			s.add(array[x])
		else:
			l.append(array[x])
	l.sort()
	p = []
	ans = 0
	for i in range(1, n + 1):
		if i not in s:
			p.append(i)
	for x in range(len(p)):
		if l[x] - p[x] > p[x]:
			ans += 1
		else:
			return -1
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(paprika(n, l))


import heapq

def solve(a):
	n = len(a)
	pq = []
	found = [False for _ in range(n + 1)]
	ret = 0
	for i in range(n):
		if 1 <= a[i] <= n and found[a[i]] == False:
			found[a[i]] = True
		else:
			heapq.heappush(pq, -a[i])
	unfoundList = []
	for i in range(1, n + 1):
		if found[i] == False:
			unfoundList.append(i)
	unfoundList.sort()
	while len(pq) > 0:
		head = -heapq.heappop(pq)
		target = unfoundList.pop(-1)
		if head > target * 2:
			ret += 1
		else:
			return -1
	return ret
t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(a))


import os
import sys
from io import BytesIO, IOBase
import math
mod = 1000000007

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def primeFactors(n):
	arr = []
	while n % 2 == 0:
		arr.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			arr.append(i)
			n = n // i
	if n > 2:
		arr.append(n)
	return arr

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(a, b):
	return a // gcd(a, b) * b

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		brr = []
		crr = [-1 for i in range(n + 1)]
		for i in range(n):
			if arr[i] > n:
				brr.append(arr[i])
			elif crr[arr[i]] != -1:
				brr.append(arr[i])
			else:
				crr[arr[i]] = 1
		brr.sort(reverse=True)
		ans = 0
		flag = True
		for i in range(1, n + 1):
			if crr[i] == -1:
				if i < int(math.ceil(brr[-1] / 2)):
					ans += 1
					brr.pop()
					crr[i] = 1
				else:
					flag = False
					break
		if flag:
			print(ans)
		else:
			print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import Counter

def solve():
	n = int(input())
	A = [int(x) for x in input().split()]
	counts = Counter(A)
	missing = []
	for i in range(1, n + 1):
		if i not in counts:
			missing.append(i)
		else:
			counts[i] -= 1
	extra = []
	for (x, c) in counts.items():
		extra += [x] * c
	extra.sort()
	for (m, e) in zip(missing, extra):
		if m > (e - 1) // 2:
			return -1
	return len(missing)
t = int(input())
for _ in range(t):
	print(solve())


inp = int(input())
for i in range(inp):
	n = int(input())
	true_set = {}
	for i in range(1, n + 1):
		true_set[i] = 1
	false_set = list(map(int, input().split()))
	false_set.sort()
	ans = n
	new_false_set = []
	for i in range(1, n + 1):
		if false_set[i - 1] in true_set and true_set[false_set[i - 1]] != 0:
			true_set[false_set[i - 1]] = 0
			false_set[i - 1] = 0
			ans -= 1
		else:
			new_false_set.append(false_set[i - 1])
	new_true_set = []
	for i in true_set:
		if true_set[i] != 0:
			new_true_set.append(i)
	iter = 0
	for i in new_true_set:
		if new_false_set[iter] % 2 == 0:
			if new_false_set[iter] // 2 - 1 < i:
				ans = -1
				break
		elif new_false_set[iter] // 2 < i:
			ans = -1
			break
		iter += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	flg = [0] * (n + 1)
	for i in range(n):
		if arr[i] <= n:
			flg[arr[i]] = 1
	arr.sort(reverse=True)
	made = n
	flag = True
	ct = 0
	for i in range(n):
		if arr[i] <= n and (i == 0 or arr[i] != arr[i - 1]):
			continue
		while made > 0 and flg[made] == 1:
			made -= 1
		if 2 * made < arr[i]:
			made -= 1
			ct += 1
		else:
			flag = False
			break
	if flag == False:
		print(-1)
	else:
		print(ct)


import sys
import math
from fractions import Fraction
from decimal import *
input = sys.stdin.readline
t = int(input())
while t > 0:
	n = int(input().strip())
	inp = input().strip()
	arr = [int(x) for x in inp.split(' ')]
	present = [0] * (n + 1)
	arr.sort()
	ans = 0
	cur = 1
	for a in arr:
		if a <= n:
			if present[a] == 0:
				present[a] = 1
			else:
				while cur <= n and present[cur] == 1:
					cur += 1
				if a >= 2 * cur + 1:
					ans += 1
					cur += 1
				else:
					ans = -1
					break
		else:
			while cur <= n and present[cur] == 1:
				cur += 1
			if a >= 2 * cur + 1:
				ans += 1
				cur += 1
			else:
				ans = -1
				break
	print(ans)
	t -= 1


def check(x, y):
	if x % 2 == 0:
		if y in range(x // 2):
			return True
		return False
	if y in range(x // 2 + 1):
		return True
	return False

def solve(n, x):
	se = set()
	unused = []
	for i in x:
		if i > n or i in se:
			unused.append(i)
		else:
			se.add(i)
	wanted = []
	for i in range(1, n + 1):
		if i not in se:
			wanted.append(i)
	unused.sort()
	for i in range(len(unused)):
		if not check(unused[i], wanted[i]):
			return -1
	return len(unused)
t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().strip().split()))
	print(solve(n, x))


from __future__ import division, print_function
import math
import sys
import os
from io import BytesIO, IOBase
from collections import deque, Counter, OrderedDict, defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

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

def inps():
	return input().strip()

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

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	n = inp()
	lst = inlt()
	g = Counter(lst)
	av = [i for i in range(1, n + 1) if g[i] == 0]
	l = []
	for each in g:
		if each > n:
			for i in range(g[each]):
				l.append(each)
		else:
			for i in range(g[each] - 1):
				l.append(each)
	l.sort()
	opr = 0
	(i, j) = (len(l) - 1, len(av) - 1)
	flg = 0
	while i >= 0 and j >= 0:
		if l[i] < av[j]:
			flg = 1
			break
		elif (l[i] - 1) // 2 >= av[j]:
			i -= 1
			j -= 1
			opr += 1
		else:
			flg = 1
			break
	if flg:
		print(-1)
	else:
		print(opr)


def solve(n, A):
	cnt = 0
	flags = [False] * (n + 1)
	A.sort()
	i = 0
	for a in A:
		if a < n + 1 and (not flags[a - 1]):
			flags[a - 1] = True
			while flags[i]:
				flags[i] = True
				i += 1
		elif i + 1 < a / 2:
			flags[i] = True
			while flags[i]:
				flags[i] = True
				i += 1
			cnt += 1
		else:
			return -1
	return cnt

def main():
	ans = []
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = list(map(int, input().split(' ')))
		ans.append(solve(n, A))
	for a in ans:
		print(a)
main()


from _collections import defaultdict
for j in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	cnt = 0
	ok = 0
	dic = defaultdict(lambda : 0)
	for el in arr:
		dic[el] += 1
	missed = []
	for i in range(1, n + 1):
		if dic[i] == 0:
			missed.append(i)
	missed.sort(reverse=True)
	for i in range(n):
		now = arr[i] - arr[i] // 2 - 1
		if arr[i] == i + 1:
			pass
		elif arr[i] <= n and dic[arr[i]] > 1:
			if now < missed[-1]:
				ok = -1
				break
			else:
				cnt += 1
				missed.pop()
				dic[arr[i]] -= 1
		elif arr[i] <= n and dic[arr[i]] == 1:
			pass
		elif now < missed[-1]:
			ok = -1
			break
		else:
			cnt += 1
			missed.pop()
		if len(missed) == 0:
			break
	if ok == -1:
		print(-1)
	else:
		print(cnt)


from collections import Counter
from sys import *
from collections import defaultdict as dd
from math import *

def inp():
	return int(stdin.readline().strip())

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

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

def sinp(n=1):
	if n == 1:
		return stdin.readline().strip()
	elif n == 2:
		return list(stdin.readline().strip())
	else:
		return list(stdin.readline().split())

def pr(*x, end='\n'):
	print(*x, end=end)

def mod(f, val=1000000007):
	return f % val

def csort(c):
	sorted(c.items(), key=lambda pair: pair[1], reverse=True)

def indc(l, n):
	c = {}
	for i in range(n):
		c[l[i]] = c.get(l[i], []) + [i + 1]
	return c
cou = inp()
for _ in range(cou):
	n = inp()
	l = linp()
	c = dd(int)
	l2 = []
	for i in range(n):
		if l[i] >= 1 and l[i] <= n and (not c[l[i]]):
			c[l[i]] = 1
		else:
			l2.append(l[i])
	l2.sort()
	l3 = [0 for i in range(n + 1)]
	for i in c.keys():
		l3[i] = 1
	k = 0
	p = True
	for i in range(1, n + 1):
		if not l3[i]:
			if i <= l2[k] // 2 - (l2[k] % 2 == 0):
				k += 1
				l3[i] = 1
			else:
				p = False
				break
	if p:
		pr(len(l2))
	else:
		pr(-1)


import math
tc = int(input())
while tc:
	tc -= 1
	n = int(input())
	a = list(map(int, input().split()))
	vis = [False] * n
	extras = []
	for x in a:
		try:
			if vis[x - 1]:
				extras.append(x)
			vis[x - 1] = True
		except:
			extras.append(x)
	missing = []
	for i in range(n):
		if not vis[i]:
			missing.append(i + 1)
	missing.sort()
	extras.sort()
	cond = True
	for i in range(len(missing)):
		ex = extras[i]
		ms = missing[i]
		if ex < 2 * ms + 1:
			cond = False
			break
	if cond:
		print(len(missing))
	else:
		print(-1)


from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	a = sorted(a)
	d = {}
	c = -1
	for i in a:
		c += 1
		if 1 < i <= n and i not in d:
			d[i] = c
	inds = set(d.values())
	ptr = 0
	ans = 0
	for i in range(1, n + 1):
		if i in d:
			continue
		while 1:
			if ptr in inds:
				ptr += 1
			else:
				break
		if i == a[ptr]:
			ptr += 1
		elif (a[ptr] - 1) // 2 >= i:
			ans += 1
			ptr += 1
		else:
			ans = -1
			break
	print(ans)


from sys import stdin
input = stdin.readline

def answer():
	nottheir = n
	ans = 0
	for i in range(n):
		if a[i] <= n and done[a[i]]:
			done[a[i]] = False
			continue
		while their[nottheir]:
			nottheir -= 1
		if nottheir == 0:
			break
		if nottheir <= a[i] // 2 - (a[i] & 1 == 0):
			their[nottheir] = True
		else:
			return -1
		ans += 1
	while their[nottheir]:
		nottheir -= 1
	if nottheir:
		return -1
	return ans
for T in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())), reverse=True)
	their = [False] * (n + 1)
	done = [False] * (n + 1)
	for i in range(n):
		if a[i] <= n:
			their[a[i]] = True
			done[a[i]] = True
	print(answer())


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	d = defaultdict(lambda : 0)
	for i in A:
		d[i] += 1
	app = []
	for i in range(1, n + 1):
		if d[i] == 0:
			app.append(i)
	app.sort()
	rest = []
	for i in d.keys():
		if i > n:
			rest += [i] * d[i]
		elif d[i] > 1:
			rest += [i] * (d[i] - 1)
	rest.sort()
	ok = True
	for i in range(len(app)):
		if rest[i] - app[i] <= app[i]:
			ok = False
	if ok:
		print(len(rest))
	else:
		print(-1)


t = int(input())
for tt in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	m = {}
	for ai in a:
		if ai in m:
			m[ai] += 1
		else:
			m[ai] = 1
	ans = 0
	flag = [False] * n
	for i in range(n):
		if i + 1 in m:
			flag[i] = True
			if m[i + 1] > 1:
				m[i + 1] -= 1
			else:
				del m[i + 1]
	(arem, ix) = ([], [])
	for num in m:
		arem += [num] * m[num]
	arem.sort()
	for i in range(n):
		if not flag[i]:
			ix.append(i + 1)
	for i in range(len(arem)):
		if arem[i] != ix[i]:
			if arem[i] % (arem[i] - ix[i]) == ix[i]:
				ans += 1
			else:
				ans = -1
				break
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	check = [0] * (n + 1)
	check_arr = []
	for i in range(n):
		if arr[i] > n or (arr[i] <= n and check[arr[i]] > 0):
			check_arr.append(arr[i])
		if arr[i] <= n:
			check[arr[i]] = check[arr[i]] + 1
	answer = len(check_arr)
	for i in range(len(check_arr)):
		if check_arr[i] % 2 == 0:
			check_arr[i] = check_arr[i] // 2 - 1
		else:
			check_arr[i] = check_arr[i] // 2
	idx = 0
	fillin = 1
	while idx < len(check_arr):
		if check[fillin] == 0:
			if fillin <= check_arr[idx]:
				idx = idx + 1
				fillin = fillin + 1
			else:
				answer = -1
				break
		else:
			fillin = fillin + 1
	print(answer)


for iq in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	est = [0] * (n + 1)
	p = 1
	for i in range(n):
		if a[i] <= n and (not est[a[i]]):
			est[a[i]] = 1
			a[i] = [a[i], a[i]]
		else:
			while p != n + 1 and est[p]:
				p += 1
			a[i] = [a[i], p]
			p += 1
	flag = False
	m = 0
	for i in range(len(a)):
		if a[i][0] == a[i][1]:
			continue
		if a[i][0] - a[i][1] > a[i][1]:
			m += 1
		else:
			flag = True
			break
	if flag:
		print(-1)
	else:
		print(m)


from sys import stdin, stdout, setrecursionlimit
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
from bisect import bisect, bisect_left
from time import time
from itertools import permutations as per
from heapq import heapify, heappush, heappop, heappushpop
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\r\n')
L = lambda : list(R())
P = lambda x: stdout.write(str(x) + '\n')
lcm = lambda x, y: x * y // gcd(x, y)
nCr = lambda x, y: f[x] * inv(f[y] * f[x - y] % N) % N
inv = lambda x: pow(x, N - 2, N)
sumx = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	n = I()
	a = L()
	e = []
	p = [0] * n
	for i in a:
		if i <= n and (not p[i - 1]):
			p[i - 1] = i
		else:
			e += (i,)
	e.sort(reverse=True)
	ans = 0
	for i in range(n):
		if not p[i]:
			if (e.pop() + 1) // 2 - 1 < i + 1:
				ans = -1
				break
			ans += 1
	print(ans)


T = int(input())
for t in range(T):
	n = int(input())
	lst = list(map(int, input().split()))
	dct = dict()
	for i in range(1, n + 1):
		dct[i] = 0
	flag = 1
	temp = []
	for i in lst:
		if i <= n and dct[i] == 0:
			dct[i] += 1
		else:
			temp.append(i)
	temp.sort()
	j = 0
	for i in range(1, n + 1):
		if dct[i] == 1:
			continue
		elif temp[j] > 2 * i:
			j += 1
		else:
			flag = 0
			break
	if flag == 0:
		print(-1)
	else:
		print(j)


import sys
import math as mt
import collections as cc
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
mod = 10 ** 9 + 7

def ch(temp):
	tans = 0
	for i in range(len(temp) - k - 1, -1, -k):
		tans += 2 * abs(temp[i])
	return tans
for tc in range(int(input())):
	(n,) = I()
	ar = I()
	ar.sort()
	visi = cc.defaultdict(int)
	temp = []
	x = []
	c = cc.Counter(ar)
	for i in range(1, n + 1):
		if c[i]:
			visi[i] = 1
			continue
		else:
			x.append(i)
	xx = []
	for i in ar:
		if visi[i]:
			visi[i] = 0
			continue
		xx.append(i)
	tf = 1
	x.sort()
	xx.sort()
	for i in range(len(x)):
		req = x[i]
		hand = (xx[i] - 1) // 2
		if hand >= req:
			continue
		tf = 0
	if tf:
		print(len(x))
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * (n + 1)
	c = []
	for i in range(n):
		if 1 <= a[i] <= n and b[a[i]] == 0:
			b[a[i]] = a[i]
		else:
			c.append(a[i])
	c.sort(reverse=True)
	count = 0
	key = 0
	for i in range(1, n + 1):
		if b[i] == 0:
			if i * 2 >= c.pop():
				key = 1
				break
			else:
				count = count + 1
	if key == 0:
		print(count)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	t = set(range(1, n + 1))
	t_a = set()
	t_d = []
	for i in range(n):
		if a[i] in t and a[i] not in t_a:
			t_a.add(a[i])
		else:
			t_d.append(a[i])
	dum = list(t - t_a)
	pr = 0
	dum.sort()
	t_d.sort()
	for i in range(len(t_d)):
		if (t_d[i] - 1) // 2 < dum[i]:
			pr = -1
			break
	if pr:
		print(-1)
	else:
		print(len(t_d))


def replace_bool(list_a, index, p_list):
	list_a[p_list[index]] = True
	return list_a

def solve():
	n = int(input())
	main = [int(x) for x in input().split()]
	(a, bigger) = ([False] * (n + 1), [])
	[replace_bool(a, i, main) if main[i] <= n and a[main[i]] is False else bigger.append(main[i]) for i in range(n)]
	bigger = sorted(bigger)
	counter = 0
	for i in range(1, n + 1):
		if not a[i]:
			if (bigger[counter] - 1) // 2 >= i:
				counter += 1
			else:
				return -1
	return counter
try:
	print(*[solve() for _ in range(int(input()))], sep='\n')
except Exception as error:
	print(error)


import bisect
mod = 10 ** 9 + 7
eps = 10 ** (-9)

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

def __lcm(a, b):
	return a * b / __gcd(a, b)

def __fact(n):
	return 1 if n == 1 else n * __fact(n - 1)

def __mex(a):
	mex = 0
	a.sort()
	for x in a:
		if x <= mex:
			mex += 1
		else:
			break
	return mex

def __dist(x1, y1, x2, y2):
	return (x1 - x2) ** 2 + (y1 - y2) ** 2

def __getprimes(n):
	isprime = [True for i in range(n + 1)]
	primes = []
	for i in range(2, n + 1):
		if isprime[i]:
			for j in range(i * i, n + 1, i):
				isprime[j] = False
	for i in range(2, n + 1):
		if isprime[i]:
			primes.append(i)
	return primes

def __modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x

def __isprime(n):
	if n < 2:
		return False
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def __cntprimediv(n):
	ret = 0
	x = n
	i = 2
	while i * i <= x:
		while n % i == 0:
			n //= i
			ret += 1
		i += 1
	if n > 1:
		ret += 1
	return ret

def __primefactors(n):
	ret = []
	x = n
	i = 2
	while i * i <= x:
		while n % i == 0:
			ret.append(i)
			n //= i
		i += 1
	if n > 1:
		ret.append(n)
	return ret

def __sumdigit(n):
	ret = 0
	while n > 0:
		ret += n % 10
		n //= 10
	return ret

def __zfunc(s):
	n = len(s)
	z = [0 for i in range(n)]
	l = 0
	r = 0
	for i in range(1, n):
		if r >= i:
			z[i] = min(z[i - l], r - i + 1)
		while z[i] + i < n and s[z[i]] == s[z[i] + i]:
			z[i] += 1
		if i + z[i] - 1 > r:
			l = i
			r = i + z[i] - 1
	return z

def __to(n, x):
	ret = ''
	while n > 0:
		q = n % x
		if q < 10:
			ret += str(q)
		else:
			ret += chr(q - 10 + ord('a'))
		n //= x
	return ret[::-1]

def solve(t):
	n = int(input())
	a = list(map(int, input().split()))
	needs = [True for i in range(n + 1)]
	v = []
	vv = []
	for i in range(n):
		if a[i] <= n and needs[a[i]]:
			needs[a[i]] = False
		else:
			vv.append(a[i])
	for i in range(1, n + 1):
		if needs[i]:
			v.append(i)
	vv.sort()
	cnt = 0
	for i in range(len(v)):
		if v[i] * 2 < vv[i]:
			cnt += 1
		else:
			cnt = -1
			break
	print(cnt)
t = 1
t = int(input())
for i in range(t):
	solve(i + 1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	d = dict()
	for i in range(1, n + 1):
		d[i] = 0
	li = []
	for i in range(n):
		if a[i] in d:
			if d[a[i]] == 0:
				d[a[i]] = 1
			else:
				li.append(a[i])
		else:
			li.append(a[i])
	rem = []
	for i in d:
		if d[i] == 0:
			rem.append(i)
	rem.sort()
	j = 0
	li.sort()
	le = len(li)
	y = 0
	c = 0
	for i in range(le):
		if li[i] % 2 == 0:
			li[i] -= 2
			if rem[j] <= li[i] // 2:
				j += 1
				c += 1
			else:
				y = 1
				break
		else:
			li[i] -= 1
			if rem[j] <= li[i] // 2:
				j += 1
				c += 1
			else:
				y = 1
				break
	if c == le:
		print(c)
	else:
		print(-1)


import sys, math
DEBUG = 0
if DEBUG == 1:
	sys.stdin = open('C-1.in', 'r')

def readi():
	return [int(t) for t in input().split()]

def output(a, i):
	for j in range(a[i]):
		print(chr(i + ord('a')), end='')

def solve(n, a):
	vis = [0] * (n + 1)
	a.sort()
	b = []
	for i in range(n):
		if a[i] >= 1 and a[i] <= n and (vis[a[i]] == 0):
			vis[a[i]] = 1
		else:
			b.append(a[i])
	b.sort()
	j = 0
	for i in range(1, n + 1):
		if vis[i] == 0:
			if i < b[j] / 2:
				j += 1
			else:
				print(-1)
				return
	print(len(b))

def main():
	(tc,) = readi()
	for i in range(tc):
		(n,) = readi()
		a = readi()
		solve(n, a)
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	done = {}
	for i in range(n):
		if a[i] <= n and a[i] >= 1:
			if done.get(a[i]):
				done[a[i]] += 1
			else:
				done[a[i]] = 1
	notdone = []
	req = []
	for i in a:
		if done.get(i) == None:
			notdone.append(i)
	for i in range(1, n + 1):
		if done.get(i) == None:
			req.append(i)
	for i in done:
		if done[i] > 1:
			notdone.extend([i] * (done[i] - 1))
	notdone.sort()
	req.sort()
	valid = True
	for i in range(len(req)):
		if notdone[i] - req[i] <= req[i]:
			valid = False
			break
	if valid:
		print(len(req))
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = sorted([int(i) for i in input().split()])
	v = [False] * (n + 1)
	arr = []
	(i, res) = (0, 0)
	for k in a:
		i += 1
		if k <= n and v[k] == False:
			v[k] = True
		else:
			arr += [k]
	j = 0
	for i in range(n):
		if v[i + 1]:
			continue
		if arr[j] >= 2 * (i + 1) + 1:
			res += 1
			j += 1
			continue
		res = -1
		break
	print(res)


def getint():
	return [int(i) for i in input().split()]

def get():
	return int(input())

def getstr():
	return [i for i in input().split()]

def S():
	for test in range(int(input())):
		solve()
import math
import itertools as it
import bisect
import time
import collections as ct

def lower_bound(a, x):
	l = -1
	r = len(a)
	while l + 1 != r:
		mid = l + r >> 1
		if a[mid] < x:
			l = mid
		else:
			r = mid
	return r

def upper_bound(a, x):
	l = -1
	r = len(a)
	while l + 1 != r:
		mid = l + r >> 1
		if a[mid] <= x:
			l = mid
		else:
			r = mid
	return r

def solve():
	n = get()
	a = getint()
	a.sort()
	cha = list(range(1, n + 1))
	cha = [0] + cha
	dic = {}
	for i in range(n):
		if 1 <= a[i] <= n and dic.get(a[i], 0) != 1:
			cha[a[i]] = 0
			dic[a[i]] = 1
			a[i] = 0
	check = []
	for i in range(n):
		if a[i] != 0:
			check.append(a[i])
	C = []
	for i in cha:
		if i != 0:
			C.append(i)
	for i in range(len(check)):
		if check[i] & 1:
			check[i] //= 2
		else:
			check[i] = check[i] // 2 - 1
	if len(check) != len(C):
		print(-1)
		return
	check.sort()
	C.sort()
	for i in range(len(check)):
		if check[i] < C[i]:
			print(-1)
			return
	print(len(check))
S()


import sys

def paprika_and_permutation(n, a):
	total = 0
	p = set(range(1, n + 1))
	a_ = []
	for ai in a:
		if ai in p:
			p.remove(ai)
		else:
			a_ += [ai]
	for (pi, ai) in zip(sorted(p), a_):
		if ai == pi:
			continue
		if pi * 2 >= ai:
			return -1
		total += 1
	return total
for _ in range(int(input())):
	n = int(input())
	a = sorted(map(int, input().split()))
	sys.stdout.write(f'{paprika_and_permutation(n, a)}\n')


nt = int(input())
ans = []

def soln(arr, n):
	if n == 1 and arr[0] == 2:
		return -1
	p = []
	q = []
	count = 0
	arr.sort()
	d = {}
	for x in range(n):
		d[x + 1] = 0
	for x in range(n):
		if arr[x] <= n and arr[x] >= 1:
			if d[arr[x]] == 0:
				d[arr[x]] = 1
			else:
				p.append(arr[x])
		else:
			p.append(arr[x])
	for x in range(n):
		if d[x + 1] == 0:
			q.append(x + 1)
	t = 0
	for x in range(len(p)):
		if p[x] > 2 * q[x]:
			count = count + 1
		else:
			count = -1
			break
	return count
for x in range(nt):
	n = int(input())
	ans.append(soln(list(map(int, input().split())), n))
for a in ans:
	print(a)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	flag = 0
	count = 0
	brr = [0] * n
	k = 0
	for i in range(n):
		while brr[k] == 1:
			k += 1
		if arr[i] <= n and brr[arr[i] - 1] == 0:
			brr[arr[i] - 1] = 1
			continue
		if arr[i] % 2 == 0:
			d = arr[i] // 2 - 1
		else:
			d = arr[i] // 2
		if d >= k + 1:
			brr[k] = 1
			count += 1
	if 0 in brr:
		print(-1)
		continue
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	c = []
	b = [0] * n
	for j in range(n):
		if a[j] <= n and b[a[j] - 1] != 1:
			b[a[j] - 1] = 1
		else:
			c.append(a[j])
	c.sort()
	j = 0
	k = 0
	s = 0
	f = 0
	while k < n:
		if b[k] == 1:
			k += 1
			continue
		if c[j] % 2 == 0:
			if c[j] // 2 - 1 >= k + 1:
				s += 1
				j += 1
				b[k] = 1
			else:
				f = 1
				break
		elif c[j] // 2 >= k + 1:
			s += 1
			j += 1
			b[k] = 1
		else:
			f = 1
			break
	if f == 1:
		print(-1)
	else:
		print(s)


import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq

def input():
	return sys.stdin.readline()[:-1]
INF = 10 ** 10

def solve(N, A):
	A.sort(reverse=True)
	B = collections.deque(A)
	dic = collections.defaultdict(int)
	ans = 0
	for a in A:
		dic[a] += 1
	for i in range(N):
		target = N - i
		if dic[target]:
			dic[target] -= 1
			continue
		while B:
			b = B.popleft()
			if dic[b] and target * 2 + 1 <= b:
				dic[b] -= 1
				ans += 1
				break
		else:
			return -1
	return ans
T0 = int(input())
for _ in range(T0):
	N = int(input())
	A = list(map(int, input().split()))
	print(solve(N, A))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = []
	length_b = 0
	a.sort()
	theta = 0
	while theta < n and a[theta] <= n:
		if length_b == 0 or b[-1] != a[theta]:
			b.append(a[theta])
			a[theta] = -1
			length_b += 1
		theta += 1
	operations = 0
	a.reverse()
	b.reverse()
	delta = n
	cur_a = 0
	cur_b = 0
	while delta > 0:
		if cur_b < length_b and b[cur_b] == delta:
			cur_b += 1
		else:
			while cur_a < n and a[cur_a] == -1:
				cur_a += 1
			if cur_a < n:
				if a[cur_a] > 2 * delta:
					operations += 1
				else:
					operations = -1
					break
			cur_a += 1
		delta -= 1
	print(operations)


import math, sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
mod1 = 1000000007
mod2 = 998244353
for _ in range(I()):
	n = I()
	a = L()
	l = [0] * (n + 1)
	f = 0
	b = [0] * (n + 1)
	for i in range(n):
		if 1 <= a[i] <= n and b[a[i]] == 0:
			b[a[i]] += 1
			continue
		l[min(n, a[i] - a[i] // 2 - 1)] += 1
	s = set(a)
	c = 0
	for i in range(n - 1, 0, -1):
		if i + 1 in s:
			l[i] += l[i + 1]
		else:
			l[i] += l[i + 1] - 1
	for i in range(1, n + 1):
		if l[i] == 0 and i not in s:
			f = 1
			break
	if f == 1:
		print(-1)
		continue
	for i in range(1, n + 1):
		if i not in s:
			c += 1
	print(c)


for _ in range(int(input())):
	check = {}
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	R = []
	for i in range(n):
		if a[i] <= n and (i == 0 or a[i] != a[i - 1]):
			if not a[i] in check:
				check[a[i]] = 1
			pass
		else:
			R.append((a[i] - 1) // 2)
	ans = 0
	j = len(R) - 1
	for i in range(n, 0, -1):
		if not i in check:
			if j >= 0 and R[j] >= i:
				ans += 1
				j -= 1
			else:
				ans = -1
				break
	print(ans)


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	c = [0] * (n + 1)
	d = [0] * (n + 1)
	ans = 0
	for i in range(n):
		if a[i] <= n:
			c[a[i]] += 1
		else:
			d[min((a[i] - 1) // 2, n)] += 1
			ans += 1
	for i in range(n + 1):
		if c[i] <= 1:
			continue
		d[(i - 1) // 2] += c[i] - 1
		ans += c[i] - 1
		c[i] = 1
	for i in range(n + 1):
		c[i] += d[i]
	val = 0
	for i in range(n, 0, -1):
		val -= 1
		val += c[i]
		if val < 0:
			break
	if val < 0:
		print(-1)
	else:
		print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	t = set(range(1, n + 1))
	t_s = set()
	t_l = []
	for i in range(n):
		if a[i] in t and a[i] not in t_s:
			t_s.add(a[i])
		else:
			t_l.append(a[i])
	new = sorted(list(t - t_s))
	cnt = 0
	t_l.sort()
	for i in range(len(t_l)):
		if (t_l[i] - 1) // 2 < new[i]:
			cnt = -1
			break
	if cnt:
		print(-1)
	else:
		print(len(t_l))


import sys
input = sys.stdin.readline

def inp():
	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())
t = inp()
for _ in range(t):
	n = inp()
	A = inlt()
	st = set()
	for i in range(n, 0, -1):
		st.add(i)
	B = []
	for a in A:
		if a in st:
			st.remove(a)
		else:
			B.append(a)
	B = sorted(B)
	C = list(st)
	res = 0
	for b in B[::-1]:
		s = C.pop()
		if b > s * 2:
			st.pop()
			res += 1
		else:
			print(-1)
			break
	else:
		print(res)


def solve(n, a):
	c = [0] * n
	b = []
	for ai in a:
		if 1 <= ai <= n and c[ai - 1] == 0:
			c[ai - 1] = 1
		else:
			b.append(ai)
	b.sort()
	(idx, b_idx) = (0, 0)
	count = 0
	while idx < n:
		while idx < n and c[idx]:
			idx += 1
		if idx == n:
			break
		num = b[b_idx]
		if num % (num - idx - 1) == idx + 1:
			count += 1
		else:
			return -1
		b_idx += 1
		idx += 1
	return count

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		print(solve(n, a))
main()


def solve(l, n):
	v = [0] * (n + 1)
	r = []
	ans = 0
	for i in range(n):
		if l[i] <= n and v[l[i]] == 0:
			v[l[i]] = l[i]
		else:
			r.append(l[i])
	r.sort(reverse=True)
	for i in range(1, n + 1):
		if v[i] == 0:
			if 2 * i >= r.pop():
				print(-1)
				return
			ans += 1
	print(ans)
t = int(input())
while t:
	n = int(input())
	l = [int(i) for i in input().split(' ')]
	solve(l, n)
	t -= 1


from math import ceil
from collections import Counter
for i in range(int(input())):
	n = int(input())
	a = Counter([*map(int, input().split())])
	us = {}
	aa = a.copy()
	for i in aa:
		a[ceil(i / 2) - 1] += aa[i] - [1, 0][i > n]
		us[i] = 1
		a[i] -= aa[i]
	ss = [*sorted(set(a), reverse=1)] + [0]
	c = 0
	k = 0
	res = 0
	while ss[k] > n:
		res += a[ss[k]]
		k += 1
	F = 0
	ss = [int(10000000000.0)] + ss
	for i in range(n, 0, -1):
		if i == ss[k + 1]:
			k += 1
			res += a[ss[k]]
		if us.get(i, 0) == 1:
			pass
		elif res > 0:
			c += 1
			res -= 1
		else:
			F = 1
	if F:
		print(-1)
	else:
		print(c)


from collections import Counter
tc = int(input())
for case in range(1, tc + 1):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	check = [False for i in range(n + 1)]
	for i in range(1, n + 1):
		if i in c and c[i] > 0:
			c[i] -= 1
			check[i] = True
	ans = 0
	c = sorted(([k, v] for (k, v) in c.items()))
	ci = 0
	for i in range(1, n + 1):
		if check[i] == False:
			while ci < len(c) and c[ci][1] == 0:
				ci += 1
			if ci < len(c) and i <= (c[ci][0] // 2 - 1 if c[ci][0] % 2 == 0 else c[ci][0] // 2):
				c[ci][1] -= 1
				check[i] = True
				ans += 1
			else:
				print(-1)
				break
	else:
		print(ans)


import os
import sys
from io import BytesIO, IOBase
import math
from decimal import Decimal
from decimal import *
from collections import defaultdict, deque
import heapq
import bisect
from decimal import Decimal
getcontext().prec = 25
abcd = 'abcdefghijklmnopqrstuvwxyz'
ABCD = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
months = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
MOD = 1000000007
BUFSIZE = 8192
from bisect import bisect_left, bisect_right

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	l = sorted(list(map(int, input().split(' '))))
	ll = [0 for i in range(n + 1)]
	extra = []
	for i in l:
		if i > n or ll[i]:
			extra.append(i)
		else:
			ll[i] = 1
	ans = 0
	extra.sort()
	j = 0
	for i in range(1, n + 1):
		if ll[i]:
			continue
		if (extra[j] - 1) // 2 < i:
			ans = -1
			break
		ans += 1
		j += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a_min = {}
	a_max = []
	awn = n
	for i in input().split():
		i = int(i)
		if i <= n:
			if a_min.get(i, 0) == 1:
				a_max.append(i)
			else:
				a_min[i] = 1
				awn -= 1
		else:
			a_max.append(i)
	a_max.sort()
	i0 = 0
	for i in range(1, n + 1):
		if a_min.get(i, 0) == 0:
			if a_max[i0] <= i * 2:
				awn = -1
				break
			i0 += 1
	print(awn)


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	done = [0] * (n + 1)
	for i in range(n):
		if a[i] <= n:
			done[a[i]] += 1
	ans = 0
	for i in range(n + 1):
		if done[i] > 1:
			done[(i - 1) // 2] += done[i] - 1
			ans += done[i] - 1
			done[i] = 1
	for i in range(n):
		if a[i] > n:
			x = (a[i] - 1) // 2
			x = min(x, n)
			done[x] += 1
			ans += 1
	cur = 0
	flag = 0
	for i in range(n, 0, -1):
		cur += done[i]
		cur -= 1
		if cur < 0:
			flag = 1
			break
	if flag:
		print(-1)
	else:
		print(ans)


from sys import stdin, stdout
nmbr = lambda : int(input())
lst = lambda : list(map(int, input().split()))
for _ in range(nmbr()):
	n = nmbr()
	a = sorted(lst())
	p = n - 1
	pos = {}
	used = set()
	for i in range(n):
		pos[a[i]] = i
	ans = 0
	f = 1
	for req in range(n, 0, -1):
		if req in pos:
			used.add(pos[req])
			continue
		while p >= 0 and p in used:
			p -= 1
		if p >= 0 and a[p] > 2 * req:
			p -= 1
			ans += 1
		else:
			f = 0
			break
	print(ans if f else -1)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	check = defaultdict(int)
	l2 = []
	for i in range(n):
		if not (l[i] >= 1 and l[i] <= n and (not check[l[i]])):
			l2.append(l[i])
		else:
			check[l[i]] = 1
	l2 = sorted(l2)
	l3 = [0] * (n + 1)
	for j in check.keys():
		l3[j] = 1
	ans = 0
	flag = 1
	for i in range(1, n + 1):
		if not l3[i]:
			if not i <= l2[ans] // 2 - (l2[ans] % 2 == 0):
				flag = 0
				break
			else:
				ans += 1
				l3[i] = 1
	if not flag:
		print(-1)
	else:
		print(len(l2))


def main():
	import sys
	input = sys.stdin.buffer.readline
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		a2 = [((i - 1) // 2, ind) for (ind, i) in enumerate(a)]
		a2.sort()
		p = [0] * n
		visit = [0] * n
		s = set()
		s2 = set()
		for (ind, i) in enumerate(a):
			if i <= n and i not in s:
				visit[i - 1] = 1
				p[ind] = i
				s.add(i)
				s2.add(ind)
		i = 0
		for (x, ind) in a2:
			if ind in s2:
				continue
			while visit[i]:
				i += 1
			if i >= x:
				print(-1)
				break
			else:
				visit[i] = 1
				i += 1
		else:
			print(p.count(0))
main()


def pp(n, l):
	lnt = []
	v = [0] * (n + 1)
	i = 0
	while i < len(l):
		if l[i] <= n:
			if v[l[i]] == 0:
				v[l[i]] += 1
			else:
				lnt.append(l[i])
		else:
			lnt.append(l[i])
		i = i + 1
	lnt.sort()
	lnv = []
	i = 1
	while i < len(v):
		if v[i] == 0:
			lnv.append(i)
		i = i + 1
	count = 0
	for i in range(len(lnt)):
		if (lnv[i] < lnt[i] / 2) & (lnv[i] >= 1):
			count = count + 1
		else:
			return -1
	return count
t = int(input())
ln = []
ls = []
for x in range(t):
	n = int(input())
	s = str(input())
	ln.append(n)
	ls.append(s)
lop = []
for i in range(len(ln)):
	n = ln[i]
	s = ls[i]
	s1 = s.split(' ')
	l = []
	for x in s1:
		l.append(int(x))
	ans = pp(n, l)
	lop.append(ans)
for x in lop:
	print(x)


import heapq
from collections import *
import math

def solve():
	n = int(input())
	a = [int(i) for i in input().split()]
	d = defaultdict(int)
	t = []
	for i in a:
		d[i] += 1
		if d[i] > 1 and d[i] <= n:
			t.append(i)
		elif i > n:
			t.append(i)
	t.sort(reverse=True)
	k = len(t)
	for i in range(n):
		if d.get(i + 1, 0) == 0:
			y = i + 1
			l = t.pop()
			if l % (l - y) == y:
				continue
			else:
				print(-1)
				return
	print(k)
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	fnums = {}
	for x in nums:
		fnums[x] = fnums.get(x, 0) + 1
	missing = []
	for x in range(1, n + 1):
		if x in fnums:
			fnums[x] -= 1
			if fnums[x] == 0:
				del fnums[x]
		else:
			missing.append(x)
	remaining = []
	for (x, f) in fnums.items():
		remaining.extend([x] * f)
	remaining.sort()
	if all((m <= (r - 1) // 2 for (m, r) in zip(missing, remaining))):
		print(len(missing))
	else:
		print(-1)


import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
from collections import defaultdict
BUFSIZE = 8192

def bootstrap(f, stack=[]):

	def fun(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return fun

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	poss = 1
	b = list(map(int, input().split()))
	cnt = [0 for i in range(n + 1)]
	ans = []
	for j in b:
		if j <= n and cnt[j] == 0:
			cnt[j] += 1
		else:
			ans.append(j)
	l = len(ans)
	curr = []
	j = n
	while j >= 1:
		if cnt[j] == 0:
			curr.append(j)
		j += -1
	ans.sort(reverse=True)
	l = min(l, len(curr))
	j = 0
	while j < l:
		if ans[j] > 2 * curr[j]:
			pass
		else:
			poss = 0
		j += 1
	if poss:
		print(l)
	else:
		print(-1)


class SegTree:

	def __init__(self, init_val, ide_ele, segfunc):
		self.n = len(init_val)
		self.num = 2 ** (self.n - 1).bit_length()
		self.ide_ele = ide_ele
		self.segfunc = segfunc
		self.seg = [ide_ele] * 2 * self.num
		for i in range(self.n):
			self.seg[i + self.num] = init_val[i]
		for i in range(self.num - 1, 0, -1):
			self.seg[i] = self.segfunc(self.seg[2 * i], self.seg[2 * i + 1])

	def update(self, k, x):
		k += self.num
		self.seg[k] = x
		while k:
			k = k >> 1
			self.seg[k] = self.segfunc(self.seg[2 * k], self.seg[2 * k + 1])

	def query(self, l, r):
		if r <= l:
			return self.ide_ele
		l += self.num
		r += self.num
		lres = self.ide_ele
		rres = self.ide_ele
		while l < r:
			if r & 1:
				r -= 1
				rres = self.segfunc(self.seg[r], rres)
			if l & 1:
				lres = self.segfunc(lres, self.seg[l])
				l += 1
			l = l >> 1
			r = r >> 1
		res = self.segfunc(lres, rres)
		return res

	def __str__(self):
		arr = [self.query(i, i + 1) for i in range(self.n)]
		return str(arr)
import sys
import io, os
input = sys.stdin.buffer.readline
import heapq
from collections import Counter
INF = 10 ** 18
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	ans = 0
	C = Counter(A)
	seg = SegTree(list(range(1, n + 1)), -INF, max)
	for a in A:
		if 1 <= a <= n:
			seg.update(a - 1, -INF)
	A.sort(reverse=True)
	flag = True
	for a in A:
		if a > n:
			r = (a - 1) // 2
			b = seg.query(0, min(seg.n, r))
			if b == -INF:
				flag = False
				break
			ans += 1
			seg.update(b - 1, -INF)
		elif C[a] >= 2:
			r = (a - 1) // 2
			b = seg.query(0, min(seg.n, r))
			if b == -INF:
				flag = False
				break
			C[a] -= 1
			seg.update(b - 1, -INF)
			ans += 1
		else:
			continue
	if flag:
		print(ans)
	else:
		print(-1)


import sys
input = sys.stdin.readline

def iinput():
	return int(input())

def sinput():
	return input().rstrip()

def i0input():
	return int(input()) - 1

def linput():
	return list(input().split())

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

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

def li0input():
	return list(map(lambda x: int(x) - 1, input().split()))

def mi0input():
	return map(lambda x: int(x) - 1, input().split())
INF = 10 ** 20
MOD = 1000000007

def solve(n, A):
	B = []
	C = [False] * n
	for a in A:
		if 1 <= a <= n and C[a - 1] == False:
			C[a - 1] = True
		else:
			B.append(a)
	D = []
	for c in range(n):
		if not C[c]:
			D.append(c + 1)
	B.sort()
	D.sort()
	for (b, d) in zip(B, D):
		if b >= 2 * d + 1:
			continue
		else:
			print(-1)
			return
	print(len(B))
t = iinput()
for _ in [0] * t:
	n = iinput()
	A = liinput()
	solve(n, A)


from sys import stdin, stdout
rd = lambda : list(map(int, stdin.readline().split()))
rds = lambda : stdin.readline().rstrip()
ii = lambda : int(stdin.readline())
INF = 1 << 62
mod = 10 ** 9 + 7
for _ in range(ii()):
	n = ii()
	ar = rd()
	p = set((i for i in range(1, n + 1)))
	b = []
	for x in ar:
		if x in p:
			p.discard(x)
		else:
			b.append(x)
	limits = []
	for x in b:
		if x % 2 == 1:
			limits.append(x // 2)
		else:
			limits.append(x // 2 - 1)
	p = list(p)
	p.sort()
	limits.sort()
	res = len(p)
	for (x, y) in zip(p, limits):
		if y < x:
			res = -1
			break
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	a.sort()
	had = [0] * n
	un_know = []
	for i in range(len(a)):
		if a[i] <= n and had[a[i] - 1] == 0:
			had[a[i] - 1] = 1
		else:
			un_know.append(a[i])
	un_know.sort()
	j = 0
	for i in range(n):
		if had[i] == 0:
			if un_know[j] - un_know[j] // 2 - 1 >= i + 1:
				j += 1
			else:
				j = -1
				break
		if j >= len(un_know):
			break
	print(j)