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.*;
import java.util.*;
 
public class codeforces2 { // in case I want to work on more than 1 problem at a time
	public static PrintWriter out;
	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;
		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
	// The next two void functions are shuffle functions on an array.
	// This way, quick sort encounters the worst case scenario with low probability.
	// A reason for doing this is if we don't a primitive data array to an object array.
	// For safety reasons, it would be preferable to use object arrays unless doing so expends a lot of time.
	public static void shuffleLongArray(long[] arr) {
		int n = arr.length;
		Random rnd = new Random();
        for (int i=0; i < n; i++) {
        	long tmp = arr[i];
        	int randomPos = i + rnd.nextInt(n-i);
        	arr[i] = arr[randomPos];
        	arr[randomPos] = tmp;
        }
	}
	public static void shuffleIntArray(int[] arr) {
		int n = arr.length;
		Random rnd = new Random();
        for (int i=0; i < n; i++) {
        	int tmp = arr[i];
        	int randomPos = i + rnd.nextInt(n-i);
        	arr[i] = arr[randomPos];
        	arr[randomPos] = tmp;
        }
	}
	public static long inf = 1000000007L;
	public static void main (String[] args) throws IOException {
    	MyScanner sc = new MyScanner();
    	out = new PrintWriter(new BufferedOutputStream(System.out));
    	double err = Math.pow(10, -4);
    	// Write solution here.
    	int t = sc.nextInt();
    	for (int asap=0; asap < t; ++asap) {
    		String s = sc.next(); int r = s.length();
    		// We find the first moment where they are not equal
    		int fork = 0;
    		for (int i=0; 2*i < r; ++i) {
    			if (s.charAt(i)==s.charAt(r-1-i)) ++fork;
    			else break;
    		}
    		// If 2*fork >= r, then the whole string is a palindrome.
    		if (2*fork >= r) {
    			out.println(s); continue;
    		}
    		else if (r-2*fork == 2) {
    			out.println(s.substring(0, fork+1) + s.substring(r-fork, r)); continue;
    		}
    		int spoon = 2*fork; // longest palindrome from simply the ends without any consideration of the middle
    		// We now search for palindromes within, knowing that there are at least 2 characters that get left behind.
    		// The leftmost and rightmost characters here will not be equal.
    		int[][] dp = new int[r-2*fork][r-2*fork]; // We only look at i <= j. We need to add fork to each index.
    		for (int i=0; i < r-2*fork; ++i) {
    			dp[i][i] = 1;
    		}
    		int rubber = 1; int left = fork+1; int right = fork+1; // middle palindromes only, so no fork or r-fork-1
    		for (int i=0; i < r-2*fork-1; ++i) {
    			if (s.charAt(i+fork)==s.charAt(i+1+fork)) {
    				dp[i][i+1] = 1;
    				if (i+fork != fork && i+fork+1 != r-fork-1) {
    					rubber = 2; left = i+fork; right = i+fork+1;
    				}
    			}
    		}
    		for (int d=2; d < r-2*fork; ++d) {
    			for (int i=0; i < r-d-2*fork; ++i) {
    				if (s.charAt(i+fork)==s.charAt(i+d+fork) && dp[i+1][i+d-1]==1) {
    					dp[i][i+d] = 1;
    					if (i+fork != fork && i+fork+d != r-fork-1 && d+1 > rubber) {
    						rubber = d+1; left = i+fork; right = i+fork+d;
    					}
    				}
    			}
    		}
    		/*
    		for (int i=0; i < r-2*fork; ++i) {
    			for (int j=0; j < r-2*fork; ++j) {
    				out.print(dp[i][j] + " ");
    			}
    			out.println();
    		}
    		*/
    		int lean = 0; // left
    		int bless = 1; int va = 0; int vb = 0;
    		for (int j=r-2*fork-1; j >= 0; --j) {
    			if (dp[0][j] == 1) {
    				lean = 1; bless = j+1; va = fork; vb = fork+j; break;
    			}
    		}
    		for (int i=0; i < r-2*fork; ++i) {
    			if (dp[i][r-2*fork-1] == 1) {
    				if (r-2*fork-i > bless) {
    					lean = 0; bless = r-2*fork-i; va = fork+i; vb = r-fork-1; break;
    				}
    			}
    		}
    		// out.println("Rubber: " + rubber + "; Left: " + left + "; Right: " + right + "; Bless: " + bless + "; VA: " + va + "; VB: " + vb);
    		// if (rubber >= 2*fork + bless) out.println(s.substring(left, right+1));
    		if (true) {
    			if (lean == 1) {
    				out.println(s.substring(0, vb+1) + s.substring(r-fork, r));
    			}
    			else {
    				out.println(s.substring(0, fork) + s.substring(va, r));
    			}
    		}
    	}
    	out.close();
	}
	public static long power (long a, long b) {
		if (b==0L) return 1L;
		String g = Long.toBinaryString(b);
		long d = a;
		for (int i=1; i < g.length(); i++) {
			d = (d*d) % inf;
			if (g.charAt(i)=='1') d = (d*a) % inf;
		}
		return d;
	}
}