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.util.*;
import java.io.*;
public class EdA {
	static long[] mods = {1000000007, 998244353, 1000000009, 101741582076581L};
	static long mod = mods[3];
	public static MyScanner sc;
    public static PrintWriter out;
	public static void main(String[] omkar) throws Exception{
		// TODO Auto-generated method stub
 		sc = new MyScanner();
 		out = new PrintWriter(System.out);

//	 		String input1 = bf.readLine().trim();
//	 		String input2 = bf.readLine().trim();
//			COMPARING INTEGER OBJECTS U DO DOT EQUALS NOT ==
 		int t = sc.nextInt();
 		for(int i = 0;i<t;i++){
 			char[] input = sc.next().toCharArray();
 			int start = 0;
 			int end = input.length-1;
 			while(start < input.length && input[start] == input[end]){
 				start++;
 				end--;
 			}
 			
 			if (end < start){
 				//the whole string
 				for(char j : input)
 					out.print(j);
 				out.println();
 			}
 			else{
 				int longestprefix = 1;
 				long base = 273;
 				long power = 273;
 				long forwardhash = input[start]-'a';
 				long backwardhash = input[start]-'a';
 				for(int j = start+1;j<=end;j++){
 					forwardhash+=power*(input[j]-'a');
 					forwardhash%=mod;
 					backwardhash*=base;
 					backwardhash%=mod;
 					backwardhash+=(input[j]-'a');
 					backwardhash%=mod;
 					if (forwardhash == backwardhash)
 						longestprefix = j-start+1;
 					power*=base;
 					power%=mod;
 				}
 				
 				int longestsuffix = 1;
 				long power1 = 273;
 				long forwardhash1 = input[end]-'a';
 				long backwardhash1 = input[end]-'a';
 				for(int j = end-1;j>=start;j--){
 					forwardhash1+=power1*(input[j]-'a');
 					forwardhash1%=mod;
 					backwardhash1*=base;
 					backwardhash1%=mod;
 					backwardhash1+=(input[j]-'a');
 					backwardhash1%=mod;
 					if (forwardhash1 == backwardhash1)
 						longestsuffix = end-j+1;
 					power1*=base;
 					power1%=mod;
 				}
 				
 				if (longestprefix > longestsuffix){
 					for(int j = 0;j<start;j++){
 						out.print(input[j]);
 					}
 					for(int j = start;j<start+longestprefix;j++)
 						out.print(input[j]);
 					for(int j = end+1;j<input.length;j++){
 						out.print(input[j]);
 					}
 				}
 				else{
 					for(int j = 0;j<start;j++){
 						out.print(input[j]);
 					}
 					for(int j = end-longestsuffix+1;j<=end;j++)
 						out.print(input[j]);
 					for(int j = end+1;j<input.length;j++){
 						out.print(input[j]);
 					}
 				}
 				out.println();
 				
 			}
 			
 		}
//	 			out.print(array[j] + " ");
//	 		}
//	 		out.println();
 		
 		out.println();
	 		
 		out.close();	
 	}
	public static void sort(int[] array){
		ArrayList<Integer> copy = new ArrayList<Integer>();
		for (int i : array)
			copy.add(i);
		Collections.sort(copy);
		for(int i = 0;i<array.length;i++)
			array[i] = copy.get(i);
	}
	static String[] readArrayString(int n){
		String[] array = new String[n];
		for(int j =0 ;j<n;j++)
			array[j] = sc.next();
		return array;
	}
	static int[] readArrayInt(int n){
    	int[] array = new int[n];
    	for(int j = 0;j<n;j++)
    		array[j] = sc.nextInt();
    	return array;
    }
	static int[] readArrayInt1(int n){
		int[] array = new int[n+1];
		for(int j = 1;j<=n;j++){
			array[j] = sc.nextInt();
		}
		return array;
	}
	static long[] readArrayLong(int n){
		long[] array = new long[n];
		for(int j =0 ;j<n;j++)
			array[j] = sc.nextLong();
		return array;
	}
	static double[] readArrayDouble(int n){
		double[] array = new double[n];
		for(int j =0 ;j<n;j++)
			array[j] = sc.nextDouble();
		return array;
	}
	static int minIndex(int[] array){
		int minValue = Integer.MAX_VALUE;
		int minIndex = -1;
		for(int j = 0;j<array.length;j++){
			if (array[j] < minValue){
				minValue = array[j];
				minIndex = j;
			}
		}
		return minIndex;
	}
	static int minIndex(long[] array){
		long minValue = Long.MAX_VALUE;
		int minIndex = -1;
		for(int j = 0;j<array.length;j++){
			if (array[j] < minValue){
				minValue = array[j];
				minIndex = j;
			}
		}
		return minIndex;
	}
	static int minIndex(double[] array){
		double minValue = Double.MAX_VALUE;
		int minIndex = -1;
		for(int j = 0;j<array.length;j++){
			if (array[j] < minValue){
				minValue = array[j];
				minIndex = j;
			}
		}
		return minIndex;
	}
	static long power(long x, long y){
		if (y == 0)
			return 1;
		if (y%2 == 1)
			return (x*power(x, y-1))%mod;
		return power((x*x)%mod, y/2)%mod;
	}
	static void verdict(boolean a){
        out.println(a ? "YES" : "NO");
    }
    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;
        }
        
    }	
}

//StringJoiner sj = new StringJoiner(" "); 
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is