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:
//package com.company;

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    public static class Task {

        public static int[] zFunction(char[] s) {
            int[] z = new int[s.length];
            for (int i = 1, l = 0, r = 0; i < z.length; ++i) {
                if (i <= r)
                    z[i] = Math.min(r - i + 1, z[i - l]);
                while (i + z[i] < z.length && s[z[i]] == s[i + z[i]])
                    ++z[i];
                if (r < i + z[i] - 1) {
                    l = i;
                    r = i + z[i] - 1;
                }
            }
            return z;
        }

        int longestPalinDromePrefix(char[] s) {
            if (s.length == 0) return 0;
            int n = s.length;
            char[] ss = new char[n * 2];
            for (int i = 0; i < n; i++) {
                ss[i] = s[i];
            }
            for (int i = n - 1; i >= 0; i--) {
                ss[n + i] = s[n - 1 - i];
            }
            int[] z = zFunction(ss);
            for (int i = n; i < z.length; i++) {
                if (z[i] + i == z.length) return z[i];
            }
            throw new RuntimeException();
        }

        public void solve(Scanner sc, PrintWriter pw) throws IOException {
            int T = sc.nextInt();
            while (T-- > 0) {
                String s = sc.next();
                int n = s.length();
                int k = 0;
                for (; k < n - 1 - k; k++) {
                    if (s.charAt(k) != s.charAt(n - 1 - k)) break;
                }
                String ts = s.substring(k, n - k);
                int usePre = longestPalinDromePrefix(ts.toCharArray());
                String tsRev = new StringBuilder(ts).reverse().toString();
                int useSuf = longestPalinDromePrefix(tsRev.toCharArray());
                String check = usePre > useSuf ? ts: tsRev;
                int use = Math.max(usePre, useSuf);
                StringBuilder f = new StringBuilder();
                for (int i = 0; i < k; i++) {
                    f.append(s.charAt(i));
                }
                for (int i = 0; i < use; i++) {
                    f.append(check.charAt(i));
                }
                for (int i = 0; i < k; i++) {
                    f.append(s.charAt(k - 1 - i));
                }
                pw.println(f.toString());
            }
        }
    }

    static long TIME_START, TIME_END;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
//        Scanner sc = new Scanner(new FileInputStream("nondec.in"));
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
//        PrintWriter pw = new PrintWriter(new FileOutputStream("nondec.out"));

        Runtime runtime = Runtime.getRuntime();
        long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
        TIME_START = System.currentTimeMillis();
        Task t = new Task();
        t.solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
        pw.close();
        System.err.println("Memory increased: " + (usedMemoryAfter - usedMemoryBefore) / 1000000);
        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

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

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

        public String nextLine() throws IOException {
            return br.readLine();
        }

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

        public boolean ready() throws IOException {
            return br.ready();
        }
    }
}