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 CF1326D {
    final static int[] MOD =  {1000000007, 2117566807,1000000007,1000000037};
    final static int[] BASE = {1572872831, 1971536491,1000003,31};
    public static void main(String ...args) {
        Reader in = new Reader();
        Writer out = new Writer();
        int test = in.nextInt();
        while(test--> 0) {

            String s = in.next();
            int sz = s.length();
            int i = 0;
            int j = sz-1;
            String ans = "";
            while(i<j) {
                if(s.charAt(i)==s.charAt(j)) {
                    ans+= s.charAt(i);
                    i++;
                    j--;
                } else {
                    break;
                }
            }
            if(i<=j) {
                String left = s.substring(i, j+1);
               // System.out.println(left);
                StringHash s1 = new StringHash();
                StringHash s2 = new StringHash();
                s1.GenerateFH(left,Long.valueOf(BASE[0]),Long.valueOf(MOD[0]));
                s2.GenerateRH(left,Long.valueOf(BASE[0]),Long.valueOf(MOD[0]));
                String temp = "";
                for(i= left.length()-1; i>=0;i--) {

                    if(s1.Fhash(0, i).equals(s2.Rhash(0, i))) {
                       // System.out.println(s1.Fhash(0,i) +" " + s2.Rhash(0,i));
                        temp = left.substring(0,i+1);
                        break;
                    }
                }
                for(i= 0;i<left.length();i++) {
                    if(s1.Fhash(i,left.length()-1) == s2.Rhash(i,left.length()-1)) {
                        if(left.substring(i,left.length()).length() > temp.length()) {
                            temp = left.substring(i,left.length());
                        }
                        break;
                    }
                }
                String temp1 = ans;

                ans+=temp;
                ans+= new StringBuilder(temp1).reverse().toString();
            } else {
                String temp1 = ans;
                ans+= new StringBuilder(temp1).reverse().toString();
            }
            out.println(ans);

        }
        out.flush();
    }

}

class StringHash{
    Long base, mod; int len;
    Long ar[], p[];

    public void GenerateFH(String s, Long b, Long m) {
        len = s.length();
        p = new Long[len];
        ar = new Long[len];
        p[0] = 1L;

        base = b;
        mod = m;
        for(int i = 1; i<len;i++) {
            p[i] = p[i-1] * base;
            p[i]%=m;
        }

        long h = 0L;
        for(int i = 0;i<len;i++) {
            h = (h*base) + s.charAt(i);
            h%=mod;
            ar[i] = h;
        }
    }

    public Long Fhash(int x,int y){
        Long h=ar[y];
        if(x>0){
            h=(h-p[y-x+1]*ar[x-1])%mod;
            if(h<0) h+=mod;
        }
        return h;
    }

    public void GenerateRH(String s, Long b, Long m) {
        len = s.length();
        p = new Long[len+1];
        ar = new Long[len+1];
        p[0] = Long.valueOf(1);
        base = b;
        mod = m;

        for(int i = 1; i<len;i++) {
            p[i] = p[i-1] * base;
            p[i]%=m;
        }

        Long h = 0L;

        for(int i = len-1;i>=0;i--) {
            h = (h*base) + s.charAt(i);
            h%=mod;
            ar[i] = h;

        }
    }

    public long Rhash(int x, int y){
        Long h=ar[x];
        if(y<len-1){
            h=(h-p[y-x+1] *ar[y+1])%mod;
            if(h<0) h+=mod;
        }
        return h;
    }
}

class Reader {
    private StringTokenizer a;
    private BufferedReader b;
    Reader () {
        a = null;
        try {
            b = new BufferedReader (new InputStreamReader (System.in)); // for file IO, replace this with new FileReader ("in.txt")
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public String next () {
        while(a == null || !a.hasMoreTokens()) {
            try {
                a = new StringTokenizer (b.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return a.nextToken();
    }
    public int nextInt() {
        return Integer.parseInt(this.next());
    }
    public long nextLong () {
        return Long.parseLong(this.next());
    }
    public double nextDouble () {
        return Double.parseDouble(this.next());
    }
    public String nextLine() {
        try {
            return b.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
class Writer {
    private PrintWriter a;
    private StringBuffer b;
    Writer () {
        try {
            a = new PrintWriter (System.out); // for file IO, replace this with new FileWriter ("out.txt")
        } catch (Exception e) {
            e.printStackTrace();
        }
        b = new StringBuffer ("");
    }
    public void print (Object s) {
        b.append(s);
    }
    public void println(Object s) {
        b.append(s).append('\n');
    }
    public void flush () {
        a.print(b);
        a.flush();
        a.close();
    }
}