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 MOD = 998244353;
void solveA() {
  int T;
  cin >> T;
  while (T--) {
    long n;
    cin >> n;
    if (n == 1) {
      cout << "-1" << endl;
    } else {
      cout << '2';
      for (int i = 1; i < n; i++) cout << '3';
      cout << '\n';
    }
  }
}
void solveB() {
  long n, mx = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long x;
    cin >> x;
    cout << (x + mx) << " ";
    mx = max(mx, x + mx);
  }
}
void solveC() {
  int n, k;
  cin >> n >> k;
  long ans = 1;
  long long int res = 0;
  vector<int> pairs;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x > n - k) {
      pairs.push_back(i + 1);
      res += x;
    }
  }
  for (int i = 1; i < (int)pairs.size(); i++) {
    ans *= (pairs[i] - pairs[i - 1]);
    ans %= MOD;
  }
  cout << res << " " << ans;
}
void solveD() {
  int T;
  cin >> T;
  while (T--) {
    string s;
    cin >> s;
    int p1, p2, n;
    p1 = 0, p2 = s.size() - 1, n = s.size();
    while (p1 < p2 && s[p1] == s[p2]) p1++, p2--;
    string t1, t2;
    t1 = t2 = "";
    for (int i = p1; i < p2; i++) {
      t1 += s[i];
      if (t1.size() > t2.size()) {
        string x = t1;
        reverse(x.begin(), x.end());
        if (x == t1) t2 = t1;
      }
    }
    t1 = "";
    for (int i = p2; i >= p1; i--) {
      t1 += s[i];
      if (t1.size() > t2.size()) {
        string x = t1;
        reverse(x.begin(), x.end());
        if (x == t1) t2 = t1;
      }
    }
    string ans = s.substr(0, p1);
    string revans = ans;
    reverse(revans.begin(), revans.end());
    cout << ans << t2 << revans << endl;
  }
}
int main() {
  solveD();
  return 0;
}