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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int num[2000010], f[2000010], t, n;
char s[1000010];
bool bo[1000010];
inline int rd() {
  int x = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return x;
}
inline void gao(int n) {
  int mx = 1, id = 1;
  f[1] = 1;
  for (int i = 2; i < n; i++) {
    f[i] = (i > mx) ? 1 : min(mx - i + 1, f[id * 2 - i]);
    while (num[i - f[i]] == num[i + f[i]]) f[i]++;
    if (f[i] > mx) mx = f[i], id = i;
  }
}
inline void work() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  bo[0] = true;
  for (int i = 1; i <= n; i++) bo[i] = bo[i - 1] && (s[i] == s[n - i + 1]);
  num[0] = -233;
  for (int i = 1; i <= n; i++) num[i * 2 - 1] = -1, num[i * 2] = s[i] - 'a';
  num[n * 2 + 1] = -1;
  num[n * 2 + 2] = -2333;
  gao(n * 2 + 2);
  int ans = 0, id = 0;
  for (int i = 1; i <= n; i++) {
    int hh = f[i * 2] / 2;
    int l = i - hh + 1, r = i + hh - 1;
    if (l - 1 <= n - r) {
      if (bo[l - 1]) {
        if ((l - 1) * 2 + hh * 2 - 1 > ans)
          ans = (l - 1) * 2 + hh * 2 - 1, id = i * 2;
      }
    } else {
      if (bo[n - r]) {
        if ((n - r) * 2 + hh * 2 - 1 > ans)
          ans = (n - r) * 2 + hh * 2 - 1, id = i * 2;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    int hh = f[i * 2 + 1] / 2;
    int l = i - hh + 1, r = i + hh;
    if (l - 1 <= n - r) {
      if (bo[l - 1]) {
        if ((l - 1) * 2 + hh * 2 > ans)
          ans = (l - 1) * 2 + hh * 2, id = i * 2 + 1;
      }
    } else {
      if (bo[n - r]) {
        if ((n - r) * 2 + hh * 2 > ans)
          ans = (n - r) * 2 + hh * 2, id = i * 2 + 1;
      }
    }
  }
  if (id % 2 == 0) {
    int i = id / 2;
    int hh = f[i * 2] / 2;
    int l = i - hh + 1, r = i + hh - 1;
    if (l - 1 <= n - r) {
      for (int i = 1; i <= l - 1; i++) putchar(s[i]);
      for (int i = l; i <= r; i++) putchar(s[i]);
      for (int i = n - (l - 1) + 1; i <= n; i++) putchar(s[i]);
      putchar('\n');
    } else {
      for (int i = 1; i <= n - r; i++) putchar(s[i]);
      for (int i = l; i <= r; i++) putchar(s[i]);
      for (int i = n - (n - r) + 1; i <= n; i++) putchar(s[i]);
      putchar('\n');
    }
  } else {
    int i = id / 2;
    int hh = f[i * 2 + 1] / 2;
    int l = i - hh + 1, r = i + hh;
    if (l - 1 <= n - r) {
      for (int i = 1; i <= l - 1; i++) putchar(s[i]);
      for (int i = l; i <= r; i++) putchar(s[i]);
      for (int i = n - (l - 1) + 1; i <= n; i++) putchar(s[i]);
      putchar('\n');
    } else {
      for (int i = 1; i <= n - r; i++) putchar(s[i]);
      for (int i = l; i <= r; i++) putchar(s[i]);
      for (int i = n - (n - r) + 1; i <= n; i++) putchar(s[i]);
      putchar('\n');
    }
  }
}
int main() {
  t = rd();
  while (t--) work();
  return 0;
}