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 CFA {
  BufferedReader br;
  PrintWriter out;
  StringTokenizer st;
  boolean eof;
//  private static final long MOD = 1000L * 1000L * 1000L + 7;
  private static final long MOD = 998244353L;
  private static final int[] dx = {0, -1, 0, 1};
  private static final int[] dy = {1, 0, -1, 0};
  private static final String yes = "Yes";
  private static final String no = "No";

  void solve() {
    int T = nextInt();
//    int T = 1;
    for (int i = 0; i < T; i++) {
      helper();
    }
  }

  void helper() {
    char[] arr = nextString().toCharArray();
    int n = arr.length;
    boolean[][] dp = new boolean[n][n];
    for (int l = 1; l <= n; l++) {
      for (int i = 0; i < n; i++) {
        int j = i + l - 1;
        if (j >= n) {
          continue;
        }
        if (l == 1) {
          dp[i][j] = true;
        } else if (l == 2) {
          dp[i][j] = arr[i] == arr[j];
        } else {
          dp[i][j] = (arr[i] == arr[j]) && dp[i + 1][j - 1];
        }
      }
    }

    Pair tmp = null;
    int res = -1;
    for (int i = 0; i < n; i++) {
      if (dp[0][i]) {
        if (i + 1 > res) {
          res = i + 1;
          tmp = new Pair(i, n);
        }
      }
      if (dp[i][n - 1]) {
        if (n - 1 - i + 1 > res) {
          res = n - 1 - i + 1;
          tmp = new Pair(-1, i);
        }
      }
    }
    boolean[] pref = new boolean[n];
    boolean valid = true;
    for (int i = 0; i < n; i++) {
      valid = valid && (arr[i] == arr[n - 1 - i]);
      pref[i] = valid;
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 1; j < n; j++) {
        // [0, i] [j, n - 1]
        int l1 = i + 1;
        int l2 = n - 1 - j + 1;
        int min = Math.min(l1, l2);
        if (!pref[min - 1]) {
          continue;
        }
        if (l1 < l2) {
          int left = l2 - l1;
          // [j, j + left - 1]
          if (dp[j][j + left - 1]) {
            if (res < l1 + l2) {
              res = l1 + l2;
              tmp = new Pair(i, j);
            }
          }
        } else if (l1 > l2) {
          if (dp[l2][l1 - 1]) {
            if (res < l1 + l2) {
              res = l1 + l2;
              tmp = new Pair(i, j);
            }
          }
        } else {
          if (res < l1 + l2) {
            res = l1 + l2;
            tmp = new Pair(i, j);
          }
        }
      }
    }

    StringBuilder can = new StringBuilder();
    for (int i = 0; i <= tmp.start; i++) {
      can.append(arr[i]);
    }
    for (int i = tmp.end; i < n; i++) {
      can.append(arr[i]);
    }
    outln(can);
  }

  class Pair {
    int start;
    int end;
    public Pair(int start, int end) {
      this.start = start;
      this.end = end;
    }
  }

  void shuffle(int[] a) {
    int n = a.length;
    for(int i = 0; i < n; i++) {
      int r = i + (int) (Math.random() * (n - i));
      int tmp = a[i];
      a[i] = a[r];
      a[r] = tmp;
    }
  }

  long gcd(long a, long b) {
    while(a != 0 && b != 0) {
      long c = b;
      b = a % b;
      a = c;
    }
    return a + b;
  }
  int gcd(int a, int b) {
    while(a != 0 && b != 0) {
      int c = b;
      b = a % b;
      a = c;
    }
    return a + b;
  }
  private void outln(Object o) {
    out.println(o);
  }
  private void out(Object o) {
    out.print(o);
  }
  private void formatPrint(double val) {
    outln(String.format("%.9f", val));
  }
  public CFA() {
    br = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(System.out);
    solve();
    out.close();
  }
  public static void main(String[] args) {
    new CFA();
  }

  public long[] nextLongArr(int n) {
    long[] res = new long[n];
    for(int i = 0; i < n; i++)
      res[i] = nextLong();
    return res;
  }
  public int[] nextIntArr(int n) {
    int[] res = new int[n];
    for(int i = 0; i < n; i++)
      res[i] = nextInt();
    return res;
  }
  public String nextToken() {
    while (st == null || !st.hasMoreTokens()) {
      try {
        st = new StringTokenizer(br.readLine());
      } catch (Exception e) {
        eof = true;
        return null;
      }
    }
    return st.nextToken();
  }
  public String nextString() {
    try {
      return br.readLine();
    } catch (IOException e) {
      eof = true;
      return null;
    }
  }
  public int nextInt() {
    return Integer.parseInt(nextToken());
  }
  public long nextLong() {
    return Long.parseLong(nextToken());
  }
  public double nextDouble() {
    return Double.parseDouble(nextToken());
  }
}