#P9348. 小园香径独徘徊
小园香径独徘徊
Background
Wandering along a quiet, deep path, you pick up fragments of memory and put them into two long pockets.
After collecting them all and pouring them out, what kind of story will they form?
Problem Description
There are two strings . Initially, is given and is the empty string. Each time, you may perform one of the following three operations until becomes the empty string:
- Delete the first character of , and insert this character at the beginning of .
- Delete the first character of , and insert this character at the end of .
- Delete the last character of , and insert this character at the beginning of .
a3 wants to know, after becomes the empty string, the lexicographically smallest that can be formed.
Input Format
This problem contains multiple test cases.
The first line contains an integer , which denotes the number of test cases.
For each test case, one line contains a string .
Output Format
For each test case, output one line containing a string, which is the lexicographically smallest that can be formed.
2
ababdca
dcbcadb
aaabbdc
abbcdcd
Hint
[Sample 1 Explanation]
- For , perform operations of type in order, and you can obtain .
- For , perform operations of type in order, and you can obtain .
[Constraints and Agreements]
This problem uses bundled testdata.
- Subtask 1 (5 points): consists of at most two distinct characters.
- Subtask 2 (10 points): .
- Subtask 3 (15 points): .
- Subtask 4 (25 points): .
- Subtask 5 (20 points): .
- Subtask 6 (25 points): no special restrictions.
For of the data, , , , and consists only of lowercase letters.
Translated by ChatGPT 5