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

Difficulty: interview

Starter code:

Solutions:
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()

import sys
input = sys.stdin.readline

t=int(input())
for tests in range(t):
    S=input().strip()

    ANSP=[]
    #ANSS=[]

    f=0
    l=len(S)-1

    while f<l and S[f]==S[l]:
        ANSP.append(S[f])
        #ANSS.append(S[l])
        f+=1
        l-=1

    if f==l or f==l+1:
        print(S)
        continue

    #print(ANSP)

    S2=S[f:l+1]
    #print(S2)

    SS2=[]
    for s in S2:
        SS2.append(s)
        SS2.append("$")
    SS2.pop()

    #print(SS2)

    LEN=len(SS2)
    i=0
    j=0
    R=[0]*LEN # 文字 i を中心とする最長の回文の半径

    while i<LEN:
        while i-j>=0 and i+j<LEN and SS2[i-j]==SS2[i+j]:
            j+=1
        R[i]=j
        
        k=1
        while i-k>=0 and i+k<LEN and k+R[i-k]<j:
            R[i+k]=R[i-k]
            k+=1

        i+=k
        j-=k

    #print(R)

    MAX=0

    for i in range(LEN):
        if i-R[i]+1==0 or i+R[i]==LEN:
            if MAX<R[i]:
                MAX=R[i]
                MAXind=i

    S3=SS2[MAXind-MAX+1:MAXind+MAX]

    #print(S3)

    SS3=[]
    for s in S3:
        if s!="$":
            SS3.append(s)

    sys.stdout.write("".join(ANSP)+"".join(SS3)+"".join(ANSP[::-1])+"\n")