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 javafx.util.Pair;

            import javax.swing.*;
            import java.awt.desktop.SystemSleepEvent;
            import java.util.*;
            import java.io.*;
            //Captain on duty!

            public class Main {

                public static boolean checkPalindrome(String s)
                {
                    // reverse the given String
                    String reverse = new StringBuffer(s).reverse().toString();

                    // check whether the string is palindrome or not
                    if (s.equals(reverse))
                        return true;

                    else
                        return false;
                }

               public static boolean palin(int l, int r, char[] c) {
                    while (l <= r) {
                        if (c[l] != c[r]) return false;
                        l++;
                        r--;
                    }
                    return true;
                }


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

                    return gcd(b, a%b);
                }

                public static long hcf(long a, long b)
                {
                    long t;
                    while(b != 0)
                    {
                        t = b;
                        b = a%b;
                        a = t;
                    }
                    return a;
                }

                public static boolean isPrime(int n)
                {
                    if (n <= 1)
                        return false;

                    // Check from 2 to n-1
                    for (int i = 2; i < n; i++)
                        if (n % i == 0)
                            return false;

                    return true;
                }

                public static String reverse(String str)
                {
                    String str1="";
                    for(int i=0;i<str.length();i++)
                    {
                        str1 = str1 + str.charAt(str.length()-i-1);
                    }
                    return str1;
                }

                public static class FastReader {
                    BufferedReader br;
                    StringTokenizer st;

                    public FastReader() {
                        br = new BufferedReader(new InputStreamReader(System.in));
                    }

                    String nextLine() {
                        String str = "";
                        try {
                            str = br.readLine();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return str;
                    }

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

                    long nextLong() {
                        return Long.parseLong(next());
                    }

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

                    String next() {
                        while (st == null || !st.hasMoreElements()) {
                            try {
                                st = new StringTokenizer(br.readLine());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        return st.nextToken();
                    }


                }


                public static void main(String[] args) {
                    FastReader s = new FastReader();
                    int t=s.nextInt();
                    while(t>0) {
                        char[] c = s.next().toCharArray();
                        int n = c.length;
                        int l = 0;
                        int r = n - 1;
                        LinkedList<Character> f = new LinkedList<>(), rev = new LinkedList<>();
                        while (l <= r && c[l] == c[r]) {
                            if (l != r) {
                                f.add(c[l]);
                                rev.addFirst(c[r]);
                            } else
                                f.add(c[l]);
                            l++;
                            r--;
                        }

                        int bl = -1;
                        int br = -2;
                        int mx = 0;
                        if (l <= r) {
                            for (int i = l; i <= r; i++) {
                                if (i - l + 1 > mx && palin(l, i, c)) {
                                    bl = l;
                                    br = i;
                                    mx = i - l +1;
                                }
                            }

                            for (int i = r; i >= l; i--) {
                                if (r - i + 1 > mx && palin(i, r, c)) {
                                    bl = i;
                                    br = r;
                                    mx = r - i + 1;
                                }
                            }
                        }

                        for (int i = bl; i <= br; i++)
                            f.add(c[i]);
                        for (int i = 0; i < f.size(); i++)
                            System.out.print(f.get(i));
                        for (int i = 0; i < rev.size(); i++)
                            System.out.print(rev.get(i));

                        System.out.println();

                        t--;
                    }

                }



            }