Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

Chef and his little brother are playing with sticks. They have total N sticks. Length of i-th stick is A_{i}. 
Chef asks his brother to choose any four sticks and to make a rectangle with those sticks its sides. Chef warns his brother to not to break any of the sticks, he has to use sticks as a whole. Also, he wants that the rectangle formed should have the maximum possible area among all the rectangles that Chef's brother can make. 

Chef's little brother takes this challenge up and overcomes it. Can you also do so? That is, you have to tell whether it is even possible to create a rectangle? If yes, then you have to tell the maximum possible area of rectangle.
 
------ Input ------ 

The first line contains a single integer T denoting the number of test-cases. T test cases follow.
The first line of each test case contains a single integer N denoting the number of sticks.
The second line of each test case contains N space-separated integers A_{1}, A_{2}, ..., A_{N} denoting the lengths of sticks.

------ Output ------ 

For each test case, output a single line containing an integer representing the maximum possible area for rectangle or -1 if it's impossible to form any rectangle using the available sticks.

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

$1 ≤ T ≤ 100$
$1 ≤ N ≤ 10^{3}$
$1 ≤ sum of N's over all test-cases in a single test file ≤ 10^{3}$
$1 ≤ A_{i} ≤ 10^{3}$

----- Sample Input 1 ------ 
2
5
1 2 3 1 2
4
1 2 2 3
----- Sample Output 1 ------ 
2
-1
----- explanation 1 ------ 
Example case 1. Chef's brother can choose sticks of lengths 1, 2, 1, 2. He can create a rectangle with area 1 * 2 = 2.
Example case 2. It's impossible to choose 4 sticks so that they form a rectangle.

Source: codechef | Difficulty: EASY
Tags: Complete search

Greedy algorithms


Solutions:
t = int(input())
for ni in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	s = list(set(l))
	s = s[-1::-1]
	x = 0
	s1 = list()
	for i in s:
		c = l.count(i)
		if c >= 2:
			x = x + c // 2 * 2
			s1.append(i)
		if x >= 4:
			break
	if len(s1) == 0 or x < 4:
		print(-1)
	elif len(s1) == 1:
		print(s1[0] * s1[0])
	else:
		print(s1[0] * s1[1])


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = list(set(l))
	ans = []
	for i in s:
		for j in range(int(n / 2)):
			if l.count(i) >= (j + 1) * 2:
				ans.append(i)
	ans.sort()
	if len(ans) < 2:
		print(-1)
	else:
		print(ans[-2] * ans[-1])


for _ in range(int(input())):
	n = int(input())
	side = list(map(int, input().split()))
	f = {}
	v = []
	for i in side:
		if i in f:
			f[i] += 1
		else:
			f[i] = 1
			v.append(i)
	v.sort()
	feasible = []
	for i in range(len(v) - 1, -1, -1):
		if len(feasible) == 2:
			break
		if f[v[i]] >= 4:
			if len(feasible) == 0:
				feasible.append(v[i])
				feasible.append(v[i])
			elif len(feasible) == 1:
				feasible.append(v[i])
		elif 2 <= f[v[i]] < 4:
			feasible.append(v[i])
	if len(feasible) < 2:
		print(-1)
	elif feasible[0] * feasible[1] == 0:
		print(-1)
	else:
		print(feasible[0] * feasible[1])


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = 0
	h = 0
	li = [int(x) for x in input().split()]
	li.sort(reverse=True)
	for i in range(n - 1):
		if li[i] == li[i + 1]:
			if l == 0:
				l = li[i]
				li[i] = 0
				li[i + 1] = 0
			else:
				h = li[i]
				li[i] = 0
				li[i + 1] = 0
			if l * h != 0:
				break
	if l * h == 0:
		print(-1)
	else:
		print(l * h)


from collections import Counter
from itertools import combinations
for tc in range(int(input())):
	ans = 0
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	sides = [k for (k, v) in c.items() if v > 1]
	sides4 = [k for (k, v) in c.items() if v > 3]
	sides.extend(sides4)
	if len(sides) < 2:
		ans = -1
	else:
		areas = [i * j for (i, j) in combinations(sides, 2)]
		ans = max(areas)
	print(ans)


