CF BUDDY
← Problems·

1991B · AND Reconstruction

1100 · bitmasks, constructive algorithms, greedy

Problem: You are given an array bb of n1n - 1 integers.

An array aa of nn integers is called good if bi=ai&ai+1b_i = a_i \, \& \, a_{i + 1} for 1in11 \le i \le n-1, where &\& denotes the bitwise AND operator.

Construct a good array, or report that no good arrays exist.

Input Format: Each test contains multiple test cases. The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (2n1052 \le n \le 10^5) — the length of the array aa.

The second line of each test case contains n1n - 1 integers b1,b2,,bn1b_1, b_2, \ldots, b_{n - 1} (0bi<2300 \le b_i < 2^{30}) — the elements of the array bb.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output Format: For each test case, output a single integer 1-1 if no good arrays exist.

Otherwise, output nn space-separated integers a1,a2,,ana_1, a_2, \ldots, a_n (0ai<2300 \le a_i < 2^{30}) — the elements of a good array aa.

If there are multiple solutions, you may output any of them.

Note: In the first test case, b=[1]b = [1]. A possible good array is a=[5,3]a=[5, 3], because a1&a2=5&3=1=b1a_1 \, \& \, a_2 = 5 \, \& \, 3 = 1 = b_1.

In the second test case, b=[2,0]b = [2, 0]. A possible good array is a=[3,2,1]a=[3, 2, 1], because a1&a2=3&2=2=b1a_1 \, \& \, a_2 = 3 \, \& \, 2 = 2 = b_1 and a2&a3=2&1=0=b2a_2 \, \& \, a_3 = 2 \, \& \, 1 = 0 = b_2.

In the third test case, b=[1,2,3]b = [1, 2, 3]. It can be shown that no good arrays exist, so the output is 1-1.

In the fourth test case, b=[3,5,4,2]b = [3, 5, 4, 2]. A possible good array is a=[3,7,5,6,3]a=[3, 7, 5, 6, 3].

Sample Cases

Case 1

Input

4
2
1
3
2 0
4
1 2 3
5
3 5 4 2

Output

5 3
3 2 1
-1
3 7 5 6 3

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback