#P1852E. Rivalries

    ID: 8565 Type: RemoteJudge 2000ms 512MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>constructive algorithmsdata structuresgreedy

Rivalries

No submission language available for this problem.

Description

Ntarsis has an array aa of length nn.

The power of a subarray alara_l \dots a_r (1lrn1 \leq l \leq r \leq n) is defined as:

  • The largest value xx such that alara_l \dots a_r contains xx and neither a1al1a_1 \dots a_{l-1} nor ar+1ana_{r+1} \dots a_n contains xx.
  • If no such xx exists, the power is 00.

Call an array bb a rival to aa if the following holds:

  • The length of both aa and bb are equal to some nn.
  • Over all l,rl, r where 1lrn1 \leq l \leq r \leq n, the power of alara_l \dots a_r equals the power of blbrb_l \dots b_r.
  • The elements of bb are positive.

Ntarsis wants you to find a rival bb to aa such that the sum of bib_i over 1in1 \leq i \leq n is maximized. Help him with this task!

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1051 \le t \le 10^5). The description of the test cases follows.

The first line of each test case has a single integer nn (1n1051 \leq n \leq 10^5).

The next line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091 \leq a_i \leq 10^9).

It is guaranteed that the sum of nn across all test cases does not exceed 21052 \cdot 10^5.

For each test case, output nn integers b1,b2,,bnb_1, b_2, \ldots, b_n — a valid rival to aa such that b1+b2++bnb_1 + b_2 + \cdots + b_n is maximal.

If there exist multiple rivals with the maximum sum, output any of them.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1051 \le t \le 10^5). The description of the test cases follows.

The first line of each test case has a single integer nn (1n1051 \leq n \leq 10^5).

The next line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091 \leq a_i \leq 10^9).

It is guaranteed that the sum of nn across all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, output nn integers b1,b2,,bnb_1, b_2, \ldots, b_n — a valid rival to aa such that b1+b2++bnb_1 + b_2 + \cdots + b_n is maximal.

If there exist multiple rivals with the maximum sum, output any of them.

Sample Input 1

7
5
1 4 1 3 3
5
1 4 1 8 8
5
2 1 1 1 2
8
3 2 3 5 2 2 5 3
8
1 1 1 1 4 3 3 3
10
1 9 5 9 8 1 5 8 9 1
16
1 1 1 1 5 5 5 5 9 9 9 9 7 7 7 7

Sample Output 1

2 4 2 3 3
3 4 3 8 8
2 1 2 1 2
4 2 4 5 5 2 5 4
1 2 2 1 4 3 2 3
7 9 5 9 8 9 5 8 9 7
1 8 8 1 5 8 8 5 9 9 9 9 7 8 8 7

Note

For the first test case, one rival with the maximal sum is [2,4,2,3,3][2, 4, 2, 3, 3].

[2,4,2,3,3][2, 4, 2, 3, 3] can be shown to be a rival to [1,4,1,3,3][1, 4, 1, 3, 3].

All possible subarrays of aa and bb and their corresponding powers are listed below:

  • The power of a[1:1]=[1]=0a[1:1] = [1] = 0, the power of b[1:1]=[2]=0b[1:1] = [2] = 0.
  • The power of a[1:2]=[1,4]=4a[1:2] = [1, 4] = 4, the power of b[1:2]=[2,4]=4b[1:2] = [2, 4] = 4.
  • The power of a[1:3]=[1,4,1]=4a[1:3] = [1, 4, 1] = 4, the power of b[1:3]=[2,4,2]=4b[1:3] = [2, 4, 2] = 4.
  • The power of a[1:4]=[1,4,1,3]=4a[1:4] = [1, 4, 1, 3] = 4, the power of b[1:4]=[2,4,2,3]=4b[1:4] = [2, 4, 2, 3] = 4.
  • The power of a[1:5]=[1,4,1,3,3]=4a[1:5] = [1, 4, 1, 3, 3] = 4, the power of b[1:5]=[2,4,2,3,3]=4b[1:5] = [2, 4, 2, 3, 3] = 4.
  • The power of a[2:2]=[4]=4a[2:2] = [4] = 4, the power of b[2:2]=[4]=4b[2:2] = [4] = 4.
  • The power of a[2:3]=[4,1]=4a[2:3] = [4, 1] = 4, the power of b[2:3]=[4,2]=4b[2:3] = [4, 2] = 4.
  • The power of a[2:4]=[4,1,3]=4a[2:4] = [4, 1, 3] = 4, the power of b[2:4]=[4,2,3]=4b[2:4] = [4, 2, 3] = 4.
  • The power of a[2:5]=[4,1,3,3]=4a[2:5] = [4, 1, 3, 3] = 4, the power of b[2:5]=[4,2,3,3]=4b[2:5] = [4, 2, 3, 3] = 4.
  • The power of a[3:3]=[1]=0a[3:3] = [1] = 0, the power of b[3:3]=[2]=0b[3:3] = [2] = 0.
  • The power of a[3:4]=[1,3]=0a[3:4] = [1, 3] = 0, the power of b[3:4]=[2,3]=0b[3:4] = [2, 3] = 0.
  • The power of a[3:5]=[1,3,3]=3a[3:5] = [1, 3, 3] = 3, the power of b[3:5]=[2,3,3]=3b[3:5] = [2, 3, 3] = 3.
  • The power of a[4:4]=[3]=0a[4:4] = [3] = 0, the power of b[4:4]=[3]=0b[4:4] = [3] = 0.
  • The power of a[4:5]=[3,3]=3a[4:5] = [3, 3] = 3, the power of b[4:5]=[3,3]=3b[4:5] = [3, 3] = 3.
  • The power of a[5:5]=[3]=0a[5:5] = [3] = 0, the power of b[5:5]=[3]=0b[5:5] = [3] = 0.

It can be shown there exists no rival with a greater sum than 2+4+2+3+3=142 + 4 + 2 + 3 + 3 = 14.