import math
for _ in range(int(input())):
	a = int(input())
	l = list(map(int, input().split()))
	l1 = list(set(l))
	n = []
	for i in l1:
		if l.count(i) >= 2:
			for j in range(0, l.count(i) // 2):
				n.append(i)
	n.sort()
	if len(n) > 1:
		print(n[-2] * n[-1])
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().strip().split()))
	a.sort()
	d = {}
	lst = []
	for i in a:
		if d.get(i) is None:
			d[i] = 1
		else:
			d[i] += 1
		if d[i] == 2 or d[i] == 4:
			lst.append(i)
	lst.sort()
	if len(lst) < 2:
		print(-1)
	else:
		print(lst[-2] * lst[-1])


t = int(input())
for x in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	b = 0
	c = 0
	for i in range(len(a) - 1, 0, -1):
		if a.count(a[i]) >= 2:
			b = a[i]
			a.pop(i)
			a.pop(i - 1)
			break
	for j in range(len(a) - 1, 0, -1):
		if a.count(a[j]) >= 2:
			c = a[j]
			a.pop(j)
			a.pop(j - 1)
			break
	if b != 0 and c != 0:
		print(b * c)
	elif b == 0 or c == 0:
		print('-1')


for m in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	s = list(set(a))
	s.sort()
	s.reverse()
	cont = 0
	ar = 1
	for i in s:
		if a.count(i) > 1 and cont < 2:
			ar *= i
			cont += 1
		if a.count(i) > 3 and cont < 2:
			ar *= i
			cont += 1
		if cont == 2:
			print(ar)
			break
	if cont < 2:
		print(-1)


for _ in range(int(input())):
	N = int(input())
	a = [int(_) for _ in input().split()]
	a.sort(reverse=True)
	d = dict()
	ans = []
	for x in a:
		if d.get(x) is None:
			d[x] = 1
		else:
			d[x] += 1
		if d[x] == 2 or d[x] == 4:
			ans.append(x)
	ans.sort(reverse=True)
	if len(ans) < 2:
		print(-1)
	else:
		print(ans[0] * ans[1])


T = int(input())
for _ in range(T):
	N = int(input())
	A = [int(_) for _ in input().split()]
	A = sorted(A, reverse=True)
	counter = dict()
	ans = []
	for x in A:
		if counter.get(x) is None:
			counter[x] = 1
		else:
			counter[x] += 1
		if counter[x] == 2 or counter[x] == 4:
			ans.append(x)
	ans = sorted(ans, reverse=True)
	if len(ans) < 2:
		print(-1)
	else:
		print(ans[0] * ans[1])


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = {}
	j = []
	for i in l:
		if i not in k.keys():
			k[i] = 1
		else:
			k[i] += 1
			if k[i] == 2:
				j.append(i)
			elif k[i] == 4:
				j.append(i)
	j.sort()
	if len(j) < 2:
		print(-1)
	else:
		print(j[-1] * j[-2])


t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	c = []
	for j in b:
		if a.count(j) >= 2:
			c.append(j)
		if a.count(j) >= 4:
			c.append(j)
	c.sort()
	d = c[::-1]
	if len(d) >= 2:
		print(d[0] * d[1])
	else:
		print(-1)


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	v = []
	b = set(s)
	for j in b:
		if s.count(j) >= 2:
			v.append(j)
		if s.count(j) >= 4:
			v.append(j)
	v.sort()
	d = v[::-1]
	if len(d) >= 2:
		print(d[0] * d[1])
	else:
		print('-1')


