#C6589. Reverse to Minimize

    ID: 50365 Type: Default 1000ms 256MiB

Reverse to Minimize

Reverse to Minimize

You are given an array of integers. You are allowed to reverse any contiguous subarray of the given array (including the possibility of reversing a single element, which leaves the array unchanged). Your task is to find the lexicographically smallest sequence that you can obtain by applying exactly one reversal operation.

For example, if the array is [4, 3, 1, 2], one optimal reversal is to reverse the subarray starting at the first element and ending at the third element resulting in [1, 3, 4, 2].

Note: The lexicographical order is defined in the usual manner, i.e. an array A is considered smaller than array B if at the first position where they differ, the element in A is smaller than the corresponding element in B.

inputFormat

The first line of the input contains an integer T (1 ≤ T ≤ 10) representing the number of test cases. Each test case consists of two lines:

  • The first line contains an integer n (1 ≤ n ≤ 1000), the number of elements in the array.
  • The second line contains n space-separated integers.

It is guaranteed that the input size is small enough to allow a solution with a cubic time complexity.

outputFormat

For each test case, output a single line representing the lexicographically smallest sequence which can be obtained by reversing exactly one contiguous subarray of the given array. The elements in the sequence should be separated by a single space.

## sample
2
4
4 3 1 2
5
1 2 3 5 4
1 3 4 2

1 2 3 4 5

</p>