Problem: 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 ≤ t ≤ 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".

Solution (in PYTHON3):

Solution:
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")