human
Give me a Python program to find the longest string, $t$, which satisfies the following conditions:   The length of $t$ does not exceed the length of $s$ given the following:
Solve in Python:
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-----

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-----

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

-----Example-----
Input
5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba

Output
a
abcdfdcba
xyzyx
c
abba


-----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".
assistant
#Here is Python program to find the longest string, $t$, which satisfies the following conditions:   The length of $t$ does not exceed the length of $s$.

def main():
    import sys
    input = sys.stdin.readline

    # 偶数長含めた回文の長さを求める
    # R[2*i] = L: S[i]を中心とする奇数長の最大回文
    # R[2*i+1] = L: S[i:i+2]を中心とする偶数長の最大回文
    # ダミー文字を挟むが、各 R[i] は実際の回文の文字列長と一致する
    def manacher(S):
        C = []
        for a in S:
            C.append(a)
            C.append(0)
        C.pop()

        L = len(C)

        R = [0] * L

        i = j = 0
        while i < L:
            while j <= i < L - j and C[i - j] == C[i + j]:
                j += 1
            R[i] = j
            k = 1
            while j - R[i - k] > k <= i < L - k:
                R[i + k] = R[i - k]
                k += 1
            i += k
            j -= k
        return R

    for _ in range(int(input())):
        S = input().rstrip('\n')
        N = len(S)
        if N == 1:
            print(S)
            continue
        ii = -1
        for i in range(N//2):
            if S[i] != S[N-1-i]:
                break
            ii = i
        if ii >= 0:
            ss = S[:ii+1]
            S = S[ii+1:N-ii-1]
        else:
            ss = ''
        N = len(S)
        if N == 0:
            print(''.join([ss, ss[::-1]]))
            continue
        R = manacher(S)
        M = 0
        iii = 0
        for i in range(N):
            if R[i*2] > M:
                if R[i*2]%2 == 0:
                    continue
                k = R[i*2] // 2
                if i-k == 0 or i+k == N-1:
                    M = R[i*2]
                    iii = i
        for i in range(N-1):
            if R[i*2+1] > M:
                if R[i*2+1]%2 == 1:
                    continue
                k = R[i*2+1] // 2
                if i-k+1 == 0 or i+k == N-1:
                    M = R[i*2+1]
                    iii = i
        if M & 1:
            ans = S[iii - M//2: iii + M // 2 + 1]
        else:
            ans = S[iii - M // 2 + 1: iii + M // 2 + 1]
        print(''.join([ss, ans, ss[::-1]]))
        #print(S, R, iii, M)

def __starting_point():
    main()

__starting_point()