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 JAVA):

Solution:
import java.util.*;
import java.io.*;
import java.math.*;
 
public class A
{
    static String find_palin_pref(int st, char arr[]){
        int n = arr.length - 1, idx = -1;
        StringBuilder res = new StringBuilder();

        for(int i = st; i <= n - st + 1; i++){
            boolean flag = true;

            for(int j = st, k = i; j <= k; j++, k--)
                flag = flag && (arr[j] == arr[k]);

            if(flag) idx = i;
        }

        if(idx == -1)
            return "";
        for(int i = st; i <= idx; i++) res.append(String.valueOf(arr[i]));
        return res.toString();
    }


    static String find_palin_suff(int st, char arr[]){
        int n = arr.length - 1, idx = -1;
        StringBuilder res = new StringBuilder();

        for(int i = n - st + 1; i >= st; i--){
            boolean flag = true;

            for(int j = i, k = n-st+1; j <= k; j++, k--)
                flag = flag && (arr[j] == arr[k]);

            if(flag) idx = i;
        }

        if(idx == -1)
            return "";
        for(int i = idx; i <= n-st+1; i++) res.append(String.valueOf(arr[i]));
        return res.toString();
    }

    public static void process()throws IOException
    {
        char arr[] = (" "+nln()).toCharArray();
        int n = arr.length - 1;

        int st = 0;
        StringBuilder res = new StringBuilder();

        for(int i = 1, j = n; i <= j; i++, j--){
            if(arr[i] != arr[j]){
                st = i;
                break;
            }
        }

        if(st == 0){ // the string is a palindrome
            for(int i = 1; i <= n; i++ )
                res.append(String.valueOf(arr[i]));
            pn(res);
            return ;
        }

        String pref = find_palin_pref(st, arr);
        String suff = find_palin_suff(st, arr);
        String take = (pref.length() > suff.length()) ? pref : suff;

        for(int i = 1; i < st; i++)
            res.append(String.valueOf(arr[i]));
        res.append(take);
        for(int i = n - st + 2; i <= n; i++)
            res.append(String.valueOf(arr[i]));

        pn(res);
    }
 
    static final long mod=(long)1e9+7l;
    
    static FastReader sc;
    static PrintWriter out;
    public static void main(String[]args)throws IOException
    {
        out = new PrintWriter(System.out);
        sc=new FastReader();
 
        long s = System.currentTimeMillis();
        int t=1;
        t=ni();
        while(t-->0)
            process();
 
        out.flush();
        System.err.println(System.currentTimeMillis()-s+"ms");
    }

    static void pn(Object o){out.println(o);}
    static void p(Object o){out.print(o);}
    static int ni()throws IOException{return Integer.parseInt(sc.next());}
    static long nl()throws IOException{return Long.parseLong(sc.next());}
    static double nd()throws IOException{return Double.parseDouble(sc.next());}
    static String nln()throws IOException{return sc.nextLine();}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
    
    static class FastReader{ 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader(){ 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
  
        String next(){ 
            while (st == null || !st.hasMoreElements()){ 
                try{ st = new StringTokenizer(br.readLine()); } catch (IOException  e){ e.printStackTrace(); } 
            } 
            return st.nextToken(); 
        } 
  
        String nextLine(){ 
            String str = ""; 
            try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } 
            return str; 
        } 
    } 
}