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 MAX_N = 1e6 + 6;
const int P1 = 792924877;
const int P2 = 1017450191;
const int P3 = 614677073;
const int P4 = 809880751;
const int P5 = 1061596561;
const int DIGIT = 401016061;
struct HASH {
  const int MOD;
  long long p[MAX_N], h[MAX_N], rh[MAX_N];
  HASH() = delete;
  HASH(const int x) : MOD(x) {
    p[0] = 1;
    for (int i = 1; i < MAX_N; i++) p[i] = (p[i - 1] * DIGIT) % MOD;
  }
  void calc_h(char *s, int n) {
    h[0] = rh[n + 1] = 0;
    for (int i = 1; i <= n; i++)
      h[i] = (h[i - 1] * DIGIT + s[i] - 'a' + 1) % MOD;
    for (int i = n; i >= 1; i--)
      rh[i] = (rh[i + 1] * DIGIT + s[i] - 'a' + 1) % MOD;
  }
  long long H(int l, int r) {
    return ((h[r] - h[l - 1] * p[r - l + 1] % MOD) % MOD + MOD) % MOD;
  }
  long long RH(int l, int r) {
    return ((rh[l] - rh[r + 1] * p[r - l + 1] % MOD) % MOD + MOD) % MOD;
  }
  bool same(int l, int r) { return H(l, r) == RH(l, r); }
} h1(P1), h2(P2), h3(P3), h4(P4), h5(P5);
int t;
char s[MAX_N];
int n;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    h1.calc_h(s, n);
    h2.calc_h(s, n);
    int same = 0;
    for (int i = 1, j = n; i < j && s[i] == s[j]; i++, j--) same++;
    int L = same, R = n - same + 1;
    for (int i = L + 1, l = i; i <= n - same; i++)
      if (h1.same(l, i) && h2.same(l, i) && h3.same(l, i) && h4.same(l, i) &&
          h5.same(l, i))
        L = i;
    for (int i = R - 1, r = i; i >= same + 1; i--)
      if (h1.same(i, r) && h2.same(i, r) && h3.same(i, r) && h4.same(i, r) &&
          h5.same(i, r))
        R = i;
    for (int i = 1; i <= same; i++) putchar(s[i]);
    if (L > n - R + 1)
      for (int i = same + 1; i <= L; i++) putchar(s[i]);
    else
      for (int i = R; i <= n - same; i++) putchar(s[i]);
    for (int i = n - same + 1; i <= n; i++) putchar(s[i]);
    puts("");
  }
  return 0;
}