We need to reverse the process. We have final array b of length n. It was constructed from original a using the described process: start with empty? Actually process: left side, right side, then leftmost possible (right of a1?), etc.
Let's define the process precisely.
We have original sequence a[1..n]. We simulate constructing an output list (on whiteboard) step by step.
Step 1: write a1 to left side → list = [a1].
Step 2: write a2 to right side → list = [a1, a2].
Step 3: write a3 "as far to the left as possible (but to the right from a1)". That means we want to insert a3 at the leftmost possible position, but it must be after a1? Actually phrase: "then as far to the left as possible (but to the right from a_1), he wrote the number a_3;" At this point list is [a1, a2]. So there are positions: at index 1 (leftmost), index 2 (between a1 and a2), index 3 (rightmost). "as far to the left as possible" means we want to place it at the smallest index possible, but the condition says "but to the right from a1". So it must be placed somewhere to the right of a1. The leftmost position that is to the right of a1 is position 2 (between a1 and a2). So we insert a3 at index 2? Actually insertion shifts? Usually, when we say "write number at a position", we insert it and the existing numbers shift to make room? The description: "he wrote the number a_3; then as far to the left as possible (but to the right from a_1)". This suggests we are writing on a whiteboard where we have slots. Initially a1 at leftmost, a2 at rightmost. Then for a3, we want to place it somewhere between a1 and a2, as left as possible. That means we effectively insert it immediately after a1. So new order becomes [a1, a3, a2].
Step 4: write a4 "as far to the right as possible (but to the left from a_2)". After step3, list = [a1, a3, a2]. "As far to the right as possible" but must be left of a2. So the rightmost position left of a2 is between a3 and a2. Insert a4 at index 3 (between a3 and a2) → [a1, a3, a4, a2].
Step 5: write a5 "as far to the left as possible (but to the right from a_1)"? Actually the pattern alternates: odd indices (starting from 1) go to left side? Let's check: a1 leftmost, a2 rightmost, a3 leftmost possible but right of a1, a4 rightmost possible but left of a2, a5 leftmost possible but right of a1? Actually after a4, list is [a1, a3, a4, a2]. Next a5: as far to the left as possible, but to the right from a1. Now the leftmost possible position that is to the right of a1 is position 2 (between a1 and a3). However, is there also a position between a1 and a3? Yes. Insert a5 at index 2 → [a1, a5, a3, a4, a2].
But wait, maybe there is nuance: "as far to the left as possible (but to the right from a_1)" for a3. After that, for a5, similar phrase? The description says "Polycarp continued to act as well, until he wrote out the entire sequence on the whiteboard." So the algorithm: For i from 1 to n:
If i is odd? Actually step1: a1 leftmost.
step2: a2 rightmost.
step3: a3: as left as possible but right of a1.
step4: a4: as right as possible but left of a2.
step5: a5: as left as possible but right of a1? Actually after step4, we have a1 at left, a2 at right, and others inside. The phrase "as left as possible but to the right from a1" for odd indices >=3? But need to consider that as we insert more numbers, the constraints might change? Possibly the process is: always alternate between inserting at the leftmost available empty slot and the rightmost available empty slot, starting with leftmost (a1) and then rightmost (a2). But the "but to the right from a1" and "but to the left from a2" conditions ensure that we never insert before a1 or after a2? That is, a1 remains the leftmost element, a2 remains the rightmost element throughout. Yes, from the process description, a1 is written left side and then never moved; a2 written right side and never moved. Then for a3, it must be placed to the right of a1; for a4, it must be placed to the left of a2; for a5, again to the right of a1, but maybe now there are multiple slots to the right of a1; we pick the leftmost possible among them. For a6, again rightmost possible but left of a2. So indeed, the process is:
Initialize deque? Actually we start with list = [a1, a2]? But step1: only a1. Then step2: a2 at rightmost gives [a1, a2]. But after that, the list always has a1 as first element, a2 as last element, and inner elements will be inserted alternately from left and right sides. So the process can be modeled as: we have two pointers: left_insert_pos and right_insert_pos? But insertion positions shift as we add.
Alternative view: The process constructs the output list by the following algorithm:
Let result be an empty list.
For i = 1 to n:
if i == 1:
result.append_left(a1)
elif i == 2:
result.append_right(a2)
else:
if i is odd: // i=3,5,7,...
// Insert a_i just after the current leftmost element? Actually "as far to the left as possible (but to the right from a_1)"
// That means we need to insert a_i at position 2 (index 1 if 0-indexed after a1) always? But after previous insertions, the leftmost possible position that is to the right of a1 might not be index 1 if we inserted something left of previous ones? Wait "as far to the left as possible" means we want the smallest index that is > index of a1. Since a1 is always at index 0, the leftmost possible position is index 1. However, after we insert a3 at index 1, the element at index 1 becomes a3. Then for a5, the leftmost possible position to the right of a1 is still index 1? But if we insert at index 1, we would shift a3 to the right, so the order becomes [a1, a5, a3, ...]. That matches the example? In the example: a=[3,1,4,1,5,9,2] → output b = [3,4,5,2,9,1,1]. Let's simulate:
Step1: [3]
Step2: [3,1]
Step3: a3=4, leftmost possible right of 3 → insert at index1 => [3,4,1]
Step4: a4=1, rightmost possible left of 1? Actually a2 is 1 at the rightmost? Wait a2=1, so rightmost element is 1. a4 should be placed as far right as possible but left of a2. In list [3,4,1], left of a2 means before the last element (index 2? Actually a2=1 is at index2). Rightmost possible position left of a2 is index 2? That would be after the current last? Wait insertion: we can insert at index 2 (between index1 and index2) or index1? The rightmost possible that is left of a2 is the position immediately before a2 (i.e., between 4 and 1). So we insert a4=1 at index2 => [3,4,1,1]? Actually if we insert at index2, list becomes [3,4,1,1]? Let's do: original [3,4,1]; insert new element before index2? Let's define positions: before a2 is the slot between 4 and 1. Insert there: resulting [3,4,1,1]? No, if you insert at index2 (0-indexed: indices: 0:3,1:4,2:1). Inserting at index2 would shift the element at index2 to index3: new: index0:3, index1:4, index2: new (1), index3: old 1. So result [3,4,1,1] which matches the example intermediate? The note says after step4: [3,4,1,1] (from note: after step4? Actually note: [3] ⇒ [3,1] ⇒ [3,4,1] ⇒ [3,4,1,1] ⇒ [3,4,5,1,1] ⇒ [3,4,5,9,1,1] ⇒ [3,4,5,2,9,1,1]. Yes step4 yields [3,4,1,1]. But careful: In step4, they wrote a4=1, so after step4 list is [3,4,1,1]. That matches.
Step5: a5=5, odd, insert as left as possible but right of a1. Current list [3,4,1,1]; a1=3 at index0. Leftmost possible position right of a1 is index1. Currently index1 is 4. Inserting at index1 yields [3,5,4,1,1]? But note's next step is [3,4,5,1,1]? Actually note says after step5: [3,4,5,1,1]. Wait they have [3,4,5,1,1] not [3,5,4,1,1]. So maybe my interpretation is wrong. Let's re-evaluate.
The note says after step5: [3,4,5,1,1]. So they inserted 5 not at index1 but somewhere else. Let's track step-by-step from note:
So step5 inserted 5. Where did 5 go? After step4 we have [3,4,1,1] (positions: 0:3,1:4,2:1,3:1). Step5: we need to insert 5. According to note, result is [3,4,5,1,1]. That means the order is 3, then 4, then 5, then 1, then 1. So 5 is placed after 4 and before the first 1 (which was at index2). So insertion index2? That is between index1 and index2. But is that "as far to the left as possible (but to the right from a_1)"? The leftmost possible position right of a1 is index1 (between 3 and 4). But they placed at index2. So maybe the condition for odd steps after step3 is not simply "right of a1" but "right of the previously placed leftmost element"? Or maybe the process: At each step, we consider the current whiteboard as having empty slots between the numbers. The phrase "as far to the left as possible (but to the right from a_1)" for a3 is clear: there is only one empty slot between a1 and a2. For a5, there are multiple empty slots: between a1 and a3? Actually after step4, we have sequence: a1, a3, a4, a2. But in the example, a3=4, a4=1. So sequence: [3,4,1,1]. The empty slots: before 3? no; between 3 and 4? yes; between 4 and 1? yes; between 1 and 1? yes; after last? no because a2 at end? Actually a2 is the last element? In note a2=1 is the last element? But after step4, the sequence ends with a2? Actually a2=1 is the last element? Check: after step4: [3,4,1,1] has two 1s. Which one is a2? a2 was written to the right side at step2 and never moved. So it should remain at the rightmost position. So in [3,4,1,1], the rightmost element is the second 1, which is a2? Possibly the first 1 is a4? Actually a4=1. So after step4, the list is: a1=3, then a3=4, then a4=1, then a2=1. So a2 is at index3. So the slots: between a1 and a3 (index1), between a3 and a4 (index2), between a4 and a2 (index3). The leftmost possible position to the right of a1 is the slot between a1 and a3. That would insert at index1, resulting [3, new, 4,1,1] which would be [3,5,4,1,1]. But note gives [3,4,5,1,1]. So they inserted at index2 (between a3 and a4). So why is that considered "as far to the left as possible"? Because maybe there is a constraint: when inserting left, you must insert it to the right of the leftmost element that is currently at the leftmost side? Actually a1 is at leftmost. But the slot between a1 and a3 is to the right of a1. So that seems leftmost. So why not choose that? Perhaps because the process says "as far to the left as possible (but to the right from a_1)" and also must maintain that after insertion, the sequence of operations is consistent? Or maybe the interpretation is: we have a sequence of empty positions that are available, and we fill them from the ends inward. That is, we consider the whiteboard as a line of n positions. Initially, we place a1 at the leftmost position (position 1). Then we place a2 at the rightmost position (position n). Then we consider the leftmost available empty position (i.e., the smallest index without a number) for a3? But wait: the positions are fixed slots? The description: "he wrote the number a_1 to the left side (at the beginning of the whiteboard); he wrote the number a_2 to the right side (at the end of the whiteboard); then as far to the left as possible (but to the right from a_1), he wrote the number a_3; then as far to the right as possible (but to the left from a_2), he wrote the number a_4;" That suggests there is a physical whiteboard with positions. When we write a number, we occupy a spot. The "as far to the left as possible" means we find the leftmost empty spot that is to the right of a1. Since a1 occupies the leftmost spot, the next empty spot to its right is the leftmost possible spot for a3. Similarly, after writing a3, the next step for a4: find the rightmost empty spot that is to the left of a2. Since a2 occupies the rightmost spot, the spot immediately to its left is the rightmost possible. So essentially, we are filling the array from both ends inward, alternating between left and right, but with a twist: first leftmost (a1), then rightmost (a2), then the next leftmost empty (a3), then the next rightmost empty (a4), and so on. However, careful: After writing a1 and a2, the whiteboard has positions 1..n. a1 occupies position 1, a2 occupies position n. The remaining empty positions are 2,3,...,n-1. For a3, we want the leftmost empty position: that is position 2. So a3 goes to position 2. Then for a4, we want the rightmost empty position (still among positions 2..n-1, but position 2 is now taken). So the rightmost empty is position n-1. So a4 goes to position n-1. Then for a5, the leftmost empty among remaining positions: positions 3..n-2? Actually after placing a3 at pos2 and a4 at pos n-1, the empty positions are 3,4,...,n-2. The leftmost empty is position 3, so a5 goes to position 3. Then for a6, rightmost empty among remaining (pos 4..n-2) is position n-2, so a6 goes to position n-2, and so on. That is exactly the process of filling from both ends inward, alternating left and right after the first two steps? Actually first step places a1 at pos1; second step places a2 at pos n; then for i=3, we place a_i at the next leftmost available (pos2); i=4 at next rightmost available (pos n-1); i=5 at next leftmost (pos3); i=6 at next rightmost (pos n-2); etc. This is a classic "deque" construction: if we simulate by maintaining left and right pointers, and place elements in order: first a1 to left, then a2 to right, then a3 to left+1, a4 to right-1, a5 to left+2, a6 to right-2,... So essentially, we are constructing the output array b as: b[1] = a1, b[n] = a2, b[2] = a3? Actually pos1 = a1, pos2 = a3, pos3 = a5, pos4 = a7? Wait pattern: positions: leftmost: index1 = a1. Then next left insertion: index2 = a3, index3 = a5, index4 = a7, ... as long as odd indices (starting from 3) go to increasing left positions. And right side: index n = a2, index n-1 = a4, index n-2 = a6, index n-3 = a8,... So the output b (the sequence on whiteboard) is a permutation of a. Specifically, b[1] = a1, b[n] = a2; for i from 2 to ... we have: b[i] = a_{2i-1}? Not exactly because the number of elements placed from left and right depends on n.
Let's formalize:
Let positions be 1-indexed. We'll place elements in order of indices i from 1 to n. We'll maintain L = 1, R = n initially (pointers to next left empty and next right empty). However the order of placement matters: we place a1 at pos L, then increment L? Actually after placing a1 at leftmost, the next left empty is position 2. But the process: a2 goes to rightmost, so we place a2 at pos R, then decrement R to n-1. Then a3 goes to the leftmost empty, i.e., current L (which is 2), then increment L to 3. Then a4 goes to the rightmost empty, i.e., current R (which is n-1), then decrement R to n-2. And so on. This matches the "filling from ends" approach.
Now let's test this with the example: n=7. Process:
L=1,R=7.
Place a1 at pos1 => b[1]=a1=3. L=2.
Place a2 at pos7 => b[7]=a2=1. R=6.
Place a3 at pos2 => b[2]=a3=4. L=3.
Place a4 at pos6 => b[6]=a4=1. R=5.
Place a5 at pos3 => b[3]=a5=5. L=4.
Place a6 at pos5 => b[5]=a6=9. R=4.
Place a7 at pos4 => b[4]=a7=2. L=5, R=3 stop.
Resulting b = [3,4,5,2,9,1,1] which matches the example! Good.
Thus the process is exactly: start with empty array of length n. For i from 1 to n:
if i == 1: place at leftmost (position 1).
elif i == 2: place at rightmost (position n).
else:
if i is odd: place at the current leftmost available position (i.e., next from left)
else (i even): place at the current rightmost available position (i.e., next from right)
But careful: For i=1 we placed at leftmost, i=2 at rightmost. For i=3 (odd) we placed at leftmost (now next leftmost after the first leftmost is taken). For i=4 (even) we placed at rightmost. So pattern: i odd -> left side placement? Actually i=1 is odd, placed left; i=3 odd, left; i=5 odd, left; so all odds placed on left side? i=2 even placed right; i=4 even placed right; i=6 even placed right. So the pattern is: odd indices of a go to the left side in increasing order of placement (i.e., a1, a3, a5, a7, ... placed from left to right in the output b), and even indices of a go to the right side in decreasing order (i.e., a2, a4, a6, ... placed from right to left in b). This is consistent.
Thus, given the output b (list b1..bn), we need to recover a. We know:
b1 = a1
bn = a2
The rest of b: positions 2,3,...,n-1 are filled with the remaining a_i in the order: a3, a4, a5, a6,... but placed according to the pattern: a3 goes to position 2, a4 to position n-1, a5 to position 3, a6 to position n-2, a7 to position 4, etc. So essentially, we can reconstruct a by reading b in a specific order: first a1 = b1; then a2 = bn; then we read from b from left to right the positions 2,3,...,some? But careful: The order of a is: a1, a2, a3, a4, a5, a6, ... But we placed a3 at b2, a4 at b[n-1], a5 at b3, a6 at b[n-2], a7 at b4, a8 at b[n-3], ... So to get a sequentially, we need to read: a1 = b1; a2 = b[n]; then for k from 2 to ??? We need to interlace: next a3 from b2, a4 from b[n-1], a5 from b3, a6 from b[n-2], etc.
Thus the reverse: Given b, we can reconstruct a by:
a[1] = b[1]
a[2] = b[n]
Then set two pointers: left = 2, right = n-1.
For i from 3 to n:
if i is odd (i=3,5,7,...) then take from left pointer and then increment left.
else (i even, i>=4) take from right pointer and then decrement right.
But careful: i=3 is odd, so we take from left (which initially is 2). That gives a3 = b[2]. Then left becomes 3.
i=4 is even -> take from right (initially n-1) -> a4 = b[n-1], then right becomes n-2.
i=5 odd -> take from left (now 3) -> a5 = b[3], left=4.
i=6 even -> take from right (now n-2) -> a6 = b[n-2], right=n-3.
... Continue until we have all n a's.
Thus we can recover a.
But wait: In the note, they gave b = [3,4,5,2,9,1,1]. Applying this reverse:
a1 = 3, a2 = 1 (b7). left=2, right=6.
i=3 odd: a3 = b2 = 4; left=3.
i=4 even: a4 = b6 = 1; right=5.
i=5 odd: a5 = b3 = 5; left=4.
i=6 even: a6 = b5 = 9; right=4.
i=7 odd: a7 = b4 = 2; left=5, right=3 stop.
So a = [3,1,4,1,5,9,2]? That's not matching the example. Wait the example original a = [3,1,4,1,5,9,2]. We got [3,1,4,1,5,9,2]? Actually that matches exactly: 3,1,4,1,5,9,2. So yes it matches. I earlier miswrote ordering? I said a2 = b[n] gave 1, which is correct. So the reversed method works.
Let's test with second example from input: n=4, b=[9,2,7,1]. Output a expected: 9 1 2 7? Actually provided output: "9 1 2 7". So a = [9,1,2,7]. Check: Using reverse: a1 = b1=9; a2 = b4=1; left=2, right=3.
i=3 odd: a3 = b2=2; left=3.
i=4 even: a4 = b3=7; right=2.
Thus a = [9,1,2,7] matches.
Now let's examine constraints: t up to 300, n up to 300, easy.
Thus for each test case, we read n, array b. Then produce a as described.
Implementation:
Input t.
Loop for each test case:
read n, read list b (size n).
create list a of size n.
a[0] = b[0]
if n > 1: a[1] = b[n-1] (since a2 is b[n-1] in 0-index).
Then set left = 1 (0-index for b, i.e., index1?), right = n-2 (index of second last). Actually in 0-index, b indices 0..n-1.
a[0] = b[0]
a[1] = b[n-1] if n>=2.
left = 1 (since next left index to take from b for a3)
right = n-2 (next right index for a4)
For i from 2 to n-1 (i is index in a for a3..a_n, i from 2 to n-1 inclusive in 0-index):
if (i % 2 == 0)?? Need to map parity: In a, indices: 0-based:
a[0] = a1
a[1] = a2
a[2] = a3
a[3] = a4
a[4] = a5
a[5] = a6
...
We need to decide when to take from left or right. For original forward process: For a3 (i=2) we took from left; for a4 (i=3) from right; for a5 (i=4) from left; a6 (i=5) from right; etc. So pattern: For a index i (0-based), if i is even? Let's test: i=2 (a3) -> left; i=2 is even (0,1,2,3,...). i=3 (a4) -> right; i=3 odd. i=4 (a5) -> left; i=4 even. i=5 (a6) -> right; i=5 odd. So indeed, for i >= 2, if i is even -> take from left; if i is odd -> take from right. Because 0-index: a1 index0; a2 index1; a3 index2 (even) left; a4 index3 (odd) right; a5 index4 (even) left; etc.
So algorithm:
a[0] = b[0]
if n>1: a[1] = b[n-1]
left = 1
right = n-2
for idx in range(2, n): # idx is index of a
if idx % 2 == 0:
a[idx] = b[left]
left += 1
else:
a[idx] = b[right]
right -= 1
Edge cases:
n=1: then a[0] = b[0] (since only a1). The loop from 2 to n-1 is empty. So a = [b0].
n=2: a[0]=b0, a[1]=b1? Wait a2 = b[n-1] = b[1]. So a = [b0, b1]. That matches sample: Input: 2, b = [11,7]; output: 11 7. So correct.
n=3: We have a1 = b0, a2 = b2, left=1, right=1? Actually n=3 => left=1, right= n-2 =1. Loop idx=2 only: idx=2 even -> take from left (b[1]) -> a3 = b1. So a = [b0, b2, b1]. Let's test with a small example: Suppose a = [x, y, z]. Forward: b1 = a1 = x; b3 = a2 = y; b2 = a3 = z. So b = [x,z,y]? Wait forward: n=3. Steps: a1->pos1; a2->pos3; a3->pos2. So b = [a1, a3, a2] = [x, z, y]. So reverse: a1 = b1 = x; a2 = b3 = y; a3 = b2 = z. So yes a = [b1, b3, b2] which matches our algorithm.
Thus algorithm works.
Complexities: O(n) per test case.
Now ensure to output with spaces and newline as in sample.
We must output t lines, each containing the sequence a with spaces and trailing space? Sample output shows trailing space after each number and before newline? Actually sample output lines:
Each line ends with a space before newline. Usually it's fine to print numbers separated by spaces, maybe without trailing space, but the sample includes trailing space. We can output exactly as needed: print each number with a space after, or join with space and print. The judge usually ignores trailing spaces. So we can simply print ' '.join(map(str,a)) (which does not have trailing space) and that should be accepted. But in the sample they have trailing space. However we can output without trailing space, it's fine. We'll output using join.
We should be careful about 1-index vs 0-index and parity.
Alternatively, we can simulate using deque: push left and right. But simple pointer approach is fine.
But is there any possibility of misinterpretation? Let's ensure the process described matches the filling from ends. In the problem statement: "he wrote the number a_1 to the left side (at the beginning of the whiteboard); he wrote the number a_2 to the right side (at the end of the whiteboard); then as far to the left as possible (but to the right from a_1), he wrote the number a_3; then as far to the right as possible (but to the left from a_2), he wrote the number a_4; Polycarp continued to act as well, until he wrote out the entire sequence on the whiteboard." The phrase "as far to the left as possible (but to the right from a_1)" for a3 indeed corresponds to taking the leftmost empty position after a1. After a3 is placed, for a4, "as far to the right as possible (but to the left from a_2)" corresponds to the rightmost empty position before a2. Then the process continues similarly, alternating. So the filling from ends interpretation is correct.
Now what about if there are duplicates? No issue.
Thus the solution is as above.
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
b = [int(next(it)) for _ in range(n)]
a = [0]*n
a[0] = b[0]
if n > 1:
a[1] = b[-1]
left = 1
right = n-2
for idx in range(2, n):
if idx % 2 == 0:
a[idx] = b[left]
left += 1
else:
a[idx] = b[right]
right -= 1
out_lines.append(' '.join(map(str, a)))
sys.stdout.write('\n'.join(out_lines))
Check for n=1: b[-1] = b[0] but we only assign a[1] if n>1, so fine.
Edge case: n=2: left=1, right=0? Actually n=2 => right = n-2 = 0. But we don't enter loop because range(2,2) empty. So fine.
n=3: left=1, right=1. idx=2 even -> a[2] = b[1]; left becomes 2; loop ends. So ok.
Now test with provided samples.
We'll run mental simulation for each:
Case 1: n=7 b=[3,4,5,2,9,1,1] -> a computed above matches.
Case 2: n=4 b=[9,2,7,1] -> a = [9,1,2,7] matches.
Case 3: n=11 b=[8,4,3,1,2,7,8,7,9,4,2] -> a computed matches.
Case 4: n=1 b=[42] -> a=[42].
Case 5: n=2 b=[11,7] -> a=[11,7].
Case 6: n=8 all ones -> a all ones.
Thus correct.
But note: In the third sample, the output is "8 2 4 4 3 9 1 7 2 8 7". In our computed a we got [8,2,4,4,3,9,1,7,2,8,7] yes.
All good.
Thus solution is simple.
However, we need to consider if there is any other possible interpretation? I recall some Codeforces problem "Polycarp and the Whiteboard" or similar? Possibly the process is exactly as we described. It is essentially reconstructing the original sequence from the final arrangement when numbers are inserted alternating from left and right. So it's like we have to "unwrap" the process.
Thus final answer.
Now let's produce final response: Explanation and then code. We are given the final sequence b that results from the process described. The process can be understood as filling an array of length n from both ends:
place a₁ at position 1,
place a₂ at position n,
place a₃ at the leftmost free position (position 2),
place a₄ at the rightmost free position (position n-1),
continue alternating: odd-indexed a (starting from a₃) go to the next free spot from the left, even-indexed a (starting from a₄) go to the next free spot from the right.
Thus the final arrangement b satisfies:
b[1] = a₁,
b[n] = a₂,
for the remaining indices: b[2] = a₃, b[n-1] = a₄, b[3] = a₅, b[n-2] = a₆, …
To recover a from b we reverse this mapping:
a[1] = b[1],
a[2] = b[n],
then use two pointers: left = 2, right = n-1.
for i from 3 to n:
if i is odd: take a[i] = b[left] and increment left,
if i is even: take a[i] = b[right] and decrement right.
In 0‑based indexing the rule becomes:
a[0] = b[0],
if n > 1: a[1] = b[n-1],
left = 1, right = n-2,
for idx from 2 to n-1:
if idx is even: a[idx] = b[left], left++,
else: a[idx] = b[right], right--.
The constraints are small (t ≤ 300, n ≤ 300), so this linear-time reconstruction works per test case.
python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it))
b = [int(next(it)) for _ in range(n)]
a = [0] * n
a[0] = b[0]
if n > 1:
a[1] = b[-1]
left = 1
right = n - 2
for idx in range(2, n):
if idx % 2 == 0: # a₃, a₅, a₇, ...
a[idx] = b[left]
left += 1
else: # a₄, a₆, a₈, ...
a[idx] = b[right]
right -= 1
out.append(' '.join(map(str, a)))
sys.stdout.write('\n'.join(out))
if __name__ == "__main__":
solve()