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:
# SUBMIT SOURCE # https://gist.github.com/raikuma/6109943d493d3ba825ba995bae70b90c
import sys
# sys.setrecursionlimit(10**6)
from pprint import pprint
if not 'DEBUG' in globals(): readline = sys.stdin.readline
def read(map_func): return map_func(readline().rstrip())
def reads(map_func): return list(map(map_func, readline().rstrip().split()))
def readn(map_func, n): return [read(map_func) for _ in range(n)]
def readint(): return read(int)
def readints(): return reads(int)
def readcol(n): return readn(int, n)
def readmat(n): return [readints() for _ in range(n)]
def readmap(n): return readn(list, n)

def makemat(n, m, v): return [[v]*m for _ in range(n)]
def zeromat(n, m=None): m=m if m else n; return makemat(n, m, 0)
def listmat(n, m=None): m=m if m else n; return [[[] for _ in range(m)] for _ in range(n)]
def crosslist(y, x, n=None, m=None): return [(p,q) for (p,q) in [(y,x+1),(y-1,x),(y,x-1),(y+1, x)] if (n==None or 0<=p<n)and(m==None or 0<=q<m)]
def roundlist(y, x, n=None, m=None): return [(p,q) for (p,q) in [(y,x+1),(y-1,x+1),(y-1,x),(y-1,x-1),(y,x-1),(y+1,x-1),(y+1, x),(y+1,x+1)] if (n==None or 0<=p<n)and(m==None or 0<=q<m)]

def plog(obj): pprint(obj) if 'DEBUG' in globals() else None
def log(msg, label=None): print((label+': ' if label else '')+str(msg)) if 'DEBUG' in globals() else None
def _logmat(mat, label=None): fmt='{:'+str(max(max(len(str(s)) for s in m) for m in mat))+'d}'; f=lambda row: '['+' '.join(fmt.format(x) for x in row)+']'; [log('['+f(row),label) if i == 0 else log((' '*(len(label)+3) if label else ' ')+f(row)+']',None) if i == len(mat)-1 else log((' '*(len(label)+3) if label else ' ')+f(row),None) for i, row in enumerate(mat)]
def logmat(mat, label=None): _logmat(mat, label) if 'DEBUG' in globals() else None
# endregion BOJ

import math

# LOGIC HERE #
def LeftLongestPalindrom(s):
    L = zeromat(len(s))
    for i in range(0, len(s)):
        L[i][i] = 1
    for k in range(1, len(s)):
        i = 0
        j = i + k
        while j < len(s):
            if s[i] == s[j]:
                L[i][j] = L[i+1][j-1] + 2
            else:
                L[i][j] = max(L[i][j-1], L[i+1][j])
            i += 1
            j += 1
    l = 0
    for j in range(0, len(s)):
        if L[0][j] == j + 1:
            l = max(l, j)
    r = 0
    for i in range(0, len(s)):
        if L[-i-1][-1] == i + 1:
            r = max(r, i)
    if l >= r:
        return s[:l+1]
    return s[-r-1:]
for _ in range(1, readint()+1):
    s = readline()[:-1]
    i, j = 0, len(s)-1
    while i < j and s[i] == s[j]:
        i += 1
        j -= 1
    # log(s[i:j+1], 's')
    t = LeftLongestPalindrom(s[i:j+1])
    # log(t, 't')
    print(s[:i] + t + s[j+1:])