Problem: This is the easy 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 ≤ 1000), 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 5000.

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
#inf=open("data1.txt",'rt')
 
#n,k=map(int,inf.readline().split(" "))
#arr=list(map(int,inf.readline().split(" ")))

# Python program to implement Manacher's Algorithm 

# def findLongestPalindromicString(text): 
# 	N = len(text) 
# 	if N == 0: 
# 		return ""
# 	elif N==1:
# 		return text
# 	N = 2*N+1 # Position count 
# 	L = [0] * (N+1)
# 	L[0] = 0
# 	L[1] = 1
# 	C = 1	 # centerPosition 
# 	R = 2	 # centerRightPosition 
# 	i = 0 # currentRightPosition 
# 	iMirror = 0	 # currentLeftPosition 
# 	maxLPSLength = 0
# 	maxLPSCenterPosition = 0
# 	start = -1
# 	end = -1
# 	diff = -1
# 	for i in range(2,N): 
# 		iMirror = 2*C-i 
# 		L[i] = 0
# 		diff = R - i 
# 		if diff > 0: 
# 			L[i] = min(L[iMirror], diff) 

# 		while (((i + L[i]) + 1 < N and (i - L[i]) > 0) and (((i + L[i] + 1) % 2 == 0) or (text[((i + L[i] + 1) // 2)] == text[((i - L[i] - 1) // 2)]))): 
# 			L[i]+=1

# 		if L[i] > maxLPSLength:	 # Track maxLPSLength 
# 			maxLPSLength = L[i] 
# 			maxLPSCenterPosition = i 

# 		if i + L[i] > R: 
# 			C = i 
# 			R = i + L[i] 

# 	start = (maxLPSCenterPosition - maxLPSLength) // 2
# 	end = start + maxLPSLength - 1

# 	if(start!=0):
# 		findLongestPalindromicString(text[:end])
# 	else:
# 		return text[:end+1]

for qw in range(int(input())):
	s=input()
	n=len(s)
	if(n==1):
		print(s)
		continue
	else:
		i=0
		j=n-1
		count=0
		while j>i:
			if(s[i]==s[j]):
				count+=1
				i+=1
				j-=1
			else:
				break
		suffix=s[:i]
		prefix=s[j+1:]
		middle=s[i:j+1]

		if(len(middle)>0):
			end=len(middle)
			while True:
				if(middle[:end]==middle[:end][::-1]):
					break
				end-=1

			middle1=middle[::-1]			
			ende=len(middle1)
			while True:
				if(middle1[:ende]==middle1[:ende][::-1]):
					break
				ende-=1
			if(ende>end):
				print(suffix+middle1[:ende]+prefix)
			else:
				print(suffix+middle[:end]+prefix)
		else:
			print(suffix+prefix)



	

# 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)