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:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
# import time,random,resource

# sys.setrecursionlimit(10**6)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def IF(c, t, f): return t if c else f
def YES(c): return IF(c, "YES", "NO")
def Yes(c): return IF(c, "Yes", "No")


class ModInt():
    def __init__(self, n):
        self.n = n

    def __add__(self, x):
        x = ModInt.xn(x)
        return ModInt((self.n+x) % mod)

    def __sub__(self, x):
        x = ModInt.xn(x)
        return ModInt((self.n-x) % mod)

    def __mul__(self, x):
        x = ModInt.xn(x)
        return ModInt((self.n*x) % mod)

    def __truediv__(self, x):
        x = ModInt.xn(x)
        return ModInt(self.n * pow(x, mod-2, mod) % mod)

    @classmethod
    def xn(cls, x):
        if isinstance(x, ModInt):
            return x.n
        return x

    def __str__(self):
        return str(self.n)

def M(n): return ModInt(n)

class RollingHash():
    def __init__(self, s):
        self.N = n = len(s)
        a = [ord(c) for c in s]
        self.A1 = a1 = [M(a[0])]
        self.A2 = a2 = [M(a[0])]
        self.A3 = a3 = [M(a[-1])]
        self.A4 = a4 = [M(a[-1])]
        for c in a[1:]:
            a1.append(a1[-1] * 997 + c)
            a2.append(a2[-1] * 991 + c)
        for c in a[::-1][1:]:
            a3.append(a3[-1] * 997 + c)
            a4.append(a4[-1] * 991 + c)

    def get(self, l, r):
        if l == 0:
            return (self.A1[r].n, self.A2[r].n)
        t1 = (self.A1[r] - self.A1[l-1] * pow(997, r-l+1, mod)).n
        t2 = (self.A2[r] - self.A2[l-1] * pow(991, r-l+1, mod)).n
        return (t1, t2)

    def rget(self, l, r):
        l,r = self.N-r-1,self.N-l-1
        if l == 0:
            return (self.A3[r].n, self.A3[r].n)
        t3 = (self.A3[r] - self.A3[l-1] * pow(997, r-l+1, mod)).n
        t4 = (self.A4[r] - self.A4[l-1] * pow(991, r-l+1, mod)).n
        return (t3, t4)

    def getr(self, l, r):
        l,r = self.N-r-1,self.N-l-1
        return self.get(l,r)

    def rgetr(self, l, r):
        l,r = self.N-r-1,self.N-l-1
        return self.rget(l,r)


def main():
    t = I()

    rr = []
    for _ in range(t):
        s = S()

        sl = len(s)
        l = 0
        r = sl-1
        for i in range(sl//2):
            if s[l] == s[r]:
                l += 1
                r -= 1

        if l >= r:
            rr.append(s)
            continue

        t = s[l:r+1]
        tl = len(t)
        rh = RollingHash(t)
        m = (1, 'L')
        for i in range(tl//2):
            k = rh.get(0, i)
            if (i+1) * 2 < tl and rh.rget(i+2, (i+1)*2) == k:
                j = ((i+1) * 2 + 1, 'L')
                m = max(m, j)
            elif rh.rget(i+1, (i+1)*2-1) == k:
                j = ((i+1) * 2, 'L')
                m = max(m, j)

        # print(t,m)

        for i in range(tl//2):
            k = rh.getr(0, i)
            if (i+1) * 2 < tl and rh.rgetr(i+2, (i+1)*2) == k:
                j = ((i+1) * 2 + 1, 'R')
                m = max(m, j)
            elif rh.rgetr(i+1, (i+1)*2-1) == k:
                j = ((i+1) * 2, 'R')
                m = max(m, j)

        # print(t,m)

        if m[1] == 'L':
            rr.append(s[:l] + t[:m[0]] + s[r+1:])
        else:
            rr.append(s[:l] + t[-m[0]:] + s[r+1:])

    return JA(rr, "\n")


print(main())