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.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter pw = new PrintWriter(System.out);
    static int MOD = 1000000007;

    public static void main(String[] args) throws IOException {
        Main m = new Main();
        m.solve();
        m.close();
    }

    void close() throws IOException {
        pw.flush();
        pw.close();
        br.close();
    }

    int readInt() throws IOException {
        return Integer.parseInt(br.readLine());
    }

    long readLong() throws IOException {
        return Long.parseLong(br.readLine());
    }

    int[] readIntLine() throws IOException {
        String[] tokens = br.readLine().split(" ");
        int[] A = new int[tokens.length];
        for (int i = 0; i < A.length; i++)
            A[i] = Integer.parseInt(tokens[i]);
        return A;
    }

    long[] readLongLine() throws IOException {
        String[] tokens = br.readLine().split(" ");
        long[] A = new long[tokens.length];
        for (int i = 0; i < A.length; i++)
            A[i] = Long.parseLong(tokens[i]);
        return A;
    }

    void solve() throws IOException {
        int t = readInt();
        for (int ti = 0; ti < t; ti++) {
            String str = br.readLine();
            char[] s = str.toCharArray();

            boolean[][] isPalindrome = new boolean[s.length][s.length];
            for (int i = 0; i < s.length; i++) isPalindrome[i][i] = true;
            for (int i = 0; i < s.length - 1; i++) if (s[i] == s[i+1]) isPalindrome[i][i+1] = true;
            for (int len = 3; len <= s.length; len++) {
                for (int i = 0; i + len - 1 < s.length; i++) {
                    isPalindrome[i][i+len-1] = s[i] == s[i+len-1] && isPalindrome[i+1][i+len-2];
                }
            }

            int l = 0;
            int r = s.length - 1;
            StringBuilder prefix = new StringBuilder();
            StringBuilder suffix = new StringBuilder();
            while (l < r && s[l] == s[r]) {
                prefix.append(s[l]);
                suffix.append(s[r]);
                l++;
                r--;
            }
            if (l == r) {
                suffix.reverse();
                pw.print(prefix);
                pw.print(s[l]);
                pw.print(suffix);
                pw.println();
                continue;
            }
            suffix.reverse();
            String mid = "";
            if (l < r) {
                int bestL = l;
                for (int i = l; i <= r; i++) {
                    if (isPalindrome[l][i]) {
                        bestL = i;
                    }
                }
                int bestR = r;
                for (int i = r; i >= l; i--) {
                    if (isPalindrome[i][r]) {
                        bestR = i;
                    }
                }
                if (bestL - l + 1 >= r - bestR + 1) {
                    mid = str.substring(l, bestL + 1);
                } else {
                    mid = str.substring(bestR, r + 1);
                }
            }
            pw.print(prefix);
            pw.print(mid);
            pw.print(suffix);
            pw.println();
        }
    }
}