#K67682. Reverse Subarray to Lexicographically Smallest Sequence
Reverse Subarray to Lexicographically Smallest Sequence
Reverse Subarray to Lexicographically Smallest Sequence
Given a sequence of integers \(a_1, a_2, \dots, a_n\) you are allowed to perform exactly two subarray reversal operations. In each operation, you may choose any contiguous subsequence of the array and reverse its order. Your task is to apply two such operations (which can be trivial, i.e. of length 1, effectively leaving the sequence unchanged) in order to obtain the lexicographically smallest sequence possible.
A sequence \(X = [x_1, x_2, \dots, x_n]\) is said to be lexicographically smaller than a sequence \(Y = [y_1, y_2, \dots, y_n]\) if there exists an index \(k\) such that \(x_i = y_i\) for all \(i < k\) and \(x_k < y_k\). For instance, among [4, 3, 2, 1, 5] and [1, 2, 3, 4, 5], the latter is lexicographically smaller because \(1 < 4\).
Your program should read multiple test cases from standard input, process each test case, and output the resulting sequence for each test case to standard output.
inputFormat
The input is given via standard input and has the following format:
T N1 A1,1 A1,2 ... A1,N1 N2 A2,1 A2,2 ... A2,N2 ... NT AT,1 AT,2 ... AT,NT
Here, T is the number of test cases. For each test case, the first integer denotes the number of elements in the sequence, followed by the sequence of integers.
outputFormat
For each test case, output a single line containing the lexicographically smallest sequence obtainable by performing the two reversal operations. The numbers in each sequence should be separated by a single space.
## sample1
3
3 2 1
1 2 3
</p>