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;
const int maxn = 1e6 + 10;
const int maxm = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const int maxblock = sqrt(maxn) + 10;
const double eps = 1e-7;
const long long INF = 1e16;
int n;
char str[maxn];
int len[maxn << 1];
char s[maxn << 1];
int manachar(char *p) {
  int n = strlen(p), l = 0;
  s[l++] = '@';
  s[l++] = '#';
  for (int i = 0; i < n; i++) {
    s[l++] = p[i];
    s[l++] = '#';
  }
  s[l++] = '~';
  s[l] = 0;
  int Max = 0, pos = 0, ans = 0;
  for (int i = 1; i < l; i++) {
    if (Max > i) {
      len[i] = min(len[2 * pos - i], Max - i);
    } else
      len[i] = 1;
    while (s[i + len[i]] == s[i - len[i]]) len[i]++;
    ans = max(ans, len[i]);
    if (len[i] + i > Max) {
      Max = len[i] + i;
      pos = i;
    }
  }
  return ans - 1;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%s", str);
    manachar(str);
    int max_prefix = 0;
    int n = strlen(str);
    int pos = 0;
    while (pos < n && str[pos] == str[n - 1 - pos] && pos <= n - 1 - pos) {
      pos++;
    }
    max_prefix = pos;
    int Max = 1;
    int pre = 0;
    int ansl = 0, ansr = 0;
    for (int i = 2; i < 2 * n + 3; i++) {
      int l = (i - (len[i] - 1)) / 2;
      int r = (i + (len[i] - 1)) / 2 - 1;
      if (l > r) continue;
      int other_len = std::min(l, n - r - 1);
      if (other_len <= max_prefix && 2 * other_len + (r - l + 1) > Max) {
        Max = 2 * other_len + (r - l + 1);
        pre = other_len;
        ansl = l;
        ansr = r;
      }
    }
    for (int i = 0; i < pre; i++) printf("%c", str[i]);
    for (int i = ansl; i <= ansr; i++) printf("%c", str[i]);
    for (int i = n - pre; i < n; i++) printf("%c", str[i]);
    printf("\n");
  }
  return 0;
}