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:


"""
NTC here
"""
import sys
inp = sys.stdin.readline
def input(): return inp().strip()
# flush= sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**26)

def iin(): return int(input())


def lin(): return list(map(int, input().split()))


# range = xrange
# input = raw_input

def palindromCentre(s):
    """
    check 
    s = ababa
    S = # a # b # a # b # a #
    p = 0 1 0 3 0 5 0 3 0 1 0
    """
    S = []
    n = len(s)
    N = 2*n+1
    for i in s:
        S.append('#')
        S.append(i)
    S.append('#')
    #print(s, S)
    p = [0]*(N)
    c = 0
    L, R = 0, 0
    i = 0
    while i < N:
       # print('A', L, R, i)
        if i > R:
            start = 1
            p[i] = 0
            l1, r1 = i-start, i+start
            while l1 >= 0 and r1 < N and S[l1] == S[r1]:
               # print('B', l1, r1, p[i])
                p[i] += 1
                l1 -= 1
                r1 += 1
            c = i
            L, R = c - p[i], c + p[i]
        else:
            r = i
            l = 2*c - r
           # print('D', l, r, p[l], R-r)
            if p[l] < R-r or (p[l] == R-r and R == N-1):
                p[r] = p[l]
            else:
                # check brute force
                start = R-r
                p[r] = start
                l1, r1 = i-start-1, i+start+1
                while l1 >= 0 and r1 < N and S[l1] == S[r1]:
                    #print('C', l1, r1, p[i])
                    p[r] += 1
                    l1 -= 1
                    r1 += 1

                c = r
                L, R = c - p[r], c + p[r]
        i += 1
    return p, S


def main():
    def red(ans):
        return ''.join([i for i in ans if i != '#'])
    T = iin()
    while T:
        T-=1
        s = list(input())
        s1, s2 = palindromCentre(s)
        # print(s2)
        n = len(s1)
        s3 = [0]*n
        for i in range(n//2):
            if s2[i]==s2[-i-1]:
                s3[i] +=1
                s3[-i-1]+=1
            else:
                break
        # print(s3)
        # print(s1)
        ans = []
        for i in range(n//2, n):
            x, y= n-i-1, i
            # print(x, y)
            if s3[x-s1[x]]:
                # print('F - ', x, x-s1[x], s1[x])
                ch = x-s1[x]
                ans = (s2[:ch] + s2[x-s1[x]:x+s1[x]+1] + s2[-ch :] ) if ch else s2[x-s1[x]:x+s1[x]+1]
                break
            if s3[n-(y+s1[y])-1]:
                ch = n-(y+s1[y])-1
                # print('F - ', y, ch, s1[y])
                ans = (s2[:ch] + s2[y-s1[y]:y+s1[y]+1] + s2[-ch:] ) if ch else s2[y-s1[y]:y+s1[y]+1]
                break
        print(red(ans))




        

        



main()

# threading.Thread(target=main).start()