for i in range(int(input())):
	x = int(input())
	s = list(map(int, input().split()))
	s.sort(reverse=True)
	(max1, max2) = (0, 0)
	try:
		for i in range(x):
			if s[i] == s[i + 1] and (not max1):
				max1 = s[i]
				s.remove(max1)
				s.remove(max1)
			if s[i] == s[i + 1] and (not max2):
				max2 = s[i]
				s.remove(max2)
				s.remove(max2)
			if max1 * max2:
				print(max1 * max2)
				break
	except:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	l = []
	for j in s:
		if a.count(j) >= 2:
			for k in range(0, a.count(j) // 2):
				l.append(j)
	l.sort()
	if len(l) >= 2:
		print(l[len(l) - 1] * l[len(l) - 2])
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	sticks = list(map(int, input().split()))
	sticks.sort(reverse=True)
	(l, b) = (0, 0)
	i = 0
	while i < n:
		s = sticks[i]
		if not l:
			if i + 1 < n and sticks[i + 1] == s:
				l = s
				i += 2
				continue
		elif i + 1 < n and sticks[i + 1] == s:
			b = s
			print(l * b)
			break
		i += 1
	if not b:
		print(-1)


n = int(input())
for _ in range(n):
	x = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	l1 = []
	for i in d:
		if d[i] >= 2:
			for j in range(0, d[i] // 2):
				l1.append(i)
	if len(l1) < 2:
		print(-1)
		continue
	l1.sort()
	area = l1[-1] * l1[-2]
	print(area)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().strip().split()))
	a.sort()
	d = {}
	lst = []
	for i in a:
		if i not in d:
			d[i] = 0
		d[i] += 1
		if d[i] == 2 or d[i] == 4:
			lst.append(i)
	lst.sort()
	if len(lst) < 2:
		print(-1)
	else:
		print(lst[-2] * lst[-1])


