#P1506G. Maximize the Remaining String
Maximize the Remaining String
No submission language available for this problem.
Description
You are given a string , consisting of lowercase Latin letters. While there is at least one character in the string that is repeated at least twice, you perform the following operation:
- you choose the index () such that the character at position occurs at least two times in the string , and delete the character at position , that is, replace with .
For example, if "codeforces", then you can apply the following sequence of operations:
- "codefrces";
- "odefrces";
- "odefrcs";
Given a given string , find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
A string of length is lexicographically less than a string of length , if:
- there is an index () such that the first characters of the strings and are the same, and the -th character of the string is less than -th character of string ;
- or the first characters in the strings and are the same and .
For example, the string "aezakmi" is lexicographically less than the string "aezus".
The first line contains one integer (). Then test cases follow.
Each test case is characterized by a string , consisting of lowercase Latin letters ().
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed .
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.
Input
The first line contains one integer (). Then test cases follow.
Each test case is characterized by a string , consisting of lowercase Latin letters ().
It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed .
Output
For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.