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 = 5010;
const int MAX_M = 201;
const long long INF = 1000000000000000000;
const long long P = 998244353;
const int SIGMA = 28;
bool start[MAX_N][MAX_N], endh[MAX_N][MAX_N];
void clear(int n) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      start[i][j] = false;
      endh[i][j] = false;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    string str;
    cin >> str;
    if ((int)str.size() == 1) {
      cout << str << "\n";
      continue;
    } else if ((int)str.size() == 2) {
      if (str[0] != str[1])
        cout << str[0] << "\n";
      else
        cout << str << "\n";
      continue;
    }
    clear((int)str.size());
    for (int i = 0; i < (int)str.size(); i++) {
      for (int j = i, k = i; 0 <= j && k < (int)str.size(); j--, k++) {
        if (str[j] != str[k]) break;
        endh[k][j] = true;
        start[j][k] = true;
      }
      for (int j = i, k = i + 1; 0 <= j && k < (int)str.size(); j--, k++) {
        if (str[j] != str[k]) break;
        endh[k][j] = true;
        start[j][k] = true;
      }
    }
    pair<int, pair<int, int> > p =
        pair<int, pair<int, int> >(0, pair<int, int>(0, 0));
    for (int i = 1, j = (int)str.size() - 2; i < j; i++, j--) {
      if (str[i - 1] != str[j + 1]) break;
      int mi = -1, mj = -1;
      for (int k = 0; k < (int)str.size(); k++) {
        if (start[i][k] && k <= j) {
          mi = k;
        }
        if (endh[j][k] && k >= i && mj == -1) {
          mj = k;
        }
      }
      pair<int, pair<int, int> > pi = pair<int, pair<int, int> >(
          mi - i + 1 + i * 2, pair<int, int>(mi, j + 1));
      pair<int, pair<int, int> > pj = pair<int, pair<int, int> >(
          (mj == -1) ? -1 : (j - mj + 1 + i * 2), pair<int, int>(i - 1, mj));
      p = max(p, max(pi, pj));
    }
    int beg = 0;
    for (int i = (int)str.size() - 1; i >= 0; i--) {
      if (start[0][i]) {
        beg = i;
        break;
      }
    }
    int end = (int)str.size() - 1;
    for (int i = 0; i < (int)str.size(); i++) {
      if (endh[(int)str.size() - 1][i]) {
        end = i;
        break;
      }
    }
    if (beg + 1 > (int)str.size() - end) {
      if (beg + 1 > p.first) {
        cout << str.substr(0, beg + 1) << "\n";
      } else {
        pair<int, int> b = p.second;
        cout << str.substr(0, b.first + 1) +
                    str.substr(b.second, (int)str.size() - b.second)
             << "\n";
      }
    } else if ((int)str.size() - end > p.first) {
      cout << str.substr(end, (int)str.size() - end) << "\n";
    } else {
      pair<int, int> b = p.second;
      cout << str.substr(0, b.first + 1) +
                  str.substr(b.second, (int)str.size() - b.second)
           << "\n";
    }
  }
  return 0;
}