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 long long p1 = 31;
const long long p2 = 37;
const long long mod1 = 1e9 + 7;
const long long mod2 = 1e9 + 9;
const long long nax = 5e3 + 10;
long long hash1[nax], hash2[nax], invhash1[nax], invhash2[nax];
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % m;
    a *= a;
    a %= m;
    b >>= 1;
  }
  return res;
}
void compute_hash() {
  hash1[0] = 1;
  hash2[0] = 1;
  for (long long i = 1; i < nax; i++) {
    hash1[i] = (hash1[i - 1] * p1) % mod1;
    hash2[i] = (hash2[i - 1] * p2) % mod2;
  }
  invhash1[nax - 1] = binpow(hash1[nax - 1], mod1 - 2, mod1);
  invhash2[nax - 1] = binpow(hash2[nax - 1], mod2 - 2, mod2);
  for (long long i = nax - 2; i >= 0; i--) {
    invhash1[i] = (invhash1[i + 1] * p1) % mod1;
    invhash2[i] = (invhash2[i + 1] * p2) % mod2;
  }
  return;
}
vector<pair<long long, long long> > get_hash(string s) {
  long long n = (long long)(s.size());
  vector<pair<long long, long long> > res(n);
  for (long long i = 0; i < n; i++) {
    long long x = s[i] - 'a' + 1;
    res[i] = {(x * hash1[i]) % mod1, (x * hash2[i]) % mod2};
    if (i) {
      res[i].first = (res[i - 1].first + res[i].first) % mod1;
      res[i].second = (res[i - 1].second + res[i].second) % mod2;
    }
  }
  return res;
}
signed main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  compute_hash();
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long n = (long long)(s.size());
    auto v1 = get_hash(s);
    reverse(s.begin(), s.end());
    auto v2 = get_hash(s);
    reverse(s.begin(), s.end());
    long long mx = 0;
    string ans;
    for (long long i = 0; i < n; i++) {
      for (long long j = i + 1; j < n; j++) {
        long long len = min(i + 1, n - j);
        auto x1 = v1[len - 1];
        auto x2 = v2[len - 1];
        if (x1 == x2) {
          if (i + 1 == n - j) {
            if (mx < len) {
              mx = len;
              ans = s.substr(0, len) + s.substr(j, len);
            }
          } else if (i + 1 > n - j) {
            long long nl = i + 1 - len;
            x1.first = (v1[i].first - v1[len - 1].first + mod1) % mod1;
            x1.second = (v1[i].second - v1[len - 1].second + mod2) % mod2;
            x1.first *= invhash1[len];
            x1.first %= mod1;
            x1.second *= invhash2[len];
            x1.second %= mod2;
            x2.first =
                (v2[n - len - 1].first - v2[n - len - 1 - nl].first + mod1) %
                mod1;
            x2.second =
                (v2[n - len - 1].second - v2[n - len - 1 - nl].second + mod2) %
                mod2;
            x2.first *= invhash1[n - len - nl];
            x2.first %= mod1;
            x2.second *= invhash2[n - len - nl];
            x2.second %= mod2;
            if (x1 == x2 && mx < i + 1 + len) {
              mx = i + 1 + len;
              ans = s.substr(0, i + 1);
              ans += s.substr(j, len);
            }
          } else {
            long long nl = n - j - len;
            x1.first =
                (v1[n - len - 1].first - v1[n - len - 1 - nl].first + mod1) %
                mod1;
            x1.second =
                (v1[n - len - 1].second - v1[n - len - 1 - nl].second + mod2) %
                mod2;
            x1.first *= invhash1[n - len - nl];
            x1.first %= mod1;
            x1.second *= invhash2[n - len - nl];
            x1.second %= mod2;
            x2.first =
                (v2[len + nl - 1].first - v2[len - 1].first + mod1) % mod1;
            x2.second =
                (v2[len + nl - 1].second - v2[len - 1].second + mod2) % mod2;
            x2.first *= invhash1[len];
            x2.first %= mod1;
            x2.second *= invhash2[len];
            x2.second %= mod2;
            if (x1 == x2 && mx < 2 * len + nl) {
              mx = 2 * len + nl;
              ans = s.substr(0, i + 1);
              ans += s.substr(j, n - j);
            }
          }
        }
      }
    }
    for (long long i = 0; i < n; i++) {
      auto x = v1[i];
      auto y = v2[n - 1];
      if (i != n - 1) {
        y.first = (y.first - v2[n - 2 - i].first + mod1) % mod1;
        y.second = (y.second - v2[n - 2 - i].second + mod2) % mod2;
      }
      y.first *= invhash1[n - 1 - i];
      y.first %= mod1;
      y.second *= invhash2[n - 1 - i];
      y.second %= mod2;
      if (x == y && mx < i + 1) {
        mx = i + 1;
        ans = s.substr(0, i + 1);
      }
    }
    reverse(s.begin(), s.end());
    for (long long i = 0; i < n - 1; i++) {
      auto x = v2[i];
      auto y = v1[n - 1];
      y.first = (y.first - v1[n - 2 - i].first + mod1) % mod1;
      y.second = (y.second - v1[n - 2 - i].second + mod2) % mod2;
      y.first *= invhash1[n - 1 - i];
      y.first %= mod1;
      y.second *= invhash2[n - 1 - i];
      y.second %= mod2;
      if (x == y && mx < i + 1) {
        mx = i + 1;
        ans = s.substr(0, i + 1);
      }
    }
    cout << ans << "\n";
  }
}