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:
//package com.example.programming;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;


public class CodeforcesProblems {


    static String[] sTakeInputs;
    static int sInputPos = 0;
    static BufferedReader sBufferedReader;

    static int[] scanIntArray() throws Exception{
        String[] inputs = sBufferedReader.readLine().trim().split(" ");
        int[] arr = new int[inputs.length];
        for(int i = 0; i<inputs.length; i++) arr[i] = Integer.parseInt(inputs[i]);
        return arr;
    }

    static long[] scanLongArray() throws Exception{
        String[] inputs = sBufferedReader.readLine().trim().split(" ");
        long[] arr = new long[inputs.length];
        for(int i = 0; i<inputs.length; i++) arr[i] = Long.parseLong(inputs[i]);
        return arr;
    }

    static int scanInt() throws Exception{
        return Integer.parseInt(sBufferedReader.readLine().trim());
    }

    static long scanLong() throws Exception{
        return Long.parseLong(sBufferedReader.readLine().trim());
    }

    static int nextInt() throws Exception {
        if(sInputPos == 0) sTakeInputs = sBufferedReader.readLine().trim().split(" ");
        if(sTakeInputs.length <= sInputPos) {
            sTakeInputs = sBufferedReader.readLine().trim().split(" ");
            sInputPos = 0;
        }
        return Integer.parseInt(sTakeInputs[sInputPos++]);
    }

    static long nextLong() throws Exception {
        if(sInputPos == 0) sTakeInputs = sBufferedReader.readLine().trim().split(" ");
        if(sTakeInputs.length <= sInputPos) {
            sTakeInputs = sBufferedReader.readLine().trim().split(" ");
            sInputPos = 0;
        }
        return Long.parseLong(sTakeInputs[sInputPos++]);
    }

    static String nextString() throws Exception {
        if(sInputPos == 0) sTakeInputs = sBufferedReader.readLine().trim().split(" ");
        if(sTakeInputs.length <= sInputPos) {
            sTakeInputs = sBufferedReader.readLine().trim().split(" ");
            sInputPos = 0;
        }
        return sTakeInputs[sInputPos++];
    }

    static int gcd(int a, int b) {
        if(a%b == 0) return b;
        return gcd(b, a%b);
    }

    static long gcd(long a, long b) {
        if(a%b == 0) return b;
        return gcd(b, a%b);
    }

    static ArrayList<Integer> getPrimes(boolean[] checker) {
        checker[0] = checker[1] = true;
        for(int i = 2; i*i<=checker.length; i++) {
            for(int j = 2; i*j<checker.length; j++) checker[i*j] = true;
        }
        ArrayList<Integer> primes = new ArrayList<>();
        for(int i = 2; i<checker.length; i++) {
            if(!checker[i]) primes.add(i);
        }
        return primes;
    }

    static int getDigitSum(long x) {
        int sum = 0;
        do {
            sum += x%10;
            x/=10;
        }while (x != 0);
        return sum;
    }

    static String getPalindromicSubstring(String str) {
        StringBuilder sb = new StringBuilder();
        sb.append("@#");
        for(int i = 0; i<str.length(); i++) sb.append(str.charAt(i)).append('#');
        sb.append('*');
        int[] lps = new int[sb.length()];
        int[] track = new int[lps.length];
        int center = 0;
        for(int i = 2; i<sb.length()-1; i++) {
            if(center+lps[center]>=i) {
                lps[i] = lps[2*center-i]<center+lps[center]-i ? lps[2*center-i]:center+lps[center]-i;
            } else {
                lps[i] = 0;
            }
            while (sb.charAt(i-lps[i]-1) == sb.charAt(i+lps[i]+1)) lps[i]++;
            if(lps[center]+center < lps[i]+i) center = i;
            if(track[i-lps[i]]<lps[i]) track[i-lps[i]] = lps[i];
            if( track[i+lps[i]]<lps[i]) track[i+lps[i]] = lps[i];
        }
        /*for(int i = 0; i<lps.length; i++) {
            System.out.print(lps[i]+" ");
        }
        System.out.println();
        for(int i = 0; i<track.length; i++) {
            System.out.print(track[i]+" ");
        }
        System.out.println();*/
        if(track[1] == str.length()) {
            return str;
        }
        String ans;
        if(track[1] >= track[sb.length()-2]) {
            ans = str.substring(0, track[1]);
        } else {
            ans = str.substring(str.length()-track[sb.length()-2], str.length());
        }
        //System.out.println(ans);
        return ans;
    }

    public static void main (String[] args) throws Exception {
        sBufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int test = nextInt();
        for(int t = 0; t<test; t++) {
            String str = nextString();
            String ans = getPalindromicSubstring(str);
            if(ans.length() == str.length()) {
                System.out.println(ans); continue;
            }
            StringBuilder temp = new StringBuilder();
            int idx = 0;
            while(str.charAt(idx) == str.charAt(str.length()-idx-1)) {
                temp.append(str.charAt(idx));
                idx++;
            }
            String last = temp.toString();
            String mid = getPalindromicSubstring(str.substring(idx, str.length()-idx));
            temp.append(mid);
            for(int i = last.length()-1; i>=0; i--) temp.append(last.charAt(i));
            if(ans.length()<temp.length()) System.out.println(temp);
            else System.out.println(ans);
        }
    }
}