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;
long long int modp(long long int a, long long int p, long long int n) {
  int d[100];
  int i, j, k, l;
  for (i = 0; n >= 1; i++) {
    d[i] = n % 2;
    n /= 2;
  }
  l = i;
  long long int e = 1;
  if (d[0] == 1) e *= (a % p);
  for (i = 1; i < l; i++) {
    a *= a;
    a = a % p;
    if (d[i] == 1) {
      e *= a;
      e = e % p;
    }
  }
  return e % p;
}
long long int modInverse(long long int n, long long int p) {
  return modp(n, p, p - 2);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p) {
  if (r == 0) return 1;
  long long int fac[n + 1];
  fac[0] = 1;
  for (long long int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;
  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int p = 1000000007, p2 = 998244353;
  long long int r, mid, l, ans;
  int t, n, m, i, j, k, ii, jj, kk, t1, t2;
  string s, ss;
  cin >> t;
  while (t--) {
    cin >> s;
    n = (int)s.size();
    ans = 0;
    while (ans < (n / 2) && (s[ans] == s[n - 1 - ans])) ans += 1;
    l = ans;
    ss.clear();
    r = n - ans;
    ss = s.substr(0, ans);
    t1 = t2 = 0;
    for (i = r - 1; i >= l; i--) {
      kk = 1;
      ii = 0;
      while (ii <= (i - l)) {
        if (s[i - ii] != s[l + ii]) {
          kk = 0;
          break;
        }
        ii += 1;
      }
      if (kk == 1) {
        t1 = i;
        break;
      }
    }
    for (i = l; i < r; i++) {
      kk = 1;
      ii = 0;
      while (ii <= r - 1 - i) {
        if (s[i + ii] != s[r - 1 - ii]) {
          kk = 0;
          break;
        }
        ii += 1;
      }
      if (kk == 1) {
        t2 = i;
        break;
      }
    }
    if (t1 == 0 && t2 == 0)
      ;
    else if (t1 + 1 >= n - t2) {
      ss += s.substr(ans, t1 - ans + 1);
    } else
      ss += s.substr(t2, n - t2 - ans);
    if (ans) ss += s.substr(n - ans, ans);
    if (ss.empty() == true) ss += s[0];
    cout << ss;
    cout << "\n";
  }
  return 0;
}