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.*;

 public class Task{
// taking inputs
static BufferedReader s1;
static BufferedWriter out;
static String read() throws IOException{String line="";while(line.length()==0){line=s1.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
static void assign(){s1=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));}
static int gcd(int a,int b){if(b==0){return a;}return gcd(b,a%b);}
static long Modpow(long a,long p,long m){long res=1;while(p>0){if((p&1)!=0){res=(res*a)%m;}p >>=1;a=(a*a)%m;}return res;}
static long Modmul(long a,long b,long m){return ((a%m)*(b%m))%m;}
static long ModInv(long a,long m){return Modpow(a,m-2,m);}

//sort map w.r.t value use-> TreeSet of array? write comparator which depends on all the entries of array otherwise weired behavior you can prove it.
//......................................@uthor_Alx..............................................
  static void kmp( String s1,int[] p) throws  IOException{
    StringBuilder sb=new StringBuilder(s1);
    String s=s1+"#"+sb.reverse().toString();
        int n=s.length(),j=0;
        for(int i=1;i<n;i++){
             j=p[i-1];
            while(j>0&&s.charAt(i)!=s.charAt(j)){
                j=p[j-1];
            }
            if(s.charAt(i)==s.charAt(j)) j++;
            p[i]=j;
            //out.write(p[i]+" ");
        }
  }
   public static void main(String[] args) throws  IOException{
                assign();
                int t=int_v(read());//1;//,cn=1;
                while(t--!=0){
                    String s=read();
                    int n=s.length();
                    int[] p=new int[2*n+2];
                    int l1=0,l2=0;
                    int i1=0,j1=n-1;
                    while(i1<=j1){
                        if(s.charAt(i1)==s.charAt(j1)){
                            i1++;j1--;
                        }
                        else break;
                    }
                    if(j1<i1){
                        out.write(s+"\n"); continue;
                    }
                    StringBuilder sb=new StringBuilder(s.substring(i1,j1+1));
                    kmp(sb.toString(),p);
                    l1=p[2*(j1-i1+1)];
                    Arrays.fill(p,0);
                    kmp(sb.reverse().toString(),p);
                    l2=p[2*(j1-i1+1)];
                    sb=new StringBuilder();
                    if(l1>l2){
                        for(int i=0;i<i1;i++) sb.append(s.charAt(i));
                        for(int i=i1;i<i1+l1;i++) sb.append(s.charAt(i));
                        for(int i=n-i1;i<n;i++) sb.append(s.charAt(i));
                    }
                    else{
                        for(int i=0;i<i1;i++) sb.append(s.charAt(i));
                        for(int i=n-i1-l2;i<n-i1;i++) sb.append(s.charAt(i));
                        for(int i=n-i1;i<n;i++) sb.append(s.charAt(i));
                    }
                    out.write(sb.toString()+"\n");
                }
                    out.flush();

    }
}