We need to parse the problem: Given an array of length 2n, we can perform n operations. Initially we choose a positive integer x. Then for each operation, we select two remaining elements whose sum equals the current x, remove them, and set x to the maximum of the two numbers. At the end, all elements should be removed. We need to decide if possible, and if yes output initial x and the pairs in order.
Constraints: total n across test cases <= 1000, each n <= 1000, so we can do O(n^2) or maybe O(n^2 log n) per test.
We need to find a valid sequence. The process is deterministic once we choose initial x and the pair selection order.
Observation: The operation: pick two numbers a, b with a+b = x, then set x' = max(a, b). This resembles a process of pairing numbers, where the next x is the larger of the pair. Since x becomes the max, subsequent sums must equal that max.
Idea: Consider the process backwards. Suppose we know the sequence of operations. At the final operation, we have two elements left, say u and v, with sum equal to the current x (which is the max from previous operation?). Actually, after the (n-1)-th operation, we have two elements left, and current x is some value. Then we pick those two with sum = current x, remove them, and set x to max(u, v). But after removal there are none, but the new x is irrelevant. However, we can think of the process as building from the last pair upward.
Let's denote the sequence: Starting x0 (initial chosen x). Operation 1: pick (a1, b1) with a1+b1 = x0, let x1 = max(a1, b1). Operation 2: pick (a2, b2) with a2+b2 = x1, x2 = max(a2, b2). ... Operation n: pick (an, bn) with an+bn = x_{n-1}, and after removal done.
If we reverse: At the last step, we had a pair (p, q) with sum = x_{n-1}, and after removal we set x_n = max(p, q). Since process ends, we don't need to use x_n. So we can think that in the final state, the x used for the last operation is some value, and that final pair's maximum is the "new x" but not used further.
We can attempt to reconstruct possible x0.
Key insight: Notice that the process ensures that each x_i (after operation i) is the maximum of the pair removed at step i. So x_i is one of the numbers originally in the array (since we remove elements from array). Also, x0 is chosen arbitrarily but must be such that we can pair something sum to it.
Let's think: After each removal, the set of remaining elements shrinks. The x values used are: initial x0 (chosen externally) and then x1, x2, ..., x_{n-1} (each equal to the maximum of the pair removed in previous step). x0 is not necessarily an element of the array, but it can be any positive integer; it doesn't need to be in the array. But after that, all subsequent x's are actually elements from the array (since they are the maximum of a pair that was in the array). So x0 is free.
If we can succeed, then among the pairs we choose, the maximum numbers will form a chain: x1 = max(pair1), x2 = max(pair2) where pair2 sum = x1, etc.
Thus we need to partition the array into n pairs, and order them, such that if we denote pairs (a_i, b_i) in order (i=1..n) where a_i <= b_i (so b_i is the max), then we have:
- b1 = x1, but not used as sum for first; actually first sum is x0 = a1 + b1.
- For i from 2 to n: a_i + b_i = b_{i-1} (since x_{i-1} = b_{i-1}).
- Also, note that the numbers used in pairs are exactly all array elements.
Additionally, we require that b_i >= a_i, obviously. And the sums must match.
So the problem reduces to: Given a multiset of 2n positive integers, can we arrange them into an ordered sequence of pairs (a_i, b_i) with a_i <= b_i, such that for i>=2, a_i + b_i = b_{i-1}, and also we need an initial x0 = a1 + b1 (which is arbitrary positive integer). There is no further constraint on x0. So essentially we need to find a permutation of the numbers into pairs with that recurrence.
Observe that if we define the pairs in order, then the b_i are decreasing? Let's see: For i>=2, we have b_i <=? Since a_i >=1, we have b_i = b_{i-1} - a_i < b_{i-1}. So b_i < b_{i-1} (strictly, because a_i positive). So the sequence of b_i is strictly decreasing positive integers. Also, a_i are positive, so b_i > 0. So we have a strictly decreasing chain of b's: b1 > b2 > b3 > ... > bn >= 1.
Now, all b_i are elements of the array, and all a_i are also elements. Moreover, each element appears exactly once either as an a or a b.
The recurrence can be expressed as: b_{i-1} = a_i + b_i. So b_{i-1} > b_i. This resembles a process where we combine the larger numbers as sums of two smaller numbers? Actually b_{i-1} is larger than b_i, and b_{i-1} is the sum of b_i and a_i, so b_{i-1} is greater than both.
If we go backwards: Starting from the smallest b (bn) and some a_n, we can compute b_{n-1} = a_n + b_n. Then b_{n-2} = a_{n-1} + b_{n-1}, etc. So the b's are built by adding a_i's to the next b.
Since the b's are strictly decreasing, the largest b is b1. And we also have the initial sum a1 + b1 = x0, but x0 is not further constrained.
Therefore, the core is to find a sequence of pairs that satisfies these equations.
Alternatively, think of constructing the pairs by repeatedly taking the largest remaining element as the current sum target (which is the previous b) and find a pair of remaining numbers that sum to it, with the larger of the two being the next b. This is similar to the forward process: we start with x0 = sum of some pair, but we don't know x0. Instead, we can start from the last step? Many solutions to similar problems (e.g., Codeforces problem "Multiset Removal Game" etc.) often involve sorting and simulating backward: consider that the final operation's x (the sum before last removal) is some value that must equal the sum of the last pair. The maximum of that last pair becomes the new x (unused). But if we work backwards, we can start by assuming the final pair consists of two numbers whose maximum is something, but we don't know.
Alternatively, note that the b_i sequence is strictly decreasing. So the largest element in the array must be b1, because all b_i are distinct and decreasing, and any a_i is less than its corresponding b_i (since a_i <= b_i). But a_i could be larger than some b_j? Possibly? Since b_i > b_{i+1}, and a_i < b_i, but a_i could be larger than some later b's? Example: b1=10, a1=1, b2=9? Wait, condition: b1 = a2 + b2. Since a2>=1, b2 <= b1-1. Also a1 <= b1, but a1 could be larger than b2? Yes, a1 could be 9, b1=10, then b2 maybe 5, a2=5? That's possible: a1=9 (<=10), b2=5, a2? b1=10 = a2+b2 => a2=5, so a2=5, b2=5, then a2 <= b2 equal. In that case a1=9 > b2=5. So b1 is the absolute maximum? Could there be an element larger than b1? b1 is the max of first pair. The array contains b1 and a1, and other numbers. But could there be a number bigger than b1? If there were, it would have to be either a b_i for some i or an a_i. But b_i are decreasing, so b1 is the largest b. a_i are <= b_i, so they are at most b_i, which is ≤ b1. So indeed, no element can exceed b1. Because any element is either a b_i (<= b1) or an a_i (<= its b_i ≤ b1). So b1 must be the maximum element of the whole array. Good.
Thus we know that the largest element of the array, call it M, must be b1, the maximum of the first pair. So we can set b1 = M.
Now, given b1 = M, we need to find a1 such that a1 + b1 = x0, but we can choose x0 freely, so any a1 (subject to a1 <= b1) will work, as long as we can find a1 among the remaining numbers. But wait, x0 must be chosen initially and not changed; we can choose any x0, so we can always set x0 = a1 + b1 after we decide a1. So the first step is essentially: we need to pair the maximum element M with some other element a1 (maybe less than or equal to M) to form the first pair. The sum x0 is then determined (a1+M). So the first step is essentially: pick a partner for M. After removal, the new x becomes M (since M is the max of the pair). So we then have to process the remaining 2n-2 elements, with initial x = M.
Now, the process continues: At each step, the current x is the maximum from previous pair, which is a number that we have removed (i.e., it's no longer in the array). But the algorithm says we have to select two elements from the remaining array whose sum equals x, and then set new x to the max of those two. So x is not necessarily an element of the remaining array; it's a value we carry. So after the first step, x becomes M, which is the element we just removed (the maximum). That element is no longer in the array. So we need to find two elements still present that sum to M.
Thus the condition for the second step: among the remaining elements (excluding the first pair's numbers), there must exist two numbers that sum to M. And after we remove them, the new x becomes the maximum of those two numbers, which will be some element from the remaining set (call it b2). Then we need two remaining numbers that sum to b2, etc.
So the forward process: We start with x0 (chosen). We must pick two numbers from the full array that sum to x0; after removal, x becomes max of those two. Then we repeatedly pick two numbers from the remaining set that sum to the current x, update x to max of those two.
Thus the problem reduces to: find an initial x0 and an ordering of pair selections that follows these rules.
From the earlier pair ordering perspective, we have b1 = M (largest element). Then after that, we need to find a pair (a2, b2) with a2 + b2 = M, and b2 = max(a2,b2). Then remove them, and set x = b2. Then need a pair (a3,b3) with a3+b3 = b2, and so on.
So the sequence of b's is decreasing: M = b1 > b2 > b3 > ... > bn.
And each b_i (for i≥2) is the larger element of the pair that sums to b_{i-1}. Also, the a_i are the smaller elements (or equal if equal) of those pairs.
Thus the problem becomes: given multiset S of 2n numbers, we need to extract a sequence: starting with the largest M, we need to find a partner a1 for it (any element <= M) to remove first? Wait, careful: In the forward process, the first pair we remove includes M and some other element a1. That pair's sum is x0. After removal, x becomes M. Then we need to remove a pair that sums to M. So the pair (a2, b2) that sums to M must be from the remaining elements (i.e., after removing M and a1). But note that a1 might be used later? No, a1 is removed in first step, so a1 is not available for later steps. So the pair that sums to M must be composed of two elements that are not M and a1.
Thus the set of numbers that will be paired in the subsequent steps (the pairs (a2,b2), (a3,b3), ..., (an,bn)) come from the original set excluding {M, a1}. And they must satisfy the chain: M = b1, then there is a pair (a2,b2) with a2+b2 = M, then a pair (a3,b3) with a3+b3 = b2, etc., until all numbers are used.
But also note that a1 is not part of this chain; it's only used in the first pair. So the numbers are partitioned into: first pair (a1, M) and then the chain of n-1 pairs (a2,b2), (a3,b3), ..., (an,bn) that satisfy the recurrence.
However, is it necessary that the first pair includes the maximum M? Let's double-check: Suppose there is a scenario where the maximum element is not used as b1 but as something else? Could it be used as a_i for some i? If it's used as an a_i, then a_i <= b_i, but b_i would be at least as large as a_i, so b_i >= M, implying b_i >= M. But since M is the maximum, b_i can't be > M, so b_i = M. So if the maximum element appears as an a_i, then the corresponding b_i must be at least M, but b_i is also an element, so b_i must be M as well. So the pair would be (M, M) with sum 2M. Could that happen? Possibly, if there are two copies of M? But then the maximum b in the chain is b1 (since b's are decreasing), so b1 would be at least M (since it's the largest b). If we had a pair (M, M) somewhere later, then b_i = M. But then b_{i-1} = a_i + b_i = M + M = 2M, so b_{i-1} = 2M must be an element in the array? But 2M might not be present. Actually b_{i-1} is the maximum of previous pair, and must be an element from the array as well (since it's the max of a pair that was removed earlier). So if we had a pair (M, M) later, we would need b_{i-1} = 2M, which must be an element in the array. But then 2M > M, contradicting that M is the maximum element (since 2M would be larger). So impossible unless there is element larger than M. Thus the maximum element cannot serve as the smaller element of a pair; it must be the larger of its pair. Moreover, it must appear as b1 (the first b) because the b sequence is strictly decreasing and the maximum b is the first one. Could the maximum element be b2? That would require b1 > b2 = M, so b1 > M. But then b1 would be larger than M, impossible. Therefore the maximum element must be b1. So indeed the first pair must contain the maximum element as the larger one.
Thus we have to pick some a1 (could be any element, possibly equal to M if duplicate? If there are two copies of M, then we could pair M (as larger) with another M as smaller; but then a1 = M, b1 = M, and they are equal. That's allowed because we take max = M (equal). Then x0 = 2M, and after removal, x = M. That's fine. However, then we have removed both copies of M? Actually if we have two copies of M, then one M is b1, the other M is a1. So both copies are removed in the first step. Then the remaining set doesn't contain M. That's okay. Then we need to find a pair that sums to M from the remaining set. But if M is the maximum, and we used both copies, then the remaining set has no M, but we need two numbers summing to M; that could be possible if there are numbers that sum to M. So it's possible. But note that after using M as a1, we must ensure that later we can form pairs. So a1 can be any element ≤ M.
Thus the problem reduces to: given multiset S of size 2n, let M = max(S). We need to choose an element a1 from S (different from the occurrence of M? but if there are multiple copies, we can use one as b1 and one as a1; if only one copy, then a1 must be a different element) to pair with M as the first pair. Then we set current x = M. Then we need to repeatedly, for i=2..n, from the remaining multiset, find two numbers that sum to the current x, and then set x to the maximum of those two. And we need to remove all elements exactly once.
But note: after the first step, we have removed M and a1, and we have x = M. The remaining multiset R has size 2n-2. The process from now on is deterministic given we can choose pairs each step. But we have freedom in which two numbers we pick each step as long as they sum to current x. However, the choice may affect future possibilities.
So we need to find a1 and a sequence of pairs for the remaining steps.
Alternatively, think of it as: we need to partition the multiset into n pairs, with a designated ordering: the first pair (p, M) where p ≤ M; the second pair sums to M, the third pair sums to the max of the second pair, etc. And the pairs after the first must follow the chain condition: if we denote the pairs after first as (c_i, d_i) with c_i ≤ d_i, and for i=1..(n-1) we have d_i = previous sum? Actually careful: Let's index pairs after first: pair2, pair3, ..., pair n. Let the current x before step i (i≥2) be some value. Initially after first step, x = M. So for step 2, we need pair2: (a2, b2) with a2+b2 = M, and set x = b2. For step 3: need pair3: (a3, b3) with a3+b3 = b2, set x = b3, etc. So it's exactly as earlier but shift indices.
Thus, we can think of constructing a chain: Starting from a target sum S1 = M. We need to choose a pair (a2, b2) from the remaining multiset such that a2 + b2 = S1. Then set S2 = b2 (the larger). Then need a pair (a3, b3) from remaining such that a3 + b3 = S2, set S3 = b3, etc. Until we use all numbers.
This resembles the concept of "pairing with decreasing sums". It is reminiscent of a known Codeforces problem: "1462D Add to Neighbour and Remove" maybe not. There is a classic problem: "Array Restoration" or "Multiset" something. Actually there is a known problem: "1472C" no. I recall a problem: "Remove the Subsequence" but not.
A known approach: For each possible choice of a1 (the partner of the max), we can simulate the process greedily: after removing M and a1, we have multiset R. Then we can try to simulate the chain. Since the chain's sums are determined by the previous b, and b must be the larger element of the chosen pair. The process can be simulated by always taking the current target sum T, and we need to find two numbers in the multiset that sum to T, with the larger being the next target. But note that the larger must be one of the numbers; and the smaller can be any. But to maximize chances, we might need to choose the pair wisely.
Observation: When we have a target sum T (which is the sum we need to achieve with a pair from remaining multiset), the larger element of the pair will become the next target. Since the chain is decreasing, the larger element must be strictly less than T (because the sum is T and both numbers positive, the larger is at most T-1 if the smaller is at least 1). Actually if the smaller >=1, then larger = T - smaller ≤ T-1. So the next target is less than current target. So the targets are strictly decreasing.
Also, the larger element of each pair must be the maximum among the remaining elements? Not necessarily, but since the sum is fixed, the larger element could be any number less than T. However, note that the larger element of a pair is the max of the two, so it's at least ceil(T/2). So b_i ∈ [ceil(T/2), T-1].
Now, given that we can choose any pair that sums to T, the decision of which pair to choose influences which numbers are left for later targets.
How can we find a valid sequence efficiently? Since total n across test cases is <=1000, we could for each test case try all possible choices of a1 (the partner of M). There are at most 2n-1 possibilities (since we exclude the max element? Actually we can choose any other element; if there are multiple copies of M, we could also choose another M as a1). So O(2n) possibilities. For each, we need to simulate the process to see if we can pair all remaining numbers according to the chain.
But we must also account for the fact that after we pick a1, we have to proceed with a deterministic algorithm to pair the rest. If we try all possible a1, and for each we attempt to build the chain greedily by always picking the largest remaining element as the larger? Let's think: For a given current target T, we need to find two numbers that sum to T. Usually, if we want to succeed, a natural greedy approach is to take the largest remaining element (call it y) less than T (since if y is too large, it might not have a complement). Then check if T-y is also present (distinct if y = T-y? careful with duplicates). If yes, we can pair them, set next target = y (since y is presumably the larger; if T-y > y? Actually we need the max of the two; we must ensure that we set the next target as the maximum. If we take y as the larger, we need y >= T-y. So we must choose y such that y >= T/2. So taking the largest remaining element that is at most T-1 might be natural, because then the complement is the smallest, leaving larger numbers for later? But later targets are smaller, so we might want to preserve larger numbers for larger targets? Actually targets are decreasing, so future targets are smaller. So larger numbers are only useful for larger targets; a large number can only be used as the larger element for a sum that is larger than it. So it's logical to use the largest remaining number as the larger element for the current target, because if we don't use it now, later targets will be smaller, so that large number might not have a complement that sums to a smaller target (since the complement would need to be even smaller, maybe possible). But sometimes using a slightly smaller large number might be necessary if the complement for the largest is missing.
We can attempt to simulate deterministically: For a given multiset, with current target T, we need to choose a pair (x, y) with x <= y, x+y=T, and y becomes next target. To maximize chances, we can consider that the next target will be y. Since y is larger than x, it will be used as the target for the next step, so we must be able to pair the rest with target y. The difficulty is that we need to find a sequence that works.
Given constraints (n up to 1000, total n 1000), we can afford for each candidate a1 to simulate the process by always choosing the largest remaining element as the larger of the pair, and checking if its complement exists. If that fails, maybe try another candidate or backtrack? However, we cannot simply backtrack for all possibilities because there could be many branching choices each step. But we can think of the process as constructing a binary tree? However, we can perhaps prove that if a solution exists, then the greedy strategy of always taking the largest remaining element as the larger works. Let's test with examples.
Consider the first example: a = [3,5,1,2], M=5. Possible a1 choices: 3, 1, 2. Try a1=3: first remove (3,5), x becomes 5. Remaining {1,2}. Target T=5. Need two numbers sum to 5 from {1,2}? 1+2=3 not 5. So fail. a1=1: remove (1,5), remaining {3,2}. Need pair sum to 5 from {3,2} -> 3+2=5 works, larger is 3. Then next target would be 3, but no remaining numbers, but we have used all? Actually after step2, we remove (2,3) sum=5, larger=3. Now no numbers left, but we have performed n=2 operations, so we are done. The process expects exactly n operations, which we did. So a1=1 works. a1=2: remove (2,5), remaining {3,1}, sum to 5? 3+1=4 not 5. So fails. So only a1=1 works. The greedy approach: after removing M and a1, we have remaining R. For target T=5, the largest remaining element is 3. Check if complement 2 exists? In R for a1=1, R={3,2}, largest 3, complement 2 exists, so pair (2,3) with larger 3. That works. For a1=3, R={1,2}, largest 2, complement 3? 5-2=3 not in set. So greedy would fail, but that candidate fails anyway. So greedy works in this case.
Consider a more complex example. Possibly there are cases where greedy fails but some other choice works. We need to analyze.
In many Codeforces problems of this type (e.g., "1462C" no, but I recall problem "1498C" maybe not). Actually there is a known problem: "1506D Epic Transformation"? no.
I recall a problem: "1399E2" not.
But let's reason more generally.
We have a chain: Starting from target T0 = M (after first step). We need to partition the remaining 2n-2 numbers into pairs (a_i, b_i) such that a_i + b_i = T_{i-1} and T_i = b_i, with T_i decreasing.
Thus, essentially we have a binary tree? It's like we start with a number M. We decompose it into two positive integers a2 and b2 (with a2 <= b2) that are present in the multiset, then we take b2 and decompose it into a3 and b3, etc., until we have used all numbers. This is similar to constructing a sequence where each step we split the current number into two numbers that sum to it, with the larger being the next number to split, and the smaller is "leaf" that is not further split.
This resembles a process of repeatedly subtracting a smaller part from a number. If we view it in reverse: starting from the final pair (a_n, b_n) we have b_n (some number) and a_n, and then b_{n-1} = a_n + b_n, and so on. So the numbers form a rooted tree? Actually it's a linear chain: each number (except the first M and the final b_n) appears as a sum of two numbers: as b_{i-1} = a_i + b_i. The a_i are never used as sums; they are only leaves. So all numbers are either "leaf" values a_i (including a1? Actually a1 is a leaf) or the chain b_i, with b_n also being a leaf (since it's not used as a sum except possibly? Actually b_n is the last b; it is not used as a sum because after the last operation we set x to b_n but no further operation. So b_n is also a leaf). So total leaves: a1, a2,..., a_n, and b_n. That's n+1 leaves? Wait, total numbers = 2n. We have b1=M, a1, and for i=2..n: a_i, b_i. That's 1 (b1) + 1 (a1) + (n-1)*2 = 2n. So b_n is included. So the leaves that are never used as sums are: a1, a2,..., a_n, and b_n? Check: b1 is used as sum (for pair2); b2 is used as sum for pair3; ...; b_{n-1} is used as sum for pair n; b_n is not used as sum because there is no pair after. So indeed b_n is not used as a sum. So the numbers that are not sums are the a_i's (including a1?) and b_n. That's n + 1 numbers? Actually there are n a_i: i=1..n gives n a's. Plus b_n gives n+1 numbers that are not sums. But total numbers are 2n, and numbers that are sums: b1, b2, ..., b_{n-1} are n-1 numbers. So total = (n) + (1) + (n-1) = 2n. Yes.
Thus the structure: we have a sequence of b's: b1 > b2 > ... > bn, where b1 = M. And for each i from 2 to n, we have a_i = b_{i-1} - b_i. And a1 is an arbitrary number <= b1 (but not part of the sums). Additionally, b_n is some number that is not further decomposed.
All numbers must be present in the multiset. So the multiset consists of: M (b1), a1, and for i=2..n: b_i and a_i = b_{i-1} - b_i. And b_n is also present.
Given the multiset, we need to find such a decreasing sequence b1 > b2 > ... > bn, all positive integers, and a1, such that the multiset matches exactly those numbers.
Thus, we can think of it as: Choose a1 and the sequence of b_i. But we can also approach by considering that the process is essentially determined by the choice of the pairs, and we can attempt to simulate it using a multiset and always picking the largest remaining element as the larger of the pair for the current target. Let's test if that greedy always works if a solution exists.
I recall a Codeforces problem: "1474C" maybe? Actually I recall problem "1472C" is different. But there is a known problem: "1462C" is about unique numbers. Hmm.
Let's search memory: There is a problem "Array Destruction" (CF Round #1462? Actually I think it's Codeforces problem 1472C? No, I remember a problem called "Array Destruction" from Codeforces Round #1455 or something. I think it's from Codeforces Round #1462 Div. 2 problem C? Let's think: I recall a problem where you choose an initial x and then repeatedly pick two numbers that sum to x, set x to max of them, etc. The problem statement is exactly this one! This is Codeforces problem 1472C? Wait, I recall a problem "Array Destruction" from Codeforces Round #1462 (Div. 2) problem C? Actually I think it's Codeforces Round #1485? Hmm. But I definitely recall this problem; the solution approach is: sort the array, then for each possible partner of the largest element (i.e., each other element), simulate the process using a multiset (like a frequency map) and always taking the largest remaining as the candidate for the larger element of the pair, because if a solution exists, then at each step the current target is the sum of the largest remaining element and some other element. Actually I recall an editorial: "We can show that the only way to proceed is to always take the largest remaining element as one of the two numbers for the current sum." Let me recall reasoning:
At each step, we have a current target X. The remaining numbers include some values. Since X is the sum we need to achieve, and we know that after we remove a pair (a, b) with a<=b and a+b=X, the next target will be b. Since b is the maximum of the two, and b < X (since a>0), we have b < X. Among the remaining numbers, the largest number, say L, must be used at some point. Could L be used as the smaller element of a pair? If L is used as smaller, then the larger element must be at least L, and sum would be at least 2L. But if L is the largest remaining, the only way it could be used as smaller is if there exists an element >= L to be the larger, but that's impossible because L is largest. Thus L must be used as the larger element of its pair. Therefore, for the step where L is removed, the current target must be equal to L + (some other number). Since L is largest, it's natural to try to remove it as soon as possible, because the current target is the sum of L and some other number. But the current target may not be large enough if we delay. Actually, consider the process: at each step, we have a current target T. The pair we remove will have sum T. Since the larger element b is at most T-1 (since a>=1), if we have the largest remaining element L, for it to be used as b, we need T = L + a for some a. So T must be at least L+1 (since a>=1). If we wait until later when the target T becomes smaller, we cannot use L as b because T would be less than L+? Actually if T is smaller than L, then L cannot be part of any pair because the sum of any two numbers is at least L + smallest other, but if T < L, then L alone exceeds T, so impossible. So L must be used when the current target T is at least L+1, and specifically T must equal L + a for some a present. Moreover, at the moment when we use L, the target T is determined by the previous step. Since after using L, the next target becomes L. So L will become the target for the next step. Therefore, if L is the largest remaining, it must be used in the current step, because if we don't use it now, the target might become smaller than L+1 and L becomes unusable. But is it forced that we must use the largest element in the very next step? Let's think: after first step, target is M (the global max). Now the remaining multiset R has maximum element, call it L1. Since target is M, which is >= L1+? Actually M is the global maximum, which is larger than any remaining element because M was the absolute max and we removed it. So the remaining max L1 < M. The current target is M. So we need to pick a pair summing to M. Could we pick a pair that does not involve L1? Possibly yes. But if we don't pick L1 now, then after this step, the next target will be the maximum of the chosen pair, which will be at most M-1 (since the pair sum is M, and the max is at most M-1). That next target will be less than M. Then L1 will still be present, and its value may be greater than the next target? Actually the next target will be some value b <= M-1. It could be less than L1? Since L1 is the largest remaining, it's possible that b < L1. For example, suppose remaining numbers are [10, 6, 4] and target is 14? Not exactly. Let's consider: after first step, M=20, a1=5, so remaining R = {15, 3, 2}? Actually we need numbers that sum to M=20. R could be {15, 4, 1}. Largest L1=15. If we pick pair (4,16) but 16 not present, so we need (15,5) but 5 removed; so maybe we need (15,5) but 5 not there. So we might need to pick (15,5) but 5 is used. So we might be forced to use L1? Not necessarily; could pick (10,10) if two 10s exist. But L1=15, maybe we have 5 and 15? But if 5 is not present, we can't use 15. So we might need to use a different pair, but then L1 remains. After we pick a pair (a,b) with b < L1, next target = b, which is less than L1. Then L1 is larger than the new target, so it's impossible to ever use L1 because any future target will be <= b (since targets strictly decrease), and L1 > b, so it cannot be part of a pair summing to a target less than L1 (since sum would be at least L1+min, which > b). Thus L1 would never be removed, making success impossible. Therefore, to clear all numbers, at each step, the current target T must be at least as large as the largest remaining element, and indeed the largest remaining element must be used in the current pair, because otherwise it will become stranded.
More formally: At any step, let T be the current target (sum needed). Let L be the maximum element among the remaining numbers. Since we need to eventually remove L, there must be some step where we remove it. Suppose we don't remove L in the current step. Then after this step, we remove two numbers whose sum is T, and the new target becomes the larger of those two, call it b. Since b < T (strictly), we have b < T. Also, b is at most the maximum of the two removed numbers, which is less than or equal to T-1. Meanwhile, L remains. Since b < T, and T is the sum of two numbers, one of which could be L? Actually if L is not used, then the maximum among remaining after removal is still at least L (since L not removed). The next target is b, which is < T. Could we ever remove L later? To remove L, we need a step where the current target S is equal to L + some other number c (with c ≤ L). So S ≥ L + 1. Since the targets are strictly decreasing, the only chance to have S ≥ L + 1 is at the current step or earlier, because after this step the target becomes b < T. If T is already less than L+1, then L cannot be used now either. But initially after first step, T = M, and M is the global maximum, so T > L because L < M, so T >= L+? Actually L < M, so T = M > L. Could T be exactly L + something? Yes. So at the first step after removal (i.e., step 2), we have T = M, which is greater than L. So it's possible to use L now. If we don't use L now, the next target will be b < M. Since L is less than M, it's possible that b could still be >= L+? Not guaranteed. But if b is less than L, then L > b, then in the next step target is b, and to use L we would need L + something = b, impossible because L > b. So if we skip using L now, we must ensure that b >= L+1, i.e., b > L. But b is the larger of the pair we remove, so b ≤ T-1, but could b still be > L? Possibly if the pair we remove includes a number larger than L? That's impossible because L is the maximum remaining; any number we remove is ≤ L (since L is max). Actually if we don't remove L, then the numbers we remove are both ≤ L (since L is max). The larger of them, b, is ≤ L. So b ≤ L. If b = L, that would mean we removed L (contradiction because we assumed we don't remove L). So b < L. Therefore b < L. Hence after the step, the new target b is strictly less than L. Then L is now larger than the current target, making it impossible to ever remove L. Therefore, at each step, we must remove the current maximum element L. More precisely, for the process to be possible, at each step we must pair the current target T with the largest remaining element L as the larger element of the pair. Because if we don't, L will remain and become larger than all future targets, making removal impossible.
This is a crucial observation. Let's verify with example: first test case: after first step, remaining {3,2}, largest L=3, target T=5. We used L=3 as larger (with 2). That works. If we had instead used (1,4) but 4 not present; can't. So indeed we used L.
Second test: a = [1,1,8,8,64,64]? Actually second test case: n=3, array: 1 1 8 8 64 64. M=64. Possible a1 choices: try a1=64? But there are two 64s. If a1=64, first pair (64,64), x0=128, x becomes 64. Remaining {1,1,8,8}. Largest L=8, target=64. Must use L=8 as larger? But 8+? =64 requires 56, not present. So fails. a1=8: first pair (8,64) sum=72, x becomes 64, remaining {1,1,8,64? Actually we have two 8s, two 64s, two 1s. If we remove one 8 and the 64 (M), remaining: one 8, two 1s, and one 64? Wait, the array: 1,1,8,8,64,64. M=64. If we choose a1=8, we remove one 8 and one 64. Remaining: 1,1,8,64. Largest L=64? Actually remaining contains another 64, so L=64. But target T=64 after step1. So we need a pair sum to 64. The largest element is 64. To remove it, we need pair (0,64)? But we need two numbers sum to 64. If we use 64 as larger, we need a 0, impossible. So we cannot use 64 because sum would be 64+? >64? Actually if we take 64 as larger, we need a smaller number a such that a+64=64 => a=0 not present. So 64 cannot be used as larger because that would require a=0. Could 64 be used as smaller? That would require a larger number >64, but none exists. So the remaining 64 cannot be used at all when target=64. So impossible. Indeed the answer is NO.
Thus the greedy approach of always taking the largest remaining as the larger element of the pair for the current target seems necessary for any feasible solution.
Now, is it also sufficient? If we always pair the largest remaining element L with T-L (assuming T-L is present), and then set next target = L, does this guarantee that if a solution exists, this greedy will find it? Possibly yes, because any solution must use L in the current step as the larger element. But there might be a nuance: what if there are multiple copies of L? Then we could choose any one; but still we must use one of them as larger. However, if there is also a scenario where L appears multiple times, but we might need to pair L with a specific complement to make the rest work. But since L is the maximum, any valid solution must pair L with some element that together sum to T, and that complement is T-L. So T-L must be present. Moreover, after that, the next target is L. The rest of the process must handle the remaining multiset (which now excludes that L and T-L). So if a solution exists, then there is some L (the maximum) and some complement c = T-L present, and after removing them, the rest must be solvable with target L. Since we are forced to use L as larger, we can simply check: if T-L exists (with multiplicity), then we can try that pair and recursively solve with new target = L and the remaining multiset. If this recursive greedy fails, then no solution exists (because any solution would have to use L as larger, and would have to pair it with exactly T-L, so if that branch fails, there is no alternative). However, is it possible that L could be paired as the smaller element in a valid solution? Earlier we argued that L cannot be the smaller because that would require a larger element than L to be the larger, which doesn't exist. So L must be the larger. Thus the only possible pair involving L is (T-L, L) (with L as larger). So the decision is forced: we must pair L with T-L. So the process is deterministic if we know T. Therefore, we can simulate deterministically: at each step, given current target T and multiset of remaining numbers, let L = max(remaining). If T-L is present (and distinct from L unless there are at least two copies), then we remove L and T-L, and set T = L for next step. If at any step the required complement is not present, then it's impossible.
Thus the whole problem reduces to: choose initial a1 (the partner of the global max M) and then simulate this deterministic process. Because after the first step, the target is M, and we proceed greedily by always pairing the current largest with target minus it.
So algorithm: For each test case:
- Sort the array, get the maximum value M = a[2n-1] (0-index after sort).
- For each possible candidate x for the other element in the first pair (i.e., each element other than the occurrence of M? Actually we need to choose a1 from the array (any element except maybe one copy of M if only one copy). We'll iterate over distinct values? But we must consider each occurrence because multiset matters. However, since we only need one valid solution, we can try each distinct element (or each index) as the partner. But there can be up to 2n candidates, n up to 1000, so total candidates across test cases up to 2000? Actually total n across test cases is <=1000, so total number of elements across all test cases is <=2000? Wait, total n sum <=1000, meaning sum of n (the half count) across test cases <=1000. So total array lengths sum <=2000. So iterating over each possible partner (which is at most 2n-1 per test case) is acceptable: total candidate checks across all test cases <= sum(2n) <= 2000. For each candidate, we need to simulate up to n-1 steps, each step requiring O(log n) operations to maintain multiset (or using frequency map). Since total steps across all simulations is at most O(n^2) but with total n small, it's fine.
But we must be careful: The array values can be up to 1e6, but we can use a frequency map (like dictionary or array of size maybe not) but given total elements small, we can use a multiset implemented via Counter from collections.
So for each candidate a1, we do:
- Create a frequency map (Counter) of all elements.
- Remove one occurrence of M and one occurrence of a1. If after removal frequency negative (i.e., a1 == M but count of M is 1), then invalid. Actually if a1 == M, we need two copies of M; so check count[M] >= 2.
- Set current_target = M.
- While there are elements left:
- Let L = maximum element in the multiset (we can get it by keeping sorted list or using a sorted container; since total size small, we can just sort the remaining list each time? That would be O(n^2) per candidate, but n <= 1000 and total candidates up to maybe 2000, could be up to 2e6 operations, still okay. But we can optimize by using a data structure, but given constraints small, we can just use a list and sort once per candidate? Actually after each removal we need to get the max quickly. We can maintain a multiset using heapq for max? Since Python's heapq is min-heap, we can use negative values. Or we can use collections.Counter and keep track of the current max by updating it since values only decrease? We'll do a simple approach: after each step, the multiset size reduces by 2; we can store the numbers in a list and sort it once, then simulate by removing elements and maintaining sorted order? That might be messy. Better: use a Counter and also maintain a sorted list of keys or use a Tree but Python doesn't have built-in. Since total n is small, we can just use list and call max() each time, which is O(k) where k is remaining size. In worst case, for each candidate we do up to n steps, each step O(n) for max and O(n) for removal? That's O(n^2) per candidate. With n up to 1000 and candidates up to 1000 (worst-case each test case n=1000, candidates 1999), O(1000^3) would be too large (1e9). But total n sum is 1000, so the largest n per test case could be 1000, but sum of n across tests is 1000, meaning there is at most one large test case or a few. For a single test with n=1000, candidates could be up to 1999. O(n^2) per candidate would be 1000^2 * 2000 = 2e9, too much.
We need a more efficient simulation per candidate. However, note that total number of elements across all tests is <=2000, so the sum of lengths is <=2000. So even if we do O(L^2) per candidate, where L=2n, and number of candidates per test case is up to L-1, then total operations could be O(L^3) worst-case? But since total L across all tests is <=2000, L^3 is 8e9, still too high. But we can optimize: we can simulate each candidate in O(L log L) or O(L) by using a proper multiset.
We can pre-sort the array. For each candidate, we need to maintain a frequency map and a way to get the current max quickly. Since the values are up to 1e6, we could use a frequency array of size max_val+1, but max_val can be 1e6, not huge for a few tests, but total elements small so we can use dictionary.
We can also use a multiset implemented via a balanced BST using sortedcontainers, but not allowed. Simpler: Since total elements per test case is at most 2000, and number of candidates is at most 2000, the total number of simulation steps across all candidates is at most sum over candidates of (n-1). Since sum of n over tests is 1000, the maximum number of candidates is roughly 2n per test, so total candidates across tests <= 2 * total n? Actually total n sum is 1000, so the sum of 2n over tests is 2000. So total number of candidates across all test cases is at most 2000 (since each test case has at most 2n candidates, and sum of 2n is 2000). Indeed, sum of lengths of arrays across tests = 2 * sum n <= 2000. So total number of candidate simulations across all test cases is at most 2000. Each simulation processes at most n-1 steps, which sum over all simulations of steps is at most total candidates * (n per test) but careful: For a test case with array length L=2n, number of candidates is L-1 (up to 2n-1). If we simulate each candidate separately, we would do (2n-1)(n-1) steps, which is about 2n^2. Since n could be up to 1000, that's about 2e6 steps per test, which is fine. And each step we need to get max and check complement, and update frequencies. If we implement with a Counter and a sorted list of values (or just a list we keep sorted after each removal, but that would be O(n) per removal). But 2n^2 with n=1000 is 2e6, times maybe constant factor, that's okay. Actually 21000^2 = 2,000,000 operations, which is