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.lang.reflect.Array;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.util.*;
import java.lang.*;
import java.util.HashMap;
import java.util.PriorityQueue;
public class Solution implements Runnable
{
    static class pair implements Comparable
    {
        int f;
        int s;
        pair(int fi, int se)
        {
            f=fi;
            s=se;
        }
        public int compareTo(Object o)//desc order
        {
            pair pr=(pair)o;
            if(s>pr.s)
                return -1;
            if(s==pr.s)
            {
                if(f>pr.f)
                    return 1;
                else
                    return -1;
            }
            else
                return 1;
        }
        public boolean equals(Object o)
        {
            pair ob=(pair)o;
            if(o!=null)
            {
                if((ob.f==this.f)&&(ob.s==this.s))
                    return true;
            }
            return false;
        }
        public int hashCode()
        {
            return (this.f+" "+this.s).hashCode();
        }
    }
    public class triplet implements Comparable
    {
        int f;
        int s;
        int t;
        triplet(int f,int s,int t)
        {
            this.f=f;
            this.s=s;
            this.t=t;
        }
        public boolean equals(Object o)
        {
            triplet ob=(triplet)o;
            if(o!=null)
            {
                if((ob.f==this.f)&&(ob.s==this.s)&&(ob.t==this.t))
                    return true;
            }
            return false;
        }
        public int hashCode()
        {
            return (this.f+" "+this.s+" "+this.t).hashCode();
        }
        public int compareTo(Object o)//asc order
        {
            triplet tr=(triplet)o;
            if(t>tr.t)
                return 1;
            else
                return -1;
        }
    }
    void merge1(int arr[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        for (int i=0; i<n1; ++i)
            L[i] = arr[l + i];
        for (int j=0; j<n2; ++j)
            R[j] = arr[m + 1+ j];
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i]<=R[j])
            {
                arr[k] = L[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
    void sort1(int arr[], int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort1(arr, l, m);
            sort1(arr , m+1, r);
            merge1(arr, l, m, r);
        }
    }
    long M=(long)1e9 + 7;
    long dp[][][],pow[];
    long solve(int i, int prev, int k, String s)
    {
        if(i==s.length())
            return 1;
        if(dp[i][prev][k]!=-1)
            return dp[i][prev][k];
        //System.out.println(i+" "+prev+" "+k);
        dp[i][prev][k]=0;
        int kk=s.charAt(i)-(int)'0';
        if(k==1)
        {
            for(int j=0;j<10;j++)
            {
                if(j==prev)
                    dp[i][prev][k]+=solve(i+1,prev,k,s);
                else
                    dp[i][prev][k]+=j*pow[s.length()-i-1] * solve(i+1,j,k,s);
            }
        }
        else
        {
            for(int j=0;j<kk;j++)
            {
                if(j==prev)
                    dp[i][prev][k]+=solve(i+1,prev,1,s);
                else
                    dp[i][prev][k]+=j*pow[s.length()-i-1] * solve(i+1,j,1,s);
            }
            if(kk==prev)
                dp[i][prev][k]+=solve(i+1,kk,0,s);
            else
                dp[i][prev][k]+=kk*pow[s.length()-i-1] * solve(i+1,kk,0,s);
        }
        return dp[i][prev][k];
    }
    int kmpsearch(String pat,String text,int p)
    {
        String s=pat+"$"+text;
        int n=s.length();
        int lps[]=new int[n+1];
        lps[0]=0;
        int border=0;
        for(int i=1;i<n;i++)
        {
            while(border>0 && s.charAt(i)!=s.charAt(border))
                border=lps[border-1];
            if(s.charAt(i)==s.charAt(border))
                border=border+1;
            else
                border=0;
            lps[i]=border;
        }
        int max=0;
        for(int i=pat.length()+1;i<n;i++)
        {
            if(lps[i]==i-text.length())
                max = Math.max(max, lps[i]);
        }
        if(p==0)
            return max;
        else
            return lps[n-1];
    }
    public static void main(String args[])throws Exception
    {
        new Thread(null,new Solution(),"Solution",1<<27).start();
    }
    public void run()
    {
        try
        {
            InputReader in = new InputReader(System.in);
            PrintWriter out = new PrintWriter(System.out);
            /*int t=in.ni();
            pow=new long[100005];
            pow[0]=1;
            for(int i=1;i<100005;i++)
                pow[i]=(pow[i-1] * 10)%M;
            while(t--!=0)
            {
                int nl=in.ni();
                BigInteger b1 = new BigInteger(in.readString());
                BigInteger b2=new BigInteger("1");
                BigInteger b3=b1.subtract(b2);
                String l=b3.toString();
                int nr=in.ni();
                String r=in.readString();
                //out.println(l+" "+r);
                dp=new long[100005][10][2];
                for(int i=0;i<100005;i++)
                {
                    for(int j=0;j<10;j++)
                        Arrays.fill(dp[i][j],-1);
                }
                long x=solve(0,0,0,"11");
                out.println(x-1);
                /*long x=solve(0,0,0,r);
                out.println(x);
                for(int i=0;i<100005;i++)
                {
                    for(int j=0;j<10;j++)
                        Arrays.fill(dp[i][j],-1);
                }
                long y=solve(0,0,0,l);
                out.println(x+" "+y);
                out.println(x-y);
            }*/
            int t=in.ni();
            while(t--!=0)
            {
                String s=in.nextLine();
                StringBuilder sb=new StringBuilder(s);
                StringBuilder sb1=sb.reverse();
                String s1=sb1.toString();
                int k=kmpsearch(s,s1,0);
                if(2*k >= s.length())
                    out.println(s);
                else
                {
                    String ss=s.substring(k,s.length()-k);
                    sb=new StringBuilder(ss);
                    sb1=sb.reverse();
                    String ss1=sb1.toString();
                    //out.println(ss+" "+ss1);
                    int x=kmpsearch(ss,ss1,1);
                    int y=kmpsearch(ss1,ss,1);
                    //out.println(x+" "+y);
                    if(x>=y)
                        out.println(s.substring(0,k+x)+s.substring(s.length()-k));
                    else
                        out.println(s.substring(0,k)+s.substring(s.length()-k-y));
                }
            }
            out.close();
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
    static class InputReader {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            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 ni() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nl() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }
}