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 sys
from array import array  # noqa: F401


def input():
    return sys.stdin.buffer.readline().decode('utf-8')


class RollingHash(object):
    __slots__ = ['hash1', 'hash2']

    from random import randint

    N = 10**6 + 100
    BASE = randint(10**8, 10**9)
    MOD1, MOD2 = 10**9 + 7, 10**9 + 9
    BASE1, BASE2 = array('i', [1]) * N, array('i', [1]) * N
    for i in range(1, N):
        BASE1[i] = BASE1[i - 1] * BASE % MOD1
        BASE2[i] = BASE2[i - 1] * BASE % MOD2

    def __init__(self, source: list):
        self.hash1 = hash1 = array('i', [0] + source)
        self.hash2 = hash2 = array('i', [0] + source)

        for i in range(1, len(source) + 1):
            hash1[i] = (hash1[i] + hash1[i - 1] * self.BASE) % self.MOD1
            hash2[i] = (hash2[i] + hash2[i - 1] * self.BASE) % self.MOD2

    def get(self, l: int, r: int):
        return (
            (self.hash1[r] - self.hash1[l] * self.BASE1[r - l]) % self.MOD1,
            (self.hash2[r] - self.hash2[l] * self.BASE2[r - l]) % self.MOD2
        )


t = int(input())
ans = [''] * t
for ti in range(t):
    s = input().rstrip()
    n = len(s)
    l, r = 0, n - 1
    while s[l] == s[r] and l < r:
        l += 1
        r -= 1
    if l >= r:
        ans[ti] = s
        continue

    rh = RollingHash([ord(c) for c in s[l:r + 1]])
    rh_rev = RollingHash([ord(c) for c in reversed(s[l:r + 1])])
    m = r - l + 1
    mid_l, mid_r = 0, 1

    for i in range(1, m + 1):
        if rh.get(0, i) == rh_rev.get(m - i, m):
            mid_l, mid_r = 0, i
        elif rh_rev.get(0, i) == rh.get(m - i, m):
            mid_l, mid_r = m - i, m

    ans[ti] = s[:l] + s[l + mid_l:l + mid_r] + s[r + 1:]

sys.stdout.buffer.write(('\n'.join(ans) + '\n').encode('utf-8'))