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:
t=int(input())
for i in range(t):
    s=input()
    n=len(s)
    beg=0
    end=n-1
    while beg<end and s[beg]==s[end]:
        beg+=1
        end-=1
    pali=0
    end2=end
    prob='a'
    probpos=n
    probpassed=1
    posgood=1
    while pali==0:
        while end2>beg and s[end2]!=s[beg] or probpassed==0 or posgood==0:
            end2-=1
            if s[end2]==prob:
                probpassed=1
            if s[end2]==s[beg]:
                if probpos>end2:
                    posgood=1
                else:
                    posgood=0
                    if s[probpos]==s[end2-probpos+beg]:
                        posgood=1
        pali=1
        end3=end2
        beg2=beg
        while beg2<end3 and pali==1:
            if s[end3]==s[beg2]:
                beg2+=1
                end3-=1
            else:
                pali=0
                prob=s[end3]
                probpassed=0
                probpos=beg2
    length1=end2-beg+1
    pali=0
    beg2=beg
    prob='a'
    probpos=0
    probpassed=1
    posgood=1
    while pali==0:
        while beg2<end and s[beg2]!=s[end] or probpassed==0 or posgood==0:
            beg2+=1
            if s[beg2]==prob:
                probpassed=1
            if s[beg2]==s[end]:
                if probpos<beg2:
                    posgood=1
                else:
                    posgood=0
                    if s[probpos]==s[beg2-probpos+end]:
                        posgood=1
            
        pali=1
        beg3=beg2
        end2=end
        while beg3<end2 and pali==1:
            if s[end2]==s[beg3]:
                beg3+=1
                end2-=1
            else:
                pali=0
                prob=s[beg3]
                probpassed=0
                probpos=end2
    length2=end-beg2+1  
    string=s[0:beg]
    if length1>length2:
        string+=s[beg:beg+length1]
    else:
        string+=s[end-length2+1:end+1]
    string+=s[end+1:]
    print(string)