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 m {
	public static int[][] palin;
	public static String s;
	public static boolean isPalin(int start, int end)
	{
		int val;
		if(start>=end)
			return true;
		if(palin[start][end]!=-1)
			return palin[start][end]==1;

		boolean match = s.charAt(start)==s.charAt(end);
		val = (match&&isPalin(start+1,end-1)) ? 1 : 0;
		palin[start][end]=val;

		return palin[start][end]==1;
	}
	public static String getLPOnEnds(int start, int end)
	{
		if(start>end)
			return "";
		int ansp1=start,ansp2=start;
		int ansln = 1;
		for(int i=start;i<=end;i++)
		{
			if(isPalin(start,i) && (i-start+1)>ansln)
			{
				ansp1 = start; ansp2 = i;
				ansln = (i-start+1);
			}
		}
		for(int i=start;i<=end;i++)
		{
			if(isPalin(i,end) && (end-i+1)>ansln)
			{
				ansp1 = i; ansp2 = end;
				ansln = (end-i+1);
			}
		}
		return s.substring(ansp1,ansp2+1);
	}
	public static void main(String[] args)throws IOException {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
		int t = in.nextInt();
		for(int z=0;z<t;z++)
		{
			s=in.next();
			int n = s.length();
			palin = new int[n][n];
			for(int i=0;i<n;i++)
				Arrays.fill(palin[i],-1);
			int p1=-1,p2=n;
			int ans=0;
			String ansstr="";
			// out.println("val:"+getPalin(1,3)[0]);
			do
			{
				String pstr = getLPOnEnds(p1+1,p2-1);
				int lcl = (2*(p1+1))+pstr.length();
				if(lcl>ans)
				{
					ans=lcl;
					ansstr=pstr;
				}
				p1++; p2--;
			}
			while(p1<n && p2>=0 && p1<p2 && s.charAt(p1)==s.charAt(p2));
			int ansends = (ans - ansstr.length())/2;
			for(int i=0;i<ansends;i++)
			{
				out.print(s.charAt(i));
			}
			out.print(ansstr);
			for(int i=n-ansends;i<n;i++)
			{
				out.print(s.charAt(i));
			}
			out.println();
		}

        out.close();
	}
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }

}