No submission language available for this problem.
Description
Ntarsis has an array a of length n.
The power of a subarray al…ar (1≤l≤r≤n) is defined as:
- The largest value x such that al…ar contains x and neither a1…al−1 nor ar+1…an contains x.
- If no such x exists, the power is 0.
Call an array b a rival to a if the following holds:
- The length of both a and b are equal to some n.
- Over all l,r where 1≤l≤r≤n, the power of al…ar equals the power of bl…br.
- The elements of b are positive.
Ntarsis wants you to find a rival b to a such that the sum of bi over 1≤i≤n is maximized. Help him with this task!
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤105). The description of the test cases follows.
The first line of each test case has a single integer n (1≤n≤105).
The next line contains n integers a1,a2,…,an (1≤ai≤109).
It is guaranteed that the sum of n across all test cases does not exceed 2⋅105.
For each test case, output n integers b1,b2,…,bn — a valid rival to a such that b1+b2+⋯+bn is maximal.
If there exist multiple rivals with the maximum sum, output any of them.
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤105). The description of the test cases follows.
The first line of each test case has a single integer n (1≤n≤105).
The next line contains n integers a1,a2,…,an (1≤ai≤109).
It is guaranteed that the sum of n across all test cases does not exceed 2⋅105.
Output
For each test case, output n integers b1,b2,…,bn — a valid rival to a such that b1+b2+⋯+bn is maximal.
If there exist multiple rivals with the maximum sum, output any of them.
Note
For the first test case, one rival with the maximal sum is [2,4,2,3,3].
[2,4,2,3,3] can be shown to be a rival to [1,4,1,3,3].
All possible subarrays of a and b and their corresponding powers are listed below:
- The power of a[1:1]=[1]=0, the power of b[1:1]=[2]=0.
- The power of a[1:2]=[1,4]=4, the power of b[1:2]=[2,4]=4.
- The power of a[1:3]=[1,4,1]=4, the power of b[1:3]=[2,4,2]=4.
- The power of a[1:4]=[1,4,1,3]=4, the power of b[1:4]=[2,4,2,3]=4.
- The power of a[1:5]=[1,4,1,3,3]=4, the power of b[1:5]=[2,4,2,3,3]=4.
- The power of a[2:2]=[4]=4, the power of b[2:2]=[4]=4.
- The power of a[2:3]=[4,1]=4, the power of b[2:3]=[4,2]=4.
- The power of a[2:4]=[4,1,3]=4, the power of b[2:4]=[4,2,3]=4.
- The power of a[2:5]=[4,1,3,3]=4, the power of b[2:5]=[4,2,3,3]=4.
- The power of a[3:3]=[1]=0, the power of b[3:3]=[2]=0.
- The power of a[3:4]=[1,3]=0, the power of b[3:4]=[2,3]=0.
- The power of a[3:5]=[1,3,3]=3, the power of b[3:5]=[2,3,3]=3.
- The power of a[4:4]=[3]=0, the power of b[4:4]=[3]=0.
- The power of a[4:5]=[3,3]=3, the power of b[4:5]=[3,3]=3.
- The power of a[5:5]=[3]=0, the power of b[5:5]=[3]=0.
It can be shown there exists no rival with a greater sum than 2+4+2+3+3=14.