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:
/*
     D    A    R    K    L      _                 _         O    R    D    D    A
         K                    / |                 | \        L    O    R         D
        A         R         _ / |                 | \ _       K    L         O    R
  D         A    R    K     _ / |         _       | \ _        L    O    R    D    D
      R    K    L       _ / _   |       _ | \\    |   _ \       O    R    D    D
     R         K        _ / _   |       _ | \ \   |   _ \        L    O    R
    D    A    R     _ / _ / |   /     /         \ \   | \ _ \ _        K    L    O
   D    D           _ / _ / |   /     /          \ \   | \ _ \ _             A
       K        _ / _ /     | /     /     _       \ \ |     \ _ \ _      L    O    R
 D    A        _ / _ /      | /     /     _       \ \ |      \ _ \ _      R         K
            _ / _ /         | |     |   |   \ (O) / | |         \ _ \ _         O
    D      _ / _ /          | |     |   |    \   /  | |          \ _ \ _         D
   A      / _ /     |   |   | \     |   \ _   \|/   / |   |   |     \ _ \
  K      / _ /      |   |   | \     |   \ _    V    / |   |   |      \ _ \         L
        / /         |   |   |   \ _ /       \ _ _ /   |   |   |         \ \
       / /          |   |   |                         |   | | |          \ \
      / /       |   | | \     \       ROWECHEN      /     / | |   |       \ \
     / /    _ _ |   | | \     \        ZHONG        /     / | |   | _ _    \ \
    / / _ /   \ | | | |   \ / \ \                 / / \ /   | | | | /   \ _ \ \
   / / _ /    \ | | | |       \ \                 / /       | | | | /    \ _ \ \
  / / /         \ \ \ \ /       /                 \       \ / / / /         \ \ \
 \ / /          \ \ \ \ /       /                 \       \ / / / /          \ \ /
  \|/             \|/         |         |         |           \|/             \|/
  L         O     \|/         |         |   |     |           \|/        R    D
 A    R    K    L             / _ |     |   | _     \      O    R    D    D    A    R
     L    O         R         / _ |     |   | _     \       D    D         A    R
    L    O    R    D        / / / _     | _ | | \ _   \      D    A    R         K
   O    R    D              / / / _ |   | _ | | \ _   \       D    A    R    K    L
  R    D    D    A    R     | / / | | / | | \ / | | \ |        K    L    O    R    D
 A         R         K      | / / | | / | | \ / | | \ |              L    O         R
     D    A         R         /   \ / | | | / | | / \ /     K    L         O    R
         D    D         A     /   \ / | | | / | | / \/            R    K    L    O
             R         D    D     / | / \ |   \ /        A    R    K    L    O    R
       D    A    R    K    L      / | / \ |    \/              O    R    D    D
 R    K    L    O    R    D       \ /     |      D    A    R         K    L    O    R
          D    A    R             \/      |            K    L    O    R    D    D

*/
//TEMPLATE V2
	import java.io.*;
	import java.util.*;
	import java.math.*;

    public class Main {

    private static int Z(char[] x, int n) {
        int[] Z = new int[n];
        int L = 0, R = 0;
        int res=0;
        for(int i = 1; i < n; ++i) {
            if (i > R) {
                L = R = i;
                while (R < n && x[R - L] == x[R]) R++;
                Z[i] = R - L;
                R--;
            } else {
                int k = i - L;
                if (Z[k] < R - i + 1) Z[i] = Z[k];
                else {
                    L = i;
                    while (R < n && x[R - L] == x[R]) R++;
                    Z[i] = R - L;
                    R--;
                }
            }
            if(Z[i]==n-i){
                res=n-i;
                break;
            }
        }
        return res;
    }
        //Solution goes below: ------------------------------------
    public static void solution() throws IOException{
        int t = nextInt();
        for(int test=0;test<t;test++){
            char[] s= nextLine().toCharArray();
            int match=0;
            for(;match<s.length/2&&s[match]==s[s.length-1-match];match++);
            int n = s.length*2-4*match+1;
            int h = s.length-2*match;
            char[] x = new char[n];
            char[] y = new char[n];
            for(int j=match;j<s.length-match;j++){
                int k = j-match;
                x[k] = s[j]; x[n-1-k] = s[j];
                y[h-1-k] = s[j]; y[h+1+k] = s[j];
            }
            x[h] = '$';
            y[h] = '$';
            int xmax = Z(x, n);
            int ymax = Z(y, n);
            for(int i=0;i<match;i++) print(s[i]);
            if(xmax>=ymax) for (int i = 0; i < xmax; i++) print(x[i]);
            else for (int i = 0; i < ymax; i++) print(y[i]);
            for(int i=match-1;i>=0;i--) print(s[i]);
            println();
        }
    }

    //Solution goes above: ------------------------------------
    //IO
    public static BufferedReader br;
    public static StringTokenizer st;
    public static BufferedWriter bw;
    public static void main(String[] args) throws IOException{
        br = new BufferedReader(new InputStreamReader(System.in));
        bw = new BufferedWriter(new OutputStreamWriter(System.out));
        solution();
        bw.close();//Flushes too.
    }

    public static String nextToken() {
        while (st == null || !st.hasMoreElements()) try {
            st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return st.nextToken();
    }

    public static String nextLine() {
        st = null;
        try {
            return br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int nextInt() {
        return Integer.parseInt(nextToken());
    }
    public static void println() throws IOException{
        bw.newLine();
    }

    public static void print(Object s) throws IOException{
        bw.write(s.toString());
    }
}