for _ in range(int(input())):
	x = int(input())
	l = list(map(int, input().split()))
	l.sort()
	l1 = l[::-1]
	s = set(l1)
	c = 0
	d = []
	for i in s:
		for j in range(l1.count(i) // 2):
			d.append(i)
	if len(d) >= 2:
		print(d[-2] * d[-1])
	else:
		print(-1)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	l = []
	A.sort()
	dick = {}
	for val in A:
		if val not in dick:
			dick[val] = 0
		dick[val] += 1
		if dick[val] == 2 or dick[val] == 4:
			l.append(val)
	l.sort()
	if len(l) <= 1:
		print(-1)
	else:
		print(l[-1] * l[-2])


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


def RectangleArea(N, Length):
	Length.sort()
	Area = 1
	count1 = 0
	count2 = 0
	for i in range(N - 1, 0, -1):
		if Length[i] == Length[i - 1]:
			if count1 == 0:
				count1 = Length[i]
				(Length[i], Length[i - 1]) = (0, 0)
			elif count2 == 0:
				count2 = Length[i]
				(Length[i], Length[i - 1]) = (0, 0)
				break
	Area = count1 * count2
	if Area > 0:
		return Area
	else:
		return -1
T = int(input())
for i in range(T):
	N = int(input())
	Length = list(map(int, input().strip().split()))
	print(RectangleArea(N, Length))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	for i in {}.fromkeys(a):
		if a.count(i) >= 2:
			l.append(i)
		if a.count(i) >= 4:
			l.append(i)
	if len(l) == 1:
		print('-1')
		continue
	l.sort()
	print(l[-1] * l[-2])


for _ in range(int(input())):
	s = int(input())
	a = list(map(int, input().split()))
	x = list(set(a))
	c = []
	for i in x:
		x = a.count(i)
		if x >= 2:
			c.append(i)
		if x >= 4:
			c.append(i)
	c.sort()
	if len(c) <= 1:
		print(-1)
	else:
		print(c[-1] * c[-2])


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort(reverse=True)
	(area, temp, i) = (1, 0, 0)
	while i < n - 1 and temp != 2:
		if arr[i] == arr[i + 1]:
			area *= arr[i]
			temp += 1
			i += 2
		else:
			i += 1
	print(area if temp == 2 else -1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	for i in set(a):
		if a.count(i) >= 4:
			l.append(i)
			l.append(i)
		elif a.count(i) >= 2 and a.count(i) < 4:
			l.append(i)
	l.sort(reverse=True)
	if len(l) >= 2:
		print(l[0] * l[1])
	else:
		print(-1)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	l1 = []
	for x in s:
		if l.count(x) > 1:
			l1.append(x)
	if len(l1) == 0 or (len(l1) == 1 and l.count(l1[0]) < 4):
		print('-1')
	else:
		m = max(l1)
		l1.remove(m)
		if l.count(m) >= 4:
			print(m * m)
		else:
			m1 = max(l1)
			print(m * m1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	sort = sorted(l)[::-1]
	for i in sort[::-1]:
		if i not in d:
			d[i] = l.count(i)
	area = 1
	count = 0
	i = 0
	while i < n - 1 and count != 2:
		if sort[i] == sort[i + 1]:
			area = area * sort[i]
			i = i + 2
			count = count + 1
		else:
			i = i + 1
	if count == 2:
		print(area)
	else:
		print(-1)


for _ in range(int(input())):
	n = input()
	a = [int(x) for x in input().split()]
	d = {}
	for i in a:
		d[i] = d.get(i, 0) + 1
	b = []
	for (x, y) in d.items():
		b += [x] * (y // 2)
	if len(b) < 2:
		print(-1)
	else:
		b = sorted(b)
		print(b[-1] * b[-2])


T = int(input())
for i in range(T):
	N = int(input())
	arr = list(map(int, input().split(' ')))
	for i in range(1, 1001):
		if arr.count(i) == 1:
			arr.remove(i)
	arr.sort(reverse=True)
	area = 0
	if len(arr) > 3:
		print(arr[1] * arr[3])
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = set(l)
	l2 = []
	for i in l1:
		if l.count(i) > 3:
			l2.append(i)
			l2.append(i)
		elif l.count(i) == 2 or l.count(i) == 3:
			l2.append(i)
	if len(l2) < 2:
		print(-1)
	else:
		l2.sort(reverse=True)
		print(l2[0] * l2[1])


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		d[i] = 0
	for i in l:
		d[i] += 1
	d_ = d
	kl = sorted(list(d.keys()), reverse=True)
	d = {}
	for i in kl:
		d[i] = d_[i]
	s = []
	a = 0
	a_ = 0
	for i in d:
		if d[i] > 3:
			a = i * i
			break
	for i in d:
		if d[i] > 1 and len(s) == 0:
			s.append(i)
		elif d[i] > 1 and len(s) == 1:
			s.append(i)
			break
	if len(s) == 2:
		a_ = s[0] * s[1]
	if a == 0 and a_ == 0:
		print(-1)
	else:
		print(max(a, a_))


for tc in range(int(input())):
	N = int(input())
	S = list(map(int, input().split()))
	S = sorted(S, reverse=True)
	l = 0
	b = 0
	break_out_flag = False
	for i in range(N - 3):
		if S[i + 1] == S[i]:
			l = S[i]
			for j in range(i + 2, N - 1):
				if S[j] == S[j + 1]:
					b = S[j]
					break_out_flag = True
					break
		if break_out_flag:
			break
	if l * b == 0:
		print(-1)
	else:
		print(int(l * b))


for i in range(int(input())):
	a = int(input()) - 1
	d = list(map(int, input().split()))
	d.sort()
	l = 0
	b = 0
	while a > -1:
		if l == 0:
			if d[a] == d[a - 1]:
				l = d[a]
				a -= 1
		elif b == 0:
			if d[a] == d[a - 1]:
				b = d[a]
				a -= 1
		a -= 1
	if l * b == 0:
		print('-1')
	else:
		print(l * b)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	l = {}
	for i in range(len(a) - 1):
		if a[i] == a[i + 1]:
			if a[i] not in l:
				l[a[i]] = 2
			else:
				l[a[i]] += 1
	v = list(map(int, l.values()))
	k = list(map(int, l.keys()))
	if v[-1] >= 4:
		d = k[-1] ** 2
	elif len(v) >= 2:
		d = k[-1] * k[-2]
	else:
		d = -1
	print(d)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in a:
		if i not in b:
			if a.count(i) >= 2 and a.count(i) < 4:
				b.append(i)
			elif a.count(i) >= 4:
				b.append(i)
				b.append(i)
	b.sort()
	b.reverse()
	if len(b) < 2:
		print(-1)
	else:
		print(b[0] * b[1])


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	r = sorted({(i, l.count(i)) for i in l if l.count(i) > 1})
	p = -1
	if len(r) > 0:
		if r[-1][1] > 3:
			p = r[-1][0] ** 2
		elif len(r) > 1:
			p = r[-1][0] * r[-2][0]
	print(p)


try:
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, input().split()))
		l.sort()
		ans = 0
		temp = []
		for i in l:
			if i not in temp:
				if l.count(i) >= 2 and l.count(i) < 4:
					temp.append(i)
				elif l.count(i) >= 4:
					temp.append(i)
					temp.append(i)
		temp.sort(reverse=True)
		if len(temp) >= 2:
			print(temp[0] * temp[1])
		else:
			print(-1)
except:
	pass


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	arr.sort()
	cnt = 0
	l = []
	for i in range(n - 1, -1, -1):
		if 2 <= arr.count(arr[i]) < 4 and arr[i] not in l:
			cnt += 1
			l.append(arr[i])
		elif arr.count(arr[i]) >= 4 and arr[i] not in l:
			l.append(arr[i])
			l.append(arr[i])
			break
		if cnt == 2:
			break
	if len(l) >= 2:
		ar = l[0] * l[1]
		print(ar)
	else:
		print(-1)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	lst = sorted(list(map(int, input().split())))
	xr = Counter(lst)
	fxr = []
	for i in lst:
		if xr[i] >= 2 and i not in fxr:
			fxr.append(i)
	fxr.sort()
	if len(fxr) >= 2 or xr[fxr[-1]] >= 4:
		l = fxr[-1]
		if xr[l] >= 4:
			print(l * l)
		else:
			b = fxr[-2]
			print(l * b)
	else:
		print(-1)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(set(sorted(a)))
	c = []
	for i in b:
		c.append(a.count(i))
	d = []
	for i in range(len(b)):
		if c[i] >= 2:
			d.append([b[i], c[i]])
	if d[-1][1] >= 4:
		print(d[-1][0] ** 2)
	elif len(d) >= 2:
		print(d[-2][0] * d[-1][0])
	else:
		print(-1)


from collections import Counter
for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	dct = dict(Counter(A))
	B = list(dct.keys())
	B.sort(reverse=True)
	count = 0
	lengths = []
	for b in B:
		freq = dct[b]
		if freq >= 4 and count == 0:
			print(b * b)
			break
		if freq >= 2:
			count += 1
			lengths.append(b)
		if count >= 2:
			print(lengths[0] * lengths[1])
			break
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	count = []
	for i in b:
		if a.count(i) > 1:
			count.append(i)
	if len(count) == 0:
		print(-1)
	elif len(count) == 1 and a.count(count[0]) < 4:
		print(-1)
	else:
		c = max(count)
		count.remove(c)
		if a.count(c) >= 4:
			print(c * c)
		else:
			d = max(count)
			print(c * d)


t = int(input())
while t != 0:
	n = int(input())
	l = list(map(int, input().split()))
	ln = b = 0
	for k in range(0, n - 1):
		if l.index(l[k]) == k:
			if l.count(l[k]) >= 2:
				if l[k] > ln:
					b = ln
					ln = l[k]
				elif l[k] > b:
					b = l[k]
	if ln != 0 and l.count(ln) >= 4:
		print(ln * ln)
	elif ln != 0 and b != 0:
		print(ln * b)
	else:
		print('-1')
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	y = list(map(int, a.split()))
	l = []
	for i in y:
		if y.count(i) >= 2:
			if i not in l:
				l.append(i)
				if y.count(i) / 2 > 1:
					x = y.count(i) // 2
					for j in range(x - 1):
						l.append(i)
	if len(l) < 2:
		print(-1)
	else:
		x = 1
		for i in range(2):
			x *= max(l)
			l.remove(max(l))
		print(x)


for _ in range(int(input())):
	N = input()
	i = list(map(int, input().split()))
	o = []
	for n in set(i):
		c = i.count(n) // 2
		if c > 0:
			for __ in range(c):
				o.append(n)
	if len(o) >= 2:
		o.sort(reverse=True)
		print(o[0] * o[1])
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a_set = list(set(a))
	valid = []
	for v in a_set:
		if a.count(v) > 1:
			valid += [v]
	valid.sort()
	if len(valid) == 0:
		print(-1)
	else:
		largest = valid[-1]
		if a.count(largest) > 3:
			print(largest ** 2)
		elif len(valid) == 1:
			print(-1)
		else:
			print(valid[-1] * valid[-2])


T = int(input())
for _ in range(T):
	N = int(input())
	S = list(map(int, input().split()))
	C = []
	R = list(set(S))
	for num in R:
		if int(S.count(num)) >= 2:
			C.append(num)
		if int(S.count(num)) >= 4:
			C.append(num)
		else:
			continue
	C.sort()
	if len(C) <= 1:
		print('-1')
		continue
	else:
		print(C[-1] * C[-2])


def rect(stick, l):
	cnt = {}
	for s in stick:
		if s in cnt:
			cnt[s] += 1
		else:
			cnt[s] = 1
	possible = []
	for s in cnt:
		for _ in range(cnt[s] // 2):
			possible.append(s)
	if len(possible) <= 1:
		return -1
	possible = sorted(possible, reverse=True)
	return possible[0] * possible[1]
t = int(input())
for _ in range(t):
	l = int(input())
	stick = [int(x) for x in input().split()]
	print(rect(stick, l))


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(n) for n in input().split()]
	l = []
	d = []
	x = 0
	for j in range(n):
		if a.count(a[j]) >= 2:
			l.append(a[j])
			if a.count(a[j]) >= 4:
				d.append(a[j])
	if len(d) > 0:
		x = max(d)
	s = list(set(l))
	if max(s) == x:
		print(x * x)
	elif len(s) > 1:
		m = max(s)
		s.remove(m)
		n = max(s)
		print(m * n)
	else:
		print(-1)


import sys
from collections import Counter
TC = int(input())
for tc in range(TC):
	N = int(input())
	L = list(map(int, input().split()))
	freq = Counter(L)
	A = sorted([(l, cnt) for (l, cnt) in freq.items() if cnt >= 2])[::-1]
	res = -1
	if len(A) >= 1:
		(lmax, cnt) = A[0]
		if cnt >= 4:
			res = lmax * lmax
		elif len(A) >= 2:
			(lmax2, cnt) = A[1]
			res = lmax * lmax2
	print(res)


for _ in range(int(input())):
	n = int(input())
	sticks = list(map(int, input().split()))
	l = []
	for i in sticks:
		if sticks.count(i) >= 4 and i not in l:
			l.append(i)
			l.append(i)
		elif sticks.count(i) >= 2 and sticks.count(i) < 4 and (i not in l):
			l.append(i)
	lst = sorted(l)
	if len(l) >= 2:
		print(lst[-1] * lst[-2])
	else:
		print(-1)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = set(l)
	b = []
	for i in a:
		if l.count(i) >= 2:
			b.append(i)
		if l.count(i) >= 4:
			b.append(i)
	b = sorted(b)
	b = b[::-1]
	if len(b) >= 2:
		print(b[0] * b[1])
	else:
		print('-1')


test_cases = int(input())
for i in range(test_cases):
	count = 0
	arr2 = []
	N = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	arr.reverse()
	j = 0
	while count < 2 and j < N - 1:
		if arr[j] == arr[j + 1]:
			j = j + 1
			count += 1
			arr2.append(arr[j])
		j += 1
	if count == 2:
		print(arr2[0] * arr2[1])
	else:
		print(-1)