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.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
 
public class Main {
 
 
    static StringBuilder s;
 
    public static int solve() {
        StringBuilder kmprev = new StringBuilder(s);
        kmprev = kmprev.reverse();
        String kmp = s + "#" + kmprev;
 
        int lps[] = new int[kmp.length()];
//        System.out.println(kmprev+" "+s);
        for (int i = 1; i < (int) kmp.length(); ++i) {
            int prev_idx = lps[i - 1];
 
            while (prev_idx > 0 && kmp.charAt(i) != kmp.charAt(prev_idx)) {
                prev_idx = lps[prev_idx - 1];
            }
 
            lps[i]=(prev_idx + ((kmp.charAt(i) == kmp.charAt(prev_idx)) ? 1 : 0));
        }
        return lps[lps.length - 1];
    }
 
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        StringBuffer res = new StringBuffer();
        while (t-- > 0) {
            char[] a = br.readLine().toCharArray();
            int n = a.length;
            int mv = 0;
            if (n % 2 == 1) {
                mv = n / 2;
            } else {
                mv = n / 2 - 1;
            }
            int fr = 0, ba = 0, fl = 0;
            for (int i = 0; i <= mv; i++) {
                int f = i, b = n - i - 1;
                if (a[f] != a[b]) {
                    fl = 1;
                    fr = f;
                    ba = b;
                    break;
                }
            }
            if (fl == 1) {
                char forwa[] = new char[ba - fr + 1];
                int c = 0;
                for (int i = fr; i <= ba; i++) {
                    forwa[c] = a[i];
                    c++;
                }
                s = new StringBuilder(new String(forwa));
//                System.out.println(s);
                int fol = solve();
                c = 0;
                char bac[] = new char[ba - fr + 1];
                for (int i = ba; i >= fr; i--) {
                    bac[c] = (a[i]);
                    c++;
                }
                s = new StringBuilder(String.valueOf(bac));
//                System.out.println(s);
                int bol = solve();
                if (fol >= bol) {
                    for (int i = 0; i < fr + fol; i++) {
                        res.append(a[i]);
                    }
                    for (int i = ba + 1; i < n; i++) {
                        res.append(a[i]);
                    }
                } else {
                    for (int i = 0; i < fr; i++) {
                        res.append(a[i]);
                    }
                    for (int i = ba - bol + 1; i < n; i++) {
                        res.append(a[i]);
                    }
                }
                res.append("\n");
            } else {
                res.append(String.valueOf(a)+"\n");
            }
        }
        System.out.println(res);
    }
}