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 MainClass
{
    public static void main(String[] args)throws IOException
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(in.readLine());
        KMPAlgorithm kmpAlgorithm = new KMPAlgorithm();
        StringBuilder stringBuilder = new StringBuilder();
        while (t-- > 0)
        {
            String ss = in.readLine();
            char[] S = ss.toCharArray();
            int i = 0;
            int j = S.length - 1;
            StringBuilder stringBuilder1 = new StringBuilder();
            StringBuilder stringBuilder2 = new StringBuilder();
            while (i <= j)
            {
                if (S[i] == S[j])
                {
                    if (i == j)
                    {
                        stringBuilder1.append(S[i]);
                    }
                    else
                    {
                        stringBuilder1.append(S[i]);
                        stringBuilder2.append(S[i]);
                    }
                    i++;
                    j--;
                }
                else
                {
                    break;
                }
            }
            String s1 = "";
            if (i < j)
            {
                String s = ss.substring(i, j + 1);
                StringBuilder stringBuilder3 = new StringBuilder();
                StringBuilder stringBuilder4 = new StringBuilder(s);
                stringBuilder4.reverse();
                stringBuilder3.append(s).append("#").append(stringBuilder4);
                int[] arr = kmpAlgorithm.computeTemporalArray(stringBuilder3.toString().toCharArray());
                int max1 = arr[arr.length - 1];

                StringBuilder stringBuilder5 = new StringBuilder();
                stringBuilder5.append(stringBuilder4).append("#").append(s);
                int[] arr1 = kmpAlgorithm.computeTemporalArray(stringBuilder5.toString().toCharArray());
                int max2 = arr1[arr1.length - 1];
                if (max1 >= max2)
                {
                    s1 = s.substring(0, max1);
                }
                else
                    s1 = s.substring(s.length() - max2);
            }
            stringBuilder.append(stringBuilder1).append(s1).append(stringBuilder2.reverse()).append("\n");
        }
        System.out.println(stringBuilder);
    }
    public static String manacher(char[] T)
    {
        int[] P = new int[T.length];
        int C = 0;
        int R = 0;
        for (int i=1;i<T.length - 1;i++)
        {
            int mirr = 2 * C - i;
            if (i < R)
                P[i] = Math.min(R - i, P[mirr]);
            while ((i + 1 + P[i]) < T.length && (i - 1 - P[i]) >= 0 && T[i + 1 + P[i]] == T[i - 1 - P[i]])
                P[i]++;
            if (i + P[i] > R)
            {
                C = i;
                R = i + P[i];
            }
        }
        int maxStart = 0;
        int maxEnd = 0;
        for (int i=1;i<T.length - 1;i++)
        {
            if (i % 2 == 0)
            {
                if (i - (P[i] / 2) * 2 == 2)
                {
                    maxStart = Math.max(maxStart, P[i]);
                }
                if (i + (P[i] / 2) * 2 == T.length - 2)
                {
                    maxEnd = Math.max(maxEnd, P[i]);
                }
            }
            else
            {
                if (i - (P[i] / 2) * 2 == 1)
                {
                    maxStart = Math.max(maxStart, P[i]);
                }
                if (i + (P[i] / 2) * 2 == T.length - 1)
                {
                    maxEnd = Math.max(maxEnd, P[i]);
                }
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i=2;i<T.length - 1;i+= 2)
        {
            stringBuilder.append(T[i]);
        }
        String s = stringBuilder.toString();
        if (maxStart >= maxEnd)
        {
            return s.substring(0, maxStart);
        }
        else
            return s.substring(s.length() - maxEnd - 1);
    }
}
class KMPAlgorithm
{
    public int[] computeTemporalArray(char[] pattern)
    {
        int[] lps = new int[pattern.length];

        int index = 0;
        for(int i=1;i<pattern.length;)
        {
            if(pattern[i] == pattern[index])
            {
                lps[i] = index + 1;
                index++;
                i++;
            }
            else
            {
                if(index != 0)
                {
                    index = lps[index - 1];
                }
                else
                {
                    lps[i] = 0;
                    i++;
                }
            }
        }
        return lps;
    }

    public ArrayList<Integer> KMPMatcher(char[] text, char[] pattern)
    {
        int[] lps = computeTemporalArray(pattern);

        int j = 0;
        int i = 0;
        int n = text.length;
        int m = pattern.length;
        ArrayList<Integer> indices = new ArrayList<>();
        while(i < n)
        {
            if(pattern[j] == text[i])
            {
                i++;
                j++;
            }
            if(j == m)
            {
                indices.add(i - j);
                j = lps[j - 1];
            }
            else if(i < n && pattern[j] != text[i])
            {
                if(j != 0)
                    j = lps[j - 1];
                else
                    i = i + 1;
            }
        }
        return indices;
    }
}
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();
    }
}