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

public class Codechef
{
    static long fac[] = new long[1000001];
    static void perm()
    {
        fac[0]=1;
        for (int x=1;x<=1000000;x++)
        fac[x]=fac[x-1]*x;
    }
    static long power(long x, long y, long p) 
    { 
          
        // Initialize result 
        long res = 1; 
      
        // Update x if it is more than or 
        // equal to p 
        x = x % p; 
                      
        while (y > 0) 
        { 
              
            // If y is odd, multiply x 
            // with result 
            if (y % 2 == 1) 
                res = (res * x) % p; 
      
            // y must be even now 
            y = y >> 1; // y = y/2 
            x = (x * x) % p; 
        } 
          
        return res; 
    } 
    // Returns n^(-1) mod p 
    static long modInverse(long n, long p) 
    { 
        return power(n, p-2, p); 
    } 
      
    // Returns nCr % p using Fermat's 
    // little theorem. 
    static long nPr(int n, int r, 
                                    long p) 
    { 
          
        // Base case 
        if (r == 0) 
            return 1; 
      
        // Fill factorial array so that we 
        // can find all factorial of r, n 
        // and n-r 
      
        return ((fac[n] % p) * modInverse(fac[n-r], p) 
                                    % p) % p; 
    }
static long mod(String num, long a) 
    { 
          
        // Initialize result 
        long res = 0; 
  
        // One by one process all digits of 'num' 
        for (int i = 0; i < num.length(); i++) 
            res = (res * 10 + (int)num.charAt(i) 
                                     - '0') % a; 
  
        return res; 
    }
    static boolean check (int a)
    {
        String s=Integer.toString(a);
        int f[] = new int[10];
        for (int x=0;x<s.length();x++)
        {
            f[s.charAt(x)-'0']++;
            if (f[s.charAt(x)-'0']>1)
            return (false);
        }
        return (true);
    }
    static long lcm(int[] element_array) 
    { 
        long lcm_of_array_elements = 1; 
        int divisor = 2; 
          
        while (true) { 
            int counter = 0; 
            boolean divisible = false; 
              
            for (int i = 0; i < element_array.length; i++) { 
  
                // lcm_of_array_elements (n1, n2, ... 0) = 0. 
                // For negative number we convert into 
                // positive and calculate lcm_of_array_elements. 
  
                if (element_array[i] == 0) { 
                    return 0; 
                } 
                else if (element_array[i] < 0) { 
                    element_array[i] = element_array[i] * (-1); 
                } 
                if (element_array[i] == 1) { 
                    
                    
                    counter++; 
                } 
  
                // Divide element_array by devisor if complete 
                // division i.e. without remainder then replace 
                // number with quotient; used for find next factor 
                if (element_array[i] % divisor == 0) { 
                    divisible = true; 
                    element_array[i] = element_array[i] / divisor; 
                } 
            } 
  
            // If divisor able to completely divide any number 
            // from array multiply with lcm_of_array_elements 
            // and store into lcm_of_array_elements and continue 
            // to same divisor for next factor finding. 
            // else increment divisor 
            if (divisible) { 
                lcm_of_array_elements = lcm_of_array_elements * divisor; 
            } 
            else { 
                divisor++; 
            } 
  
            // Check if all element_array is 1 indicate  
            // we found all factors and terminate while loop. 
            if (counter == element_array.length) { 
                return lcm_of_array_elements; 
            } 
        } 
    }
    static boolean find(int N)
    {
        String r="";
        int rem=1%N;
        HashMap<Integer, Integer> h = new HashMap<Integer, Integer>();
        while ((rem!=0) && !h.containsKey(rem))
        {
            h.put(rem,r.length());
            rem=rem*10;
            r=r+(rem/N);
            rem=rem%N;
        }
        if (rem==0)
        return (false);
        else
        return (true);
    }
    static String sorts(String inputString) 
    { 
        // convert input string to char array 
        char tempArray[] = inputString.toCharArray(); 
          
        // sort tempArray 
        Arrays.sort(tempArray); 
          
        // return new sorted string 
        return new String(tempArray); 
    }
    static long qrt(double d)
    {
        long k=(long)(d);
        double x=-1+Math.sqrt(1+(8*d));
        x=x/2;
        long w=(long)(Math.ceil(x));
        long sum=w*(w+1);
        sum=sum/2;
        //System.out.println (w+"GDSGDS");
        while (sum%2!=k%2)
        {
            w++;
            sum=w*(w+1);
            sum=sum/2;
        }
        return (w);
    }
    static int knapSack(long W, int wt[], int val[], int n) 
     { 
        // Base Case 
    if (n <=0 || W == 0) 
        return 0; 
    if (wt[n-1] > W) 
       return knapSack(W, wt, val, n-1); 
    else 
    return Math.max( val[n-1] + knapSack(W-wt[n-1], wt, val, n-2), knapSack(W, wt, val, n-1)); 
      } 
    static boolean index(int x, int y, int N, int M)
    {
        if (x>=0 && y>=0 && x<N && y<M)
        return (true);
        else
        return (false);
    }
    static boolean check (String a)
    {
        int len=a.length();
        char z;
        int l=0;
        int r=len-1;int p=0;
        while (l<=r && p==0)
        {
            if (a.charAt(l)==a.charAt(r))
            {l++;r--;}
            else 
            p=1;
        }
        if (p==0)
        return (true);
        else
        return (false);
    }
	public static void main (String[] args) throws java.lang.Exception
	{
	    Scanner in = new Scanner(System.in);
	    int T=in.nextInt();
	    for (int Q=1;Q<=T;Q++)
	    {
	        String a=in.next();
	        int len=a.length();
	        int l=0;
	        int r=len-1;
	        ArrayList<Integer> A[] = new ArrayList[26];
	        char z;
	        for (int x=0;x<len;x++)
	        {
	            z=a.charAt(x);
	            if (A[z-'a']==null)
	            A[z-'a'] = new ArrayList<Integer>();
	            A[z-'a'].add(x);
	        }
	        int p=0;
	        while (l<=r && p==0)
	        {
	            if (a.charAt(l)==a.charAt(r))
	            {
	                l++;
	                r--;
	            }
	            else
	            p=1;
	        }
	        if (p==0)
	        {System.out.println (a);continue;}
	        String ans1="";
	        ans1=a.substring(0,l);
	        String ans2="";
	        ans2=a.substring(r+1,len);
	        //System.out.println (l+" "+r);
	        char z1=a.charAt(l);
	        char z2=a.charAt(r);
	        int l1=-1;
	        int l2=-1;
	        
	        for (int x=0;x<A[z1-'a'].size();x++)
	        {
	           // System.out.println (A[z1-'a'].get(x)+" l");
	            if (A[z1-'a'].get(x)<r && A[z1-'a'].get(x)>=l)
	            if (check(a.substring(l,A[z1-'a'].get(x)+1)))
	            l1=Math.max(l1,A[z1-'a'].get(x)-l+1);
	        }
	        for (int x=0;x<A[z2-'a'].size();x++)
	        {
	           // System.out.println (A[z2-'a'].get(x)+" r");
	            if (A[z2-'a'].get(x)>l && A[z2-'a'].get(x)<=r)
	            if (check(a.substring(A[z2-'a'].get(x),r+1)))
	            l2=Math.max(l2,r-A[z2-'a'].get(x)+1);
	        }
	        //System.out.println (l1+" "+l2);
	        if (l1>l2)
	        {
	            ans1=ans1+a.substring(l,l+l1);
	        }
	        else
	        {
	            ans2=a.substring(r-l2+1,r+1)+ans2;
	        }
	        System.out.println ((ans1+ans2));
	    }
	}
  static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;
 
        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
 
        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
 
        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }
 
        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');
 
            if (neg)
                return -ret;
            return ret;
        }
 
        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }
 
        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
 
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
 
            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }
 
            if (neg)
                return -ret;
            return ret;
        }
 
        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }
 
        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }
 
        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }
}