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.*;
import java.util.stream.Collectors;
import java.util.stream.*;


public class Main { 
    
    public static void main(String[] args) throws java.lang.Exception { 
        //Reader pm =new Reader(); 
        Scanner pm = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int t = pm.nextInt();
        while(t-- > 0){
            String s = pm.next();
            char[] c = s.toCharArray();
            int n = s.length();
            if(n == 1){
                System.out.println(s);
                continue;
            }
            if(isPalindrome(s)){
                System.out.println(s);
                continue;
            }
            //ArrayList<Character> al = new ArrayList<>(c);
            StringBuffer sb = new StringBuffer(s);
            int o = 0;
            int e = n;
            for(int i=0;i<n;i++){
                if(c[i] == c[n-i-1]){
                    o++;
                    e--;
                } else
                    break;
            }
            String pre = "";
            String sub = s.substring(o,e);
            String subC = sub;
            //System.out.println(sub);
            
            StringBuffer sbb = new StringBuffer(sub);
            int l = sub.length();
            if(isPalindrome(sub)){
                pre = sub;
            }
            for(int i=l-1;i>=0;i--){
                sbb.deleteCharAt(sbb.length()-1);
                subC = sbb.toString();
                if(isPalindrome(subC)){
                    pre = subC;
                    break;
                }
            }
            //System.out.println(pre);
            
            String suf = "";
            sbb = new StringBuffer(sub);
            subC = sub;
            if(isPalindrome(sub)){
                suf = sub;
            }
            for(int i=0;i<l;i++){
                sbb.deleteCharAt(0);
                subC = sbb.toString();
                if(isPalindrome(subC)){
                    suf = subC;
                    break;
                }
            }
            //System.out.println(suf);
            
            String mid = "";
            if(pre.length() > suf.length())
                mid = pre;
            else
                mid = suf;
            
            if(pre.length() == 0 && suf.length() == 0){
                mid = String.valueOf(s.charAt(o+1));
            }
            
            StringBuffer ans = new StringBuffer();
            ans.append(s.substring(0,o));
            ans.append(mid);
            ans.append(s.substring(e,n));
            System.out.println(ans);
        }
        
    }
    
    static String reverse(String input) { 
		char[] a = input.toCharArray(); 
		int l, r = 0; 
		r = a.length - 1; 
		for (l = 0; l < r; l++, r--) 
		{ 
			// Swap values of l and r 
			char temp = a[l]; 
			a[l] = a[r]; 
			a[r] = temp; 
		} 
		return String.valueOf(a); 
	} 

	// Function to check whether 
	// the string is a palindrome 
	static boolean isPalindrome(String r) { 
		String p = r; 

		// reverse the string to 
		// compare with the 
		// original string 
		p = reverse(p); 
		
		// check if both are same 
		return (r.equals(p)); 
	} 
    
    
    private static int binarySearchPM(int[] arr, int key){
        int n = arr.length;
        int mid = -1;
        int begin = 0,end=n;
        while(begin <= end){
            mid = (begin+end) / 2;
            if(mid == n){
                return n;
            }
            if(key < arr[mid]){
                end = mid-1;
            } else if(key > arr[mid]){
                begin = mid+1;
            } else {
                return mid;
            }
        }
        //System.out.println(begin+" "+end);
        return -begin; //expected Index
    }
    
    private static List<Integer> setToList(Set<Integer> s){
        List<Integer> al = s.stream().collect(Collectors.toList());
        return al;
    }
    
    private static List<Integer> mapValToList(HashMap<Integer,Integer> map){
        return map.values().stream().collect(Collectors.toList());
    }
    
    private static List<Integer> mapKeyToList(HashMap<Integer,Integer> map){
        return map.keySet().stream().collect(Collectors.toList());
    }
    
    private static HashMap<Integer, Integer> sortByKey(HashMap<Integer,Integer> map){
        return map
                .entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue())
                .collect(Collectors.toMap(
                    Map.Entry::getKey, 
                    Map.Entry::getValue, 
                    (oldValue, newValue) -> oldValue, LinkedHashMap::new));
    }
    
    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(); 
        } 
    }
}