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:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
# sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def Manacher(S):
    N = len(S)
    R = [0] * N
    i = j = 0
    while i < N:
        while i-j >= 0 and i+j < N and S[i-j] == S[i+j]:
            j += 1
        R[i] = j
        k = 1
        while i-k >= 0 and i+k < N and k+R[i-k] < j:
            R[i+k] = R[i-k]
            k += 1
        i += k
        j -= k
    return R

def Manacher_even(_S):
    _N = len(_S)
    S = []
    for i in range(_N-1):
        S.append(_S[i])
        S.append('$')
    S.append(_S[-1])
    N = len(S)
    R = [0] * N
    i = j = 0
    while i < N:
        while i-j >= 0 and i+j < N and S[i-j] == S[i+j]:
            j += 1
        R[i] = j
        k = 1
        while i-k >= 0 and i+k < N and k+R[i-k] < j:
            R[i+k] = R[i-k]
            k += 1
        i += k
        j -= k
    res = [0] * (_N-1)
    j = 0
    for i in range(1, N, 2):
        res[j] = R[i] // 2
        j += 1
    return res

for _ in range(INT()):
    S = input()
    N = len(S)
    i = 0
    j = N - 1
    T1 = []
    while i < j:
        if S[i] == S[j]:
            T1.append(S[i])
            i += 1
            j -= 1
        else:
            break
    else:
        print(S)
        continue
    T3 = T1[::-1]
    R = Manacher(S[i:j+1])
    mx = a = b = 0
    for k, r in enumerate(R):
        k += i
        ln = r * 2 - 1
        if k-r+1 == i:
            if ln > mx:
                mx = ln
                a, b = i, i+ln
        if k+r-1 == j:
            if ln > mx:
                mx = ln
                a, b = j+1-ln, j+1
    R = Manacher_even(S[i:j+1])
    for k, r in enumerate(R):
        k += i + 1
        ln = r * 2
        if k-r == i:
            if ln > mx:
                mx = ln
                a, b = i, i+ln
        if k+r-1 == j:
            if ln > mx:
                mx = ln
                a, b = j+1-ln, j+1
    T2 = list(S[a:b])
    ans = T1 + T2 + T3
    print(''.join(ans))