#C2587. Maximize Lexicographical String

    ID: 45919 Type: Default 1000ms 256MiB

Maximize Lexicographical String

Maximize Lexicographical String

You are given a string \(S\) consisting of lowercase English letters and an integer \(K\). You are required to perform exactly \(K\) operations on the string. In each operation, you can select any non-empty substring of \(S\) and reverse it. Your goal is to obtain the lexicographically largest string possible after these operations.

Note: Even if the lexicographically largest string can be achieved in fewer than \(K\) operations, you must perform exactly \(K\) operations. In such cases, you may reverse a substring of length 1 (which does not change the string) for the extra operations.

For example, if \(S = \texttt{abba}\) and \(K = 2\), one optimal approach is to sort the characters in descending order to get \(\texttt{bbaa}\), and if an extra reversal is needed, reversing a single character will keep the string unchanged.

inputFormat

The input is given from stdin and is structured as follows:

  • The first line contains an integer \(T\) which denotes the number of test cases.
  • Each of the following \(T\) lines contains a test case with a string \(S\) (consisting of lowercase letters) followed by an integer \(K\) separated by a space.

outputFormat

For each test case, output the lexicographically largest string possible after exactly \(K\) operations. Each answer should be printed on a new line to stdout.

## sample
3
abc 1
abba 2
dcba 1
cba

bbaa dcba

</p>