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:
# from sys import stdin
# inf=stdin

# def help(a):
# 	s=a+"#"+a[::-1]
# 	c=0
# 	pref=[0]*(len(s))
# 	for i in range(1,len(s)):
# 		while(c!=0 and s[c]!=s[i]):
# 			c=pref[c-1]
# 			print("changed",c,pref[c-1])
# 		if(s[i]==s[c]):
# 			c+=1
# 		pref[i]=c
# 		print(pref,c)
# 	return a[:c]

def help(s):
	if(s==""):
		return ""
	base=26
	mod=86028121
	mod1=179424673
	hash1=0
	hash2=0
	hash11=0
	hash22=0

	pos=-1
	pow=1
	pow1=1
	for  i in range(len(s)):
		hash1=(hash1*base+ord(s[i])-ord("a"))%mod
		hash2=(hash2+(ord(s[i])-ord("a"))*pow)%mod

		hash11=(hash11*base+ord(s[i])-ord("a"))%mod1
		hash22=(hash22+(ord(s[i])-ord("a"))*pow1)%mod1

		if hash1==hash2 and hash11==hash22:
			pos=i
		pow=pow*base%mod
		pow1=pow1*base%mod1
	return s[:pos+1]

for _ in range(int(input())):
	s=input()
	n=len(s)
	count=0
	i=0
	j=n-1
	while j>i:
		if(s[j]!=s[i]):
			break
		count+=1
		i+=1
		j-=1

	posi1=help(s[count:n-count])
	posi2=help(s[count:n-count][::-1])

	print(s[:count]+(posi1 if(len(posi1)>len(posi2)) else posi2)+s[:count][::-1])


# for i in dicti:
# 	temp=0
# 	for j in helper:
# 		if(j[0]<=i[0] and j[1]<=i[1]):
# 			temp+=(i[0]-j[0]+1)*(i[1]-j[1]+1)
# 		if(j[1]<=i[0] and j[0]<=i[1] and j[0]!=j[1]):
# 			temp+=(i[0]-j[1]+1)*(i[1]-j[0]+1)
# 	count+=temp*dicti[i]



# def all_partitions(string):
#     for cutpoints in range(1 << (len(string)-1)):
#         result = []
#         lastcut = 0
#         for i in range(len(string)-1):
#             if (1<<i) & cutpoints != 0:
#                 result.append(string[lastcut:(i+1)])
#                 lastcut = i+1
#         result.append(string[lastcut:])
#         yield result

# maxyet=0
# store={'h': -6, 'e': -7, 'l': -8, 'o': 3, 'he': 3, 'hel': -3, 'el': 0, 'hell': 6, 'ell': -5, 'll': 10, 'hello': -3, 'ello': -8, 'llo': 9, 'lo': -6}

# def quality(stri):
# 	return store[stri]

# for partition in all_partitions("hel"):
#     temp=0
#     for i in partition:
#     	temp+=quality(i)
#     if(temp>maxyet):
#     	print(partition)
#     	maxyet=temp

# print(maxyet)