#P16968. [SCCPC 2026] 博德之跃 3

    ID: 19052 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>贪心四川2026省赛/邀请赛

[SCCPC 2026] 博德之跃 3

Problem Description

You are given a string SS consisting of lowercase English letters, and an initially empty string TT.

You need to perform some operations on SS until it becomes an empty string. Each time, you may perform one of the following three operations:

  • Delete the first character of SS and append it to the end of TT.
  • Delete the last character of SS and append it to the end of TT.
  • Choose non-empty strings A,BA, B such that S=ABARS = A B A^R. Let S=BS = B, and append the string AA to the end of TT.

Here, ARA^R denotes the reverse of the string AA.

Among all possible sequences of operations, find the lexicographically smallest TT that can be obtained.

Input Format

This problem contains multiple testdata.

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

For each test case:

Input one line containing a string SS of lowercase letters (1S5×1051 \le |S| \le 5 \times 10^5).

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

Output Format

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

6
cba
zayaz
aaaaa
xazbx
ababa
aa
abc
zaay
aaa
xabz
aaba
aa

Hint

Translated by ChatGPT 5