QOJ.ac

QOJ

実行時間制限: 1.0 s メモリ制限: 1024 MB 満点: 100 ハック可能 ✓

#18478. Baldur's Leap 3

統計

You are given a string $S$ consisting of lowercase English letters and a string $T$ that is initially empty.

You need to perform operations on $S$ until it becomes empty. In each operation, you may perform one of the following three actions:

  • Delete the first character of $S$ and append it to the end of $T$.
  • Delete the last character of $S$ and append it to the end of $T$.
  • Choose nonempty strings $A,B$ satisfying $S=ABA^R$, set $S=B$, and append the string $A$ to the end of $T$.

Here, $A^R$ denotes the reverse of the string $A$.

Find the lexicographically smallest $T$ obtainable over all sequences of operations.

Input

This problem contains multiple test cases.

The first line contains a positive integer $t$ ($1\le t\le 10^5$), representing the number of test cases.

For each test case:

One line contains a string $S$ consisting of lowercase letters ($1\le |S|\le 5\times 10^5$).

It is guaranteed that the sum of $|S|$ over all test cases does not exceed $5\times 10^5$.

Output

For each test case, output a string representing the answer on one line.

Examples

Input 1

6
cba
zayaz
aaaaa
xazbx
ababa
aa

Output 1

abc
zaay
aaa
xabz
aaba
aa

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 1
IDTypeStatusTitlePosted ByLast UpdatedActions
#2703Off-topicOpenNo EditorialThisIsLuView

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.