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:
/**
 * Created by Aminul on 3/20/2020.
 */

import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

import static java.lang.Math.max;

public class D__2 {
    static final Random random = new Random();

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
        int test = Integer.parseInt(in.readLine());
        for (int t = 1; t <= test; t++) {
            String str = in.readLine();
            String rev = reverseString(str);
            Hashing strHashing = new Hashing(str.toCharArray());
            Hashing revHashing = new Hashing(rev.toCharArray());
            int n = str.length();
            int start = 0, end = n - 1;
            while (start < end && str.charAt(start) == str.charAt(end)) {
                start++;
                end--;
            }
            if (start >= end) {
                pw.println(str);
                continue;
            }
            String ans = "";
            for (int i = n; i >= 0; i--) {
                if (start + start + i > n) continue;
                if (isPalindrome(strHashing, revHashing, start, start + i - 1)) {
                    String curr = str.substring(0, start + i);
                    curr += str.substring(end + 1, n);
                    if (curr.length() > ans.length()) {
                        ans = curr;
                    }
                    break;
                }
            }

            for (int i = n; i >= 0; i--) {
                if (start + start + i > n) continue;
                if (isPalindrome(strHashing, revHashing, end - i + 1, end)) {
                    String curr = str.substring(0, start);
                    curr += str.substring(end - i + 1, n);
                    if (curr.length() > ans.length()) {
                        ans = curr;
                    }
                    break;
                }
            }

            pw.println(ans);
        }

        pw.close();
    }

    static boolean isPalindrome(Hashing strHashing, Hashing revHashing, int l, int r) {
        return strHashing.getHash(l, r) == revHashing.revHash(l, r);
    }

    static String reverseString(String sq) {
        return new StringBuilder(sq).reverse().toString();
    }

    static void debug(Object... obj) {
        System.err.println(Arrays.deepToString(obj));
    }

    static int randomPrime(int n) {
        return BigInteger.valueOf(random.nextInt(n) + (n / 2)).nextProbablePrime().intValue();
    }

    static class Hashing {
        static final long mod1 = 1000000097L, mod2 = (long) 1e9 + 9;
        static final long multiplier1 = randomPrime((int) 2e5), multiplier2 = randomPrime((int) 2e5);
        static final long invMultiplier1 = BigInteger.valueOf(multiplier1).modInverse(BigInteger.valueOf(mod1)).longValue();
        static final long invMultiplier2 = BigInteger.valueOf(multiplier2).modInverse(BigInteger.valueOf(mod2)).longValue();

        int n;
        long[] hash1, hash2;
        long[] inv1, inv2;

        Hashing(char[] s) {
            build_Hash(s);
        }

        void build_Hash(char[] s) {
            n = s.length;
            hash1 = new long[n + 1];
            hash2 = new long[n + 1];
            inv1 = new long[n + 1];
            inv2 = new long[n + 1];
            inv1[0] = 1;
            inv2[0] = 1;

            long p1 = 1;
            long p2 = 1;
            for (int i = 0; i < n; i++) {
                hash1[i + 1] = (hash1[i] + s[i] * p1) % mod1;
                p1 = (p1 * multiplier1) % mod1;
                inv1[i + 1] = inv1[i] * invMultiplier1 % mod1;
                hash2[i + 1] = (hash2[i] + s[i] * p2) % mod2;
                p2 = (p2 * multiplier2) % mod2;
                inv2[i + 1] = inv2[i] * invMultiplier2 % mod2;
            }
        }

        long getHash(int i, int j) { //0-based
            return getHash_2(i, j - i + 1);
        }

        long getHash_2(int i, int len) { //i is 0  based indexed
            return (((hash1[i + len] - hash1[i] + mod1) * inv1[i] % mod1) << 32)
                    + (hash2[i + len] - hash2[i] + mod2) * inv2[i] % mod2;
        }

        long revHash(int i, int j) { ////0-based
            return getHash(n - j - 1, n - i - 1);
        }
    }
}