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 Main
{
	char s[];
	int n;
	long hash[],revhash[];
	static final int p=128;
	private void solve()throws IOException
	{
		s=(" "+nextLine()).toCharArray();
		n=s.length-1;
		hash=new long[n+1];
		revhash=new long[n+1];
		for(int i=1;i<=n;i++)
			hash[i]=((hash[i-1]*p)%mod+s[i])%mod;
		for(int i=1;i<=n;i++)
			revhash[i]=((revhash[i-1]*p)%mod+s[n-i+1])%mod;
		int i;
		for(i=1;i<=n/2;i++)
		{
			if(s[i]==s[n-i+1])
				continue;
			else
				break;
		}
		if(s[i]!=s[n-i+1])
			i--;
		int maxp=max_prefix(i+1,n-i);
		int maxs=max_suffix(i+1,n-i);
		if(maxp>maxs)
		{
			for(int j=1;j<=i;j++)
				out.print(s[j]);
			for(int j=i+1;j<=i+maxp;j++)
				out.print(s[j]);
			for(int j=Math.max(i+1,n-i+1);j<=n;j++)
				out.print(s[j]);
		}
		else
		{
			for(int j=1;j<=i;j++)
				out.print(s[j]);
			for(int j=n-i-maxs+1;j<=n-i;j++)
				out.print(s[j]);
			for(int j=Math.max(i+1,n-i+1);j<=n;j++)
				out.print(s[j]);
		}
		out.println();
	}
	int max_prefix(int l,int r)
	{
		int ret=0;
		for(int i=l;i<=r;i++)
		{
			if(check_palindrome(l,i))
				ret=i-l+1;
		}
		return ret;
	}
	int max_suffix(int l,int r)
	{
		int ret=0;
		for(int i=r;i>=l;i--)
		{
			if(check_palindrome(i,r))
				ret=r-i+1;
		}
		return ret;
	}
	boolean check_palindrome(int l,int r)
	{
		int size=r-l+1;
		if(size==1)
			return true;
		else if(size%2==0)
			return hash(l,l+size/2-1)==revhash(l+size/2,r);
		else
			return hash(l,l+size/2-1)==revhash(r-size/2+1,r);
	}
	long hash(int l,int r)
	{
		return (((hash[r]-(hash[l-1]*modpow(p,r-l+1))%mod+mod)%mod));
	}
	long revhash(int l,int r)
	{
		int l1=n-r+1;
		int r1=n-l+1;
		return (((revhash[r1]-(revhash[l1-1]*modpow(p,r1-l1+1))%mod+mod)%mod));
	}
	static final long mod=(int)(1e9+7);
	long modinv(long x){
	    return modpow(x,mod-2);
	}
	long modpow(long a,long b){
	    long ret=1;
	    while(b!=0)
	    {
	        if(b%2==1)
	            ret=(ret*a)%mod;
	        a=(a*a)%mod;
	        b=b/2;
	    }
	    return ret;
	} 

	 
	///////////////////////////////////////////////////////////

	public void run()throws IOException
	{
		br=new BufferedReader(new InputStreamReader(System.in));
		st=null;
		out=new PrintWriter(System.out);

		int t=nextInt();
		while(t-->0)
			solve();

		br.close();
		out.close();
	}
	public static void main(String args[])throws IOException{
		new Main().run();
	}
	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	String nextToken()throws IOException{
		while(st==null || !st.hasMoreTokens())
		st=new StringTokenizer(br.readLine());
		return st.nextToken();
	}
	String nextLine()throws IOException{
		return br.readLine();
	}
	int nextInt()throws IOException{
		return Integer.parseInt(nextToken());
	}
	long nextLong()throws IOException{
		return Long.parseLong(nextToken());
	}
	double nextDouble()throws IOException{
		return Double.parseDouble(nextToken());
	}
}