#P1506G. Maximize the Remaining String

    ID: 5854 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>brute forcedata structuresdpgreedystrings*2000

Maximize the Remaining String

No submission language available for this problem.

Description

You are given a string ss, consisting of lowercase Latin letters. While there is at least one character in the string ss that is repeated at least twice, you perform the following operation:

  • you choose the index ii (1is1 \le i \le |s|) such that the character at position ii occurs at least two times in the string ss, and delete the character at position ii, that is, replace ss with s1s2si1si+1si+2sns_1 s_2 \ldots s_{i-1} s_{i+1} s_{i+2} \ldots s_n.

For example, if s=s="codeforces", then you can apply the following sequence of operations:

  • i=6s=i=6 \Rightarrow s="codefrces";
  • i=1s=i=1 \Rightarrow s="odefrces";
  • i=7s=i=7 \Rightarrow s="odefrcs";

Given a given string ss, 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 aa of length nn is lexicographically less than a string bb of length mm, if:

  • there is an index ii (1imin(n,m)1 \le i \le \min(n, m)) such that the first i1i-1 characters of the strings aa and bb are the same, and the ii-th character of the string aa is less than ii-th character of string bb;
  • or the first min(n,m)\min(n, m) characters in the strings aa and bb are the same and n<mn < m.

For example, the string a=a="aezakmi" is lexicographically less than the string b=b="aezus".

The first line contains one integer tt (1t1041 \le t \le 10^4). Then tt test cases follow.

Each test case is characterized by a string ss, consisting of lowercase Latin letters (1s21051 \le |s| \le 2 \cdot 10^5).

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

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 tt (1t1041 \le t \le 10^4). Then tt test cases follow.

Each test case is characterized by a string ss, consisting of lowercase Latin letters (1s21051 \le |s| \le 2 \cdot 10^5).

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

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.

Samples

Sample Input 1

6
codeforces
aezakmi
abacaba
convexhull
swflldjgpaxs
myneeocktxpqjpz

Sample Output 1

odfrces
ezakmi
cba
convexhul
wfldjgpaxs
myneocktxqjpz