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 io
import os

from random import randint

DEBUG = False


def bisect_f(f, lo, hi):
    if DEBUG:
        assert f(lo) and not f(hi)
    while hi - lo > 1:
        mid = (lo + hi) // 2
        if f(mid):
            lo = mid
        else:
            hi = mid

        if DEBUG:
            assert f(lo) and not f(hi)

    if DEBUG:
        assert hi - lo == 1
        assert f(lo) and not f(hi)
    return lo


cachedPow = {}
PRIME = 31
MOD = 2 ** 61 - 1


class RabinKarpRollingHash:
    def __init__(self, s, PRIME=31, MOD=1000000007):
        n = len(s)

        # Build polynomial hash where prefix[k] is hash of s[0:k]
        # prefix[i + 1] = (
        #   s[0]     * (PRIME **  i     ) +
        #   s[1]     * (PRIME ** (i - 1)) +
        #   s[2]     * (PRIME ** (i - 2)) +
        #   ...
        #   s[i - 2] * (PRIME **  2     ) +
        #   s[i - 1] * (PRIME **  1     ) +
        #   s[i]     * (PRIME **  0     )
        # )
        prefix = [0] * (n + 1)
        for i in range(n):
            prefix[i + 1] = (prefix[i] * PRIME + ord(s[i]) - ord("a")) % MOD

        # Exponentiating is expensive since bigints: `PRIME ** k`
        # We could use python's mod pow to keep numbers small: `pow(PRIME, k, MOD)`
        # But since we know we need all the powers from 0 to n,
        # can just precompute them instead
        if n not in cachedPow:
            cachedPow[0] = 1
            for i in range(n):
                cachedPow[i + 1] = (cachedPow[i] * PRIME) % MOD

        self.PRIME = PRIME
        self.MOD = MOD
        self.prefix = prefix

    def getHash(self, i, j):
        # Inclusive i, exclusive j
        return (self.prefix[j] - self.prefix[i] * cachedPow[j - i]) % self.MOD


def solve(s):
    n = len(s)
    forward = RabinKarpRollingHash(s, PRIME, MOD)
    backward = RabinKarpRollingHash(s[::-1], PRIME, MOD)

    def revEqual(i1, j1, i2, j2):
        # Returns whether s[i1:j1] == s[i2:j2][::-1]
        ret = forward.getHash(i1, j1) == backward.getHash(n - j2, n - i2)
        if DEBUG:
            assert 0 <= i1 <= j1 <= n
            assert 0 <= i2 <= j2 <= n
            check = s[i1:j1] == s[i2:j2][::-1]
            if ret != check:
                print(forward.getHash(i1, j1), backward.getHash(n - j2, n - i2))
                print(s[i1:j1], s[i2:j2][::-1])
            assert ret == check
        return ret

    if revEqual(0, n, 0, n):
        return s

    # Find min value of a and b
    minAB = 0
    for i in range(n):
        if i < n - 1 - i and s[i] == s[n - 1 - i]:
            minAB = i
        else:
            break

    best = (0, 0, 0)  # (a + b, a, b)
    for center in range(minAB, n - minAB):
        for parity in [0, 1]:

            def isPal(r):
                i = center - r
                j = center + r + parity
                if not (0 <= i <= j <= n):
                    return False
                return revEqual(i, j, i, j)

            r = bisect_f(isPal, 0, n)
            if DEBUG:
                for rr in range(0, n):
                    i = center - rr
                    j = center + rr + parity
                    if i < 0 or j > n or s[i:j] != s[i:j][::-1]:
                        assert r == rr - 1
                        break
            i = center - r
            j = center + r + parity
            if DEBUG:
                assert revEqual(i, j, i, j)

            if revEqual(0, i, n - i, n) and j <= n - i:
                a = j
                b = i
                best = max(best, (a + b, a, b))
                if DEBUG:
                    ans = s[:a] + s[n - b :]
                    assert a <= n - b
                    assert ans == ans[::-1]
            if revEqual(0, n - j, j, n) and n - j <= i:
                a = n - j
                b = n - i
                best = max(best, (a + b, a, b))
                if DEBUG:
                    ans = s[:a] + s[n - b :]
                    assert a <= n - b
                    assert ans == ans[::-1]

    if DEBUG:
        best2 = 0
        for a in range(n + 1):
            for b in range(n - a + 1):
                w = s[:a] + s[n - b :]
                if w == w[::-1]:
                    best2 = max(best2, a + b)
        assert best[0] == best2

    l, a, b = best
    return s[:a] + s[n - b :]


if False:
    for i in range(100):
        N = 500
        P = randint(0, 250)
        mid = [chr(ord("a") + randint(0, 26)) for j in range(N - 2 * P)]
        prefix = [chr(ord("a") + randint(0, 26)) for j in range(P)]
        solve(prefix + mid + prefix[::-1])
    exit()

if False:
    solve([chr(ord("a") + randint(0, 26)) for j in range(10 ** 6)])

if __name__ == "__main__":
    input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
    answers = []
    T = int(input())
    for t in range(T):
        S = input().decode().rstrip()
        answers.append(solve(S))
    print("\n".join(answers))