#ARC113E. [ARC113E] Rvom and Rsrev

[ARC113E] Rvom and Rsrev

Score : 800800 points

Problem Statement

Given is a string SS consisting of a and b. Find the lexicographically greatest string that can be obtained by applying the following operation to SS zero or more times:

  • Choose two characters of SS that are the same letter. Reverse the string between them and delete the chosen two characters. In other words: let sis_i denote the ii-th character of SS. Choose i<ji < j such that si=sjs_i = s_j and replace SS with $s_1\dots s_{i-1}s_{j-1}\dots s_{i+1}s_{j+1}\dots s_{|S|}$.

In this problem, you are given TT test cases. The ii-th test case is represented as SiS_i and asks you to solve the problem above for S=SiS = S_i.

Constraints

  • 1T2×1051 \leq T \leq 2\times 10^5
  • 1Si(i=1,,T)1 \leq |S_i|\quad (i=1,\dots, T)
  • 1S1++ST2×1051 \leq |S_1| + \dots + |S_T| \leq 2\times 10^5
  • SiS_i consists of a and b.

Input

Input is given from Standard Input in the following format:

TT

S1S_1

\vdots

STS_T

Output

Print TT lines. The ii-th line should contain the lexicographically greatest string that can be obtained by applying the operation to SiS_i zero or more times.

20
abbaa
babbb
aabbabaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbabaaaaabbaababaaabbabbbbbaaaaa
babbbaaaabaababbbabaabaaaaababaa
bbaababababbbaaabaabababaabbabab
abaabbabaabaaaaabaaaabbaabaaaaab
aabababbabbabbabbaaaabbabbbabaab
aabababbabbbbaaaabbaaaaabbaaaabb
abbbbaabaaabababaaaababababbaabb
aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbb
aaaaaaaaaabbbbbbbbbbbbbbbbbbbbbb
abababaaababaaabbbbbaaaaaaabbbbb
aabbaaaaababaabbbbbbbbbaabaaabbb
babababbababbbababbbbbababbbabbb
bbbbababbababbbabababbabbabbbbbb
aaaaaaaaaaaaaaaaababababbbabbbbb
aabababbabbabababababababbbbabbb
bba
bba
bbba
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbaaaaaaaa
bbbbbbbbbbbbbaaaaaaa
bbbbbbbbbbbbbbbb
bbbbbbbbbb
bbbbbbbbbbbbbbbbab
bbbbbbbbbbbbbb
bbbbbbbbbbbbbabb
abbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbaaaaaaaaa
bbbbbbbbbbbbbbbaaaaa
bbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbba
bbbbbbbbbaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbba
  • In the 11-st test case, we can do the operation for the 11-st and 44-th characters to make SS bba.
  • In the 22-nd test case, we can do the operation for the 11-st and 55-th characters to make SS bba.
  • In the 33-rd test case, we can do the operation for the 11-st and 22-nd characters to make SS bbabaa, then do it for the 33-rd and 55-th characters to make SS bbba.