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.*;
import static java.lang.Math.*;
import static java.lang.System.*;
public class D
{
    public static void main(String[]args){
        InputReader sc = new InputReader(System.in);
        PrintWriter pw =new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int cases=sc.nextInt();
        for(int i=0;i<cases;i++){
        	String s=sc.next();
        	if(s.length()==1){
        	    System.out.println(s);
        	    continue;
        	   }
        	boolean[][]startmax=new boolean[s.length()][s.length()+2];
        	boolean[][]endmax=new boolean[s.length()][s.length()+2];
        	//one core,
        	for(int a=0;a<s.length();a++){
        		for(int b=0;a-b>-1&&a+b<s.length()&&s.charAt(a-b)==s.charAt(a+b);b++){
        			startmax[a-b][1+2*b]=true;
        			endmax[a+b][1+2*b]=true;
        		}
        		
        	}
        	//zero core,
        	for (int a=0; a<s.length(); a++) {
        		for(int b=0;a-b>-1&&a+1+b<s.length()&&s.charAt(a-b)==s.charAt(a+1+b);b++){
        			startmax[a-b][2*(b+1)]=true;
        			endmax[a+b+1][2*(b+1)]=true;
        		}
        		
        	}
        	//mix
        	int max=-1;
        	int start=-1;
        	int mid=-1;
        	boolean front=false;
        	for(int a=0;2*(a+1)<s.length()&&s.charAt(a)==s.charAt(s.length()-1-a);a++){
        		int midmax=0;
        		boolean midfront=false;
        		for(int b=0;2*(a+1)+b<s.length();b++){
        			if(startmax[a+1][b]){
        				midmax=b;
        				midfront=true;
        			}
        			if(endmax[s.length()-1-a-1][b]){
        				midmax=b;
        				midfront=false;
        			}
        		}
        		if(2*(a+1)+midmax>max){
        			max=2*(a+1)+midmax;
        			start=(a+1);
        			mid=midmax;
        			front=midfront;
        		}
        	}
        	int edgemaxfront=-1;
        	int edgemaxend=-1;
        	for(int ii=0;ii<startmax[0].length;ii++){
        	    if(startmax[0][ii])
        	    edgemaxfront=ii;
        	    if(endmax[s.length()-1][ii])
        	    edgemaxend=ii;
        	   }
        	if(edgemaxfront>=max&&edgemaxfront>=edgemaxend){
        	    System.out.println(s.substring(0,edgemaxfront));
        	    continue;
        	   }
        	if(edgemaxend>=max&&edgemaxend>=edgemaxfront){
        	    System.out.println(s.substring(s.length()-edgemaxend,s.length()));
        	    continue;
        	   }
        	if(front){
        		System.out.println(s.substring(0,start)+s.substring(start,start+mid)+s.substring(s.length()-start,s.length()));
        	}else{
        		System.out.println(s.substring(0,start)+s.substring(s.length()-start-mid,s.length()-start)+s.substring(s.length()-start,s.length()));
        	}
        }
        
        pw.flush();
    }
    
    static class InputReader
    {
        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;
        private SpaceCharFilter filter;
        public InputReader(InputStream stream)
        {
                this.stream = stream;
        }
        public int snext()
        {
                if (snumChars == -1)
                        throw new InputMismatchException();
                if (curChar >= snumChars)
                {
                        curChar = 0;
                        try
                        {
                                snumChars = stream.read(buf);
                        } catch (IOException e)
                        {
                                throw new InputMismatchException();
                        }
                        if (snumChars <= 0)
                                return -1;
                }
                return buf[curChar++];
        }
        public int nextInt()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                int sgn = 1;
                if (c == '-')
                {
                        sgn = -1;
                        c = snext();
                }
                int res = 0;
                do
                {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        res *= 10;
                        res += c - '0';
                        c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
        }
        public long nextLong()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                int sgn = 1;
                if (c == '-')
                {
                        sgn = -1;
                        c = snext();
                }
                long res = 0;
                do
                {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        res *= 10;
                        res += c - '0';
                        c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
        }
        public double nextDouble()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                int sgn = 1;
                if (c == '-')
                {
                        sgn = -1;
                        c = snext();
                }
                double res = 0;
                while(c<='9'&&c>='0'){
                    res*=10;
                    res+=c-'0';
                    c=snext();
                }
                if(c=='.'){
                    double decimal=0;
                    long multiplier=1;
                    c=snext();
                    do
                    {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        decimal *= 10;
                        decimal += c - '0';
                        multiplier*=10;
                        c = snext();
                    } while (!isSpaceChar(c));
                    return sgn*(res+decimal/multiplier);
                }else{
                    if(!isSpaceChar(c)){
                        throw new InputMismatchException();
                    }
                    return sgn*res;
                }
        }
        public int[] nextIntArray(int n)
        {
                int a[] = new int[n];
                for (int i = 0; i < n; i++)
                {
                        a[i] = nextInt();
                }
                return a;
        }
        public long[] nextLongArray(int n)
        {
                long a[] = new long[n];
                for (int i = 0; i < n; i++)
                {
                        a[i] = nextLong();
                }
                return a;
        }
        public String next(){return readString();}
        public String readString()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                StringBuilder res = new StringBuilder();
                do
                {
                        res.appendCodePoint(c);
                        c = snext();
                } while (!isSpaceChar(c));
                return res.toString();
        }
        public String nextLine()
        {
                int c = snext();
                StringBuilder res = new StringBuilder();
                do
                {
                        res.appendCodePoint(c);
                        c = snext();
                } while (!isEndOfLine(c));
                if(isEndOfLine(res.charAt(res.length()-1)))
                return res.deleteCharAt(res.length()).toString();
                return res.toString();
        }
        public ArrayList<String> readAll(){
            ArrayList<String>a=new ArrayList<String>();
            try{
                while(true){
                    a.add(nextLine());
                }
            }catch(Exception e){
                
            }
            return a;
        }
        public boolean hasNext(){
            boolean hasnext=true;
            try{
                hasnext= stream.available()!=0;
            }catch(IOException e){
            }
            return hasnext||curChar<snumChars;
        }
        public boolean isSpaceChar(int c)
        {
                if (filter != null)
                        return filter.isSpaceChar(c);
                return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
        private boolean isEndOfLine(int c)
        {
                return c == '\n' || c == '\r' || c == -1;
        }
        public interface SpaceCharFilter
        {
                public boolean isSpaceChar(int ch);
        }
    }
}