Prefix-Suffix Palindrome (Hard version)
Contest: Codeforces Global Round 7
Difficulty rating: 1800
Tags: binary search, greedy, hashing, string suffix structures, strings

This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.

You are given a string $$$s$$$, consisting of lowercase English letters. Find the longest string, $$$t$$$, which satisfies the following conditions:

- The length of $$$t$$$ does not exceed the length of $$$s$$$.
- $$$t$$$ is a palindrome.
- There exists two strings $$$a$$$ and $$$b$$$ (possibly empty), such that $$$t = a + b$$$ ( "$$$+$$$" represents concatenation), and $$$a$$$ is prefix of $$$s$$$ while $$$b$$$ is suffix of $$$s$$$.

Input format:
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$), the number of test cases. The next $$$t$$$ lines each describe a test case.

Each test case is a non-empty string $$$s$$$, consisting of lowercase English letters.

It is guaranteed that the sum of lengths of strings over all test cases does not exceed $$$10^6$$$.

Output format:
For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test, the string $$$s = $$$"a" satisfies all conditions.

In the second test, the string "abcdfdcba" satisfies all conditions, because:

- Its length is $$$9$$$, which does not exceed the length of the string $$$s$$$, which equals $$$11$$$.
- It is a palindrome.
- "abcdfdcba" $$$=$$$ "abcdfdc" $$$+$$$ "ba", and "abcdfdc" is a prefix of $$$s$$$ while "ba" is a suffix of $$$s$$$.

It can be proven that there does not exist a longer string which satisfies the conditions.

In the fourth test, the string "c" is correct, because "c" $$$=$$$ "c" $$$+$$$ "" and $$$a$$$ or $$$b$$$ can be empty. The other possible solution for this test is "s".

Example input:
5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba

Example output:
a
abcdfdcba
xyzyx
c
abba

Editorial:
Each possible string $$$t$$$ can be modeled as $$$s[1..l] + s[(n-r+1)..n]$$$ for some numbers $$$l,r$$$ such that $$$0 \leq l, r$$$ and $$$l + r \leq n$$$.
Let's find the maximum integer $$$0 \leq k \leq \lfloor \frac{n}{2} \rfloor$$$ such that $$$s_1 = s_n, s_2 = s_{n-1}, \ldots, s_k = s_{n-k+1}$$$.
In some optimal solution, where $$$t$$$ is as long as possible, $$$\min{(l, r)} = k$$$. This is because if $$$\min{(l, r)} < k$$$, we can increase $$$l$$$ or $$$r$$$ by $$$1$$$ and decrease the other variable by $$$1$$$ (if needed), and the string will be still a palindrome.
So if we know that $$$\min{(l, r)} = k$$$, we just need to find the longest palindrome $$$w$$$ that is a prefix or suffix of the string $$$s[(k+1)..(n-k)]$$$. After that, the answer will be $$$s[1..k] + w + s[(n-k+1)..n]$$$.
In order to find the longest palindrome which is a prefix of some string, $$$a$$$, let's find $$$p$$$ from the prefix function of the string $$$a +$$$ '#' $$$+ \overline{a}$$$, where $$$\overline{a}$$$ represents the reverse of $$$a$$$. The string $$$a[1..p]$$$ will be the longest palindrome which is a prefix of $$$a$$$.
After that, we can repeat this process for $$$\overline{a}$$$ to find the longest palindrome which is a suffix of the string.
Time complexity: $$$O(|s|)$$$.