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 D2
{
    long MOD = 998244353;

    static class Manacher {
        static char BOUNDARY_CHARACTER = '|';
        // Longest Palindromic Substring array. //%
        // Stores for each boundary point in the string the longest //%
        // palindrome where that index is the center. //%

        static int[] starting(String s)
        {
            int n = s.length();
            int[] lps = Manacher.LPSArray(s);
            int[] fwd = new int[n];
            for (int i = 0; i < 2*n; i++)
            {
                if (lps[i] == 0)
                    continue;

                if (i % 2 == 0)
                {
                    int dist = lps[i] / 2;

                    int pos = i / 2 - 1;
                    int k = pos - dist + 1;

                    fwd[k] = Math.max(fwd[k], lps[i]);
                }
                else
                {
                    int dist = (lps[i] - 1) / 2;

                    int pos = i / 2;
                    int k = pos - dist;

                    fwd[k] = Math.max(fwd[k], lps[i]);
                }
            }

            return fwd;
        }

        static int[] LPSArray(String s) {
          char[] s2 = addBoundaries(s.toCharArray());
          int[] p = new int[s2.length]; 
          int c = 0, r = 0, m = 0, n = 0;
          for (int i = 1; i<s2.length; i++) {
            if (i>r) {
              p[i] = 0; m = i-1; n = i+1;
            } else {
              int i2 = c*2-i;
              if (p[i2]<(r-i)) { p[i] = p[i2];  m = -1; }
              else { p[i] = r-i;  n = r+1; m = i*2-n; }
            }
            while (m>=0 && n<s2.length && s2[m]==s2[n]) {
              p[i]++; m--; n++;
            }
            if ((i+p[i])>r) { c = i;  r = i+p[i]; }
          }
          return p;
        }
        private static char[] addBoundaries(char[] cs) {
          if (cs==null || cs.length==0)
            return new char[] {BOUNDARY_CHARACTER, BOUNDARY_CHARACTER};
          char[] cs2 = new char[cs.length*2+1];
          for (int i = 0; i<(cs2.length-1); i = i+2) {
            cs2[i] = BOUNDARY_CHARACTER;  cs2[i+1] = cs[i/2];
          }
          cs2[cs2.length-1] = BOUNDARY_CHARACTER;
          return cs2;
        }
        // Get the longest palindromic substring //%
        static String getLPS(String s) {
          int[] lps = LPSArray(s);
          int max = 0;
          for(int i = 0; i <= s.length()*2; ++i)
            if(lps[i] > lps[max]) max = i;
          int lower = (max - lps[max])/2;
          int higher = lower + lps[max];
          return s.substring(lower, higher);
        }
        // Get the LPS array where each index holds the longest //%
        // palindrome starting at that index in the string. //%
        static int[] StartLPSArray(String s) {
          int[] lps = LPSArray(s), ret = new int[s.length()];
          for(int i = 0; i < lps.length; ++i) {
            if(lps[i] > 0) {
              int lower = (i - lps[i])/2;
              ret[lower] = Math.max(ret[lower], lps[i]);
            }
          }
          for(int i = 1; i < s.length(); ++i)
            ret[i] = Math.max(ret[i], ret[i-1]-2);
          return ret;
        }
    }
      
    int go(int i, int j, int k, char[] s, int[][][] dp)
    {
        if (i > j)
            return -1000000;

        if (i == j)
            return 1;

        int val = 1;

        if (s[i] == s[j])
            val = Math.max(val, 2 + go(i+1, j-1, k == 0 ? 0 : 2, s, dp));

        if (k == 0 || k == 1)
            val = Math.max(val, go(i+1, j, 1, s, dp));

        if (k == 0 || k == 2)
            val = Math.max(val, go(i, j-1, 1, s, dp));

        return dp[i][j][k] = val;
    }

    void solve(FastIO io)
    {
        String a = io.next();

        char[] s = a.toCharArray();
        int n = s.length;


        int match = 0;
        for (int i = 0; i < n-1 - i; i++)
            if (s[i] == s[n-1-i])
                match++;
            else
                break;

        if (match == n / 2)
        {
            io.println(a);

            return;
        }


        String b = a.substring(match, n - match);
        int m = b.length();
        int[] lps = Manacher.starting(b);
        int[] lps2 = Manacher.starting(new StringBuilder(b).reverse().toString());

        StringBuilder ans = new StringBuilder();

        ans.append(a.substring(0, match));

        if (lps[0] >= lps2[0])
            ans.append(b.substring(0, lps[0]));
        else
            ans.append(b.substring(m - lps2[0], m));

        if (match != 0)
            ans.append(a.substring(n - match, n));
    
        io.println(ans);

    }

    public static void main(String[] args) {
        FastIO io = new FastIO();

        int t = io.nextInt();
        while (t-- > 0)
            new D2().solve(io);

        io.close();
    }

    static class FastIO extends PrintWriter
    {
        StringTokenizer st = new StringTokenizer("");
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));

        FastIO()
        {
            super(System.out);
        }

        String next()
        {
            while (!st.hasMoreTokens())
            {
                try {
                    st = new StringTokenizer(r.readLine());
                } catch (Exception e) { }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }
    }
}