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.*;
import java.math.*;

public class D {


//credit fabrizio3 @ leetcode

public static String manacherize(String s) {
		StringBuilder ms = new StringBuilder("^");
		for(int i=0;i<s.length();i++) {
			ms.append("#"+s.charAt(i));
		}
		ms.append("#$");
		return ms.toString();
	}
	public static String longestPalindromeStartingFrom0(String s) {
		if(s.length()==0) return "";
		String T = manacherize(s);
		int[] P = new int[T.length()];
		int C = 0;
		int R = 0;
		for(int i=1;i<T.length()-1;i++) {
			int imirror = C-(i-C);
			P[i] = (R > i) ? Math.min(R-i, P[imirror]) : 0;
			while(T.charAt(i+1+P[i])==T.charAt(i-1-P[i])) {
				P[i]++;
			}
			if(i+P[i]>R) {
				C = i;
				R = i + P[i];
			}
		}
		int maxLen = 0;
		int maxCenter = 0;
		for(int i=1;i<P.length-1;i++) {
			if(P[i]>maxLen && i-P[i]==1) {
				maxLen = P[i];
				maxCenter = i;
			}
		}
		int start = (maxCenter-1-maxLen)/2;
		return s.substring(start,start+maxLen);
	}


	public void realMain() throws Exception {

		BufferedReader fin = new BufferedReader(new InputStreamReader(System.in), 1000000);

		String in = fin.readLine();

		String[] ar = in.split(" ");


//System.err.println(findLongestPalindrome("aabacd"));
//System.err.println(longestPalindromeStartingFrom0("aabacd"));


		int T = Integer.parseInt(ar[0]);
		StringBuilder sb = new StringBuilder("");
	for(int t = 0; t < T; t++) {
		in = fin.readLine();

		//match all that i can
		int match = 0;

		int n = in.length();

		char[] c = in.toCharArray();

		for(int i = 0; i < n; i++) {
			if(c[i] == c[n - i - 1]) {
				match++;
			} else {
				break;
			}
		}


		if(match > n - match) {
			sb.append(in.substring(0, match));
			sb.append('\n');
			continue;
		}

		int poss1 = 0;

		//System.err.println("first try: " + in.substring(match, n - match));

		String add1 = longestPalindromeStartingFrom0(in.substring(match, n - match));

		poss1 = add1.length();

		//System.err.println("add1:" + add1);

		int poss2 = 0;
		String add2 = longestPalindromeStartingFrom0( new StringBuilder( in.substring(match, n - match) ).reverse().toString() );
		poss2 = add2.length();
		
		//System.err.println("add2:" + add2);


		sb.append(in.substring(0, match));
		if(poss1 >= poss2) {
			sb.append(add1);
		} else {
			sb.append(add2);
		}
		sb.append( in.substring(n - match) );
		sb.append('\n');

		//System.out.println("abcdfdcecba".substring(3, 8));
	}

	System.out.println(sb);


	}


	public static void main(String[] args) throws Exception {
		D d = new D();
		d.realMain();
	}
}