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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        D2PrefixSuffixPalindromeHardVersion solver = new D2PrefixSuffixPalindromeHardVersion();
        solver.solve(1, in, out);
        out.close();
    }

    static class D2PrefixSuffixPalindromeHardVersion {
        long mod = 1000000009;
        int MAXN = 100005;
        PrintWriter out;
        InputReader in;
        long[] pow = new long[MAXN];
        long[] inv_pow = new long[MAXN];

        void pre() {
            int i = 0;
            pow[0] = 1;
            inv_pow[0] = 1;
            for (i = 1; i < MAXN; i++) {
                pow[i] = (pow[i - 1] * 31) % mod;
                inv_pow[i] = power(pow[i], mod - 2);
            }
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            int t = ni();
            pre();
            StringBuffer ans = new StringBuffer();
            while (t-- > 0) {
                String st = n();
                int n = st.length();
                char[] arr = st.toCharArray();
                int lo = 0, hi = n - 1;
                while (lo <= hi) {
                    if (arr[lo] == arr[hi]) {
                        lo++;
                        hi--;
                    } else
                        break;
                }

                if (lo > hi) {
                    ans.append(st).append("\n");
                    continue;
                }
                StringBuilder str = new StringBuilder();
                int i = 0;
                for (i = lo; i <= hi; i++)
                    str.append(arr[i]);
                String st1 = str.toString();
                Rabin_Karp rk1 = new Rabin_Karp(st1);
                Rabin_Karp rk2 = new Rabin_Karp(str.reverse().toString());
                rk1.pre();
                //pn(str);
                rk2.pre();
                //pn(rk1.hash[3]);
                //pn(rk2.hash[3]);
                //pn(str);
                //pn(rk1.hash_val(1, 3));
                int len = st1.length();
                int max1 = 0, max2 = 0;
                for (i = 0; i < len; i++) {
                    long hash1 = rk1.hash_val(1, i + 1);
                    long hash2 = rk2.hash_val(len - i, len);
                    //pn(hash1 +" "+hash2);
                    if (hash1 == hash2)
                        max1 = Math.max(max1, i + 1);
                }
                for (i = 0; i < len; i++) {
                    long hash1 = rk1.hash_val(len - i, len);
                    long hash2 = rk2.hash_val(1, i + 1);
                    if (hash1 == hash2)
                        max2 = Math.max(max2, i + 1);
                }
                //pn(max1 +" "+max2);
                //pn(lo +" "+hi);
                for (i = 0; i < lo; i++)
                    ans.append(arr[i]);
                if (max1 > max2) {
                    for (i = 0; i < max1; i++)
                        ans.append(arr[lo + i]);
                } else {
                    for (i = max2 - 1; i >= 0; i--)
                        ans.append(arr[hi - i]);
                }
                for (i = hi + 1; i < n; i++)
                    ans.append(arr[i]);
                ans.append("\n");
            }
            pn(ans);
        }

        String n() {
            return in.next();
        }

        int ni() {
            return in.nextInt();
        }

        void pn(Object o) {
            out.println(o);
        }

        long power(long x, long y) {
            if (y == 0) {
                return 1;
            }
            long p = power(x, y / 2) % mod;
            p = (p * p) % mod;
            return (y % 2 == 0) ? p : (x * p) % mod;
        }

        class Rabin_Karp {
            long[] hash = new long[MAXN];
            String st;

            Rabin_Karp(String st) {
                this.st = st;
            }

            void pre() {
                hash[0] = 0;
                int i = 0;
                for (i = 1; i <= st.length(); i++)
                    hash[i] = (hash[i - 1] + ((st.charAt(i - 1) - 'a' + 1) * pow[i - 1]) % mod) % mod;
            }

            long hash_val(int l, int r) {
                long hashes = hash[r] - hash[l - 1] + mod;
                hashes %= mod;
                hashes *= inv_pow[l - 1];
                hashes %= mod;
                return hashes;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

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

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}