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;
vector<long long> z;
const long long N = 5e3 + 7, M1 = 1e9 + 7, M2 = 1e9 + 9, b1 = 127, b2 = 239;
long long maxi1[N], maxi2[N], maxx[N];
pair<long long, long long> h1[N], h2[N];
pair<long long, long long> poww[N];
string best;
long long mod(long long x, long long m) {
  x %= m;
  if (x < 0) x += m;
  return x;
}
pair<long long, long long> get_h(long long l, long long r) {
  long long res1 =
      mod(h1[r].first - h1[l - 1].first * poww[r - l + 1].first, M1);
  long long res2 =
      mod(h1[r].second - h1[l - 1].second * poww[r - l + 1].second, M2);
  return {res1, res2};
}
pair<long long, long long> get_h2(long long l, long long r) {
  long long res1 =
      mod(h2[r].first - h2[l - 1].first * poww[r - l + 1].first, M1);
  long long res2 =
      mod(h2[r].second - h2[l - 1].second * poww[r - l + 1].second, M2);
  return {res1, res2};
}
void solve(string s) {
  long long n = s.length();
  string t = s;
  long long ser = (n + 1) / 2;
  long long len = 0;
  while (len != n && s[len] == s[n - len - 1]) {
    len++;
  }
  if (len == n) {
    best = s;
    return;
  }
  if (2 * len > (long long)best.size()) {
    best = "";
    for (long long i = 0; i < len; i++) best += s[i];
    for (long long i = len - 1; i >= 0; i--) best += s[i];
  }
  for (long long i = 0; i < ser; i++) {
    maxi1[i] = -1;
    long long l = i, r = i;
    while (l >= 0 && r < n && s[l] == s[r]) {
      l--;
      r++;
      maxi1[i] += 2;
      if (l < len) {
        long long curlen = maxi1[i] + 2 * (l + 1);
        if (curlen > (long long)best.size()) {
          best = "";
          for (long long j = 0; j < r; j++) {
            best += s[j];
          }
          for (long long j = l; j >= 0; j--) {
            best += s[j];
          }
        }
      }
    }
  }
  if (n % 2 == 1) ser--;
  for (long long i = 0; i < ser; i++) {
    maxi2[i] = 0;
    long long l = i, r = i + 1;
    while (l >= 0 && r < n && s[l] == s[r]) {
      l--;
      r++;
      maxi2[i] += 2;
      if (l < len) {
        long long curlen = maxi2[i] + 2 * (l + 1);
        if (curlen > (long long)best.size()) {
          best = "";
          for (long long j = 0; j < r; j++) {
            best += s[j];
          }
          for (long long j = l; j >= 0; j--) {
            best += s[j];
          }
        }
      }
    }
  }
}
signed main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  for (long long i = 0; i < t; i++) {
    string s;
    cin >> s;
    best = s[0];
    solve(s);
    reverse(s.begin(), s.end());
    solve(s);
    cout << best << endl;
  }
  return 0;
}