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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.StringTokenizer;


/*

 */
/**
 *
 * @author Andy Phan
 */
public class d {

    public static void main(String[] args) {
        FS in = new FS(System.in);
        PrintWriter out = new PrintWriter(System.out);
        
        int t = in.nextInt();
        while(t-->0) {
            String s = in.next();
            char[] arr = new char[s.length()];
            char[] rev = new char[s.length()];
            for(int i = 0; i < s.length(); i++) arr[i] = rev[s.length()-i-1] = s.charAt(i);
            int[] lps = Manacher.StartLPSArray(new String(arr));
            int[] rps = Manacher.StartLPSArray(new String(rev));
            int ind = -1;
            int side = -1;
            int res = -1;
            for(int i = 0; 2*i < arr.length; i++) {
                if(i != 0 && arr[i-1] != rev[i-1]) break;
                int tmp = 2*i;
                if(lps[i]+2*i > res) {
                    ind = i;
                    side = 0;
                    res = lps[i]+2*i;
                }
                if(rps[i]+2*i > res) {
                    ind = i;
                    side = 1;
                    res = rps[i]+2*i;
                }
            }
            for(int i = 0; i < ind; i++) out.print(arr[i]);
            if(side == 0) {
                for(int i = ind; i < lps[ind]+ind; i++) out.print(arr[i]);
            } else {
                for(int i = ind; i < rps[ind]+ind; i++) out.print(rev[i]);
            }
            for(int i = ind-1; i >= 0; i--) out.print(arr[i]);
            out.println();
        }
        
        out.close();
    }

    static class FS {

        BufferedReader in;
        StringTokenizer token;

        public FS(InputStream str) {
            in = new BufferedReader(new InputStreamReader(str));
        }

        public String next() {
            if (token == null || !token.hasMoreElements()) {
                try {
                    token = new StringTokenizer(in.readLine());
                } catch (IOException ex) {
                }
                return next();
            }
            return token.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    static class Manacher {

        static char BOUNDARY_CHARACTER = '|';
        // Longest Palindromic Substring array. //%
        // Stores for each boundary point in the string the longest //%
        // palindrome where that index is the center. //%

        static int[] LPSArray(String s) {
            char[] s2 = addBoundaries(s.toCharArray());
            int[] p = new int[s2.length];
            int c = 0, r = 0, m = 0, n = 0;
            for (int i = 1; i < s2.length; i++) {
                if (i > r) {
                    p[i] = 0;
                    m = i - 1;
                    n = i + 1;
                } else {
                    int i2 = c * 2 - i;
                    if (p[i2] < (r - i)) {
                        p[i] = p[i2];
                        m = -1;
                    } else {
                        p[i] = r - i;
                        n = r + 1;
                        m = i * 2 - n;
                    }
                }
                while (m >= 0 && n < s2.length && s2[m] == s2[n]) {
                    p[i]++;
                    m--;
                    n++;
                }
                if ((i + p[i]) > r) {
                    c = i;
                    r = i + p[i];
                }
            }
            return p;
        }

        private static char[] addBoundaries(char[] cs) {
            if (cs == null || cs.length == 0) {
                return new char[]{BOUNDARY_CHARACTER, BOUNDARY_CHARACTER};
            }
            char[] cs2 = new char[cs.length * 2 + 1];
            for (int i = 0; i < (cs2.length - 1); i = i + 2) {
                cs2[i] = BOUNDARY_CHARACTER;
                cs2[i + 1] = cs[i / 2];
            }
            cs2[cs2.length - 1] = BOUNDARY_CHARACTER;
            return cs2;
        }
        // Get the longest palindromic substring //%

        static String getLPS(String s) {
            int[] lps = LPSArray(s);
            int max = 0;
            for (int i = 0; i <= s.length() * 2; ++i) {
                if (lps[i] > lps[max]) {
                    max = i;
                }
            }
            int lower = (max - lps[max]) / 2;
            int higher = lower + lps[max];
            return s.substring(lower, higher);
        }
        // Get the LPS array where each index holds the longest //%
        // palindrome starting at that index in the string. //%

        static int[] StartLPSArray(String s) {
            int[] lps = LPSArray(s), ret = new int[s.length()];
            for (int i = 0; i <= lps.length/2; ++i) {
                if (lps[i] > 0) {
                    int lower = (i - lps[i]) / 2;
                    ret[lower] = Math.max(ret[lower], lps[i]);
                }
            }
            for (int i = 1; i < s.length(); ++i) {
                ret[i] = Math.max(ret[i], ret[i - 1] - 2);
            }
            return ret;
        }
    }

}