import sys
from collections import Counter
def solve():
try:
n = int(sys.stdin.readline())
sticks = list(map(int, sys.stdin.readline().split()))
except (IOError, ValueError):
return
counts = Counter(sticks)
all_pairs = []
singles = []
for length in sorted(counts.keys()):
all_pairs.extend([length] * (counts[length] // 2))
if counts[length] % 2 == 1:
singles.append(length)
singles.sort(reverse=True)
max_perimeter = 0
# Case 0: 0 singles
if len(all_pairs) >= 2:
current_pairs = list(all_pairs)
p_even = sum(current_pairs) * 2
while len(current_pairs) >= 2:
s_max = current_pairs[-1]
if 2 * s_max < p_even:
max_perimeter = max(max_perimeter, p_even)
break
else:
p_even -= 2 * s_max
current_pairs.pop()
# Case 1: 1 single
if len(all_pairs) >= 1 and len(singles) >= 1:
current_pairs = list(all_pairs)
p_pairs = sum(current_pairs) * 2
while len(current_pairs) >= 1:
s_pair_max = current_pairs[-1]
found_valid = False
for s in singles:
p_total = p_pairs + s
s_max = max(s_pair_max, s)
if 2 * s_max < p_total:
max_perimeter = max(max_perimeter, p_total)
found_valid = True
break
if found_valid:
break
p_pairs -= 2 * s_pair_max
current_pairs.pop()
# Case 2: 2 singles
if len(all_pairs) >= 1 and len(singles) >= 2:
current_pairs = list(all_pairs)
p_pairs = sum(current_pairs) * 2
s1, s2 = singles[0], singles[1]
while len(current_pairs) >= 1:
s_pair_max = current_pairs[-1]
p_total = p_pairs + s1 + s2
s_max = max(s_pair_max, s1)
if 2 * s_max < p_total:
max_perimeter = max(max_perimeter, p_total)
break
else:
p_pairs -= 2 * s_pair_max
current_pairs.pop()
print(max_perimeter)
def main():
try:
num_tests_str = sys.stdin.readline()
if not num_tests_str: return
num_tests = int(num_tests_str)
for _ in range(num_tests):
solve()
except (IOError, ValueError):
return
if __name__ == "__main__":
main()