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 max(long long a, long long b) { return a > b ? a : b; }
long long min(long long a, long long b) { return a < b ? a : b; }
double max(double a, double b) { return a > b ? a : b; }
double min(double a, double b) { return a < b ? a : b; }
long long modd(long long a) {
  if (a < 0LL) return a * -1LL;
  return a;
}
double modd(double a) {
  if (a < 0.0) return a * -1.0;
  return a;
}
long long modulo(long long a) { return a % 1000000007LL; }
long long power(long long k, long long p) {
  if (p == 0) return 1LL;
  long long P = power(k, p / 2LL);
  P = (P * P) % 1000000007LL;
  if (p % 2 == 1) P = (P * k) % 1000000007LL;
  return P;
}
struct p_str {
  char s;
  long long m;
};
void swap(long long& s1, long long& s2) {
  long long temp = s1;
  s1 = s2;
  s2 = temp;
}
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
void _manacher(long long d1[], long long d2[], string s, long long n) {
  for (int i = 0; i < n; i++) {
    d1[i] = 1;
    while (0 <= i - d1[i] && i + d1[i] < n && s[i - d1[i]] == s[i + d1[i]]) {
      d1[i]++;
    }
    d2[i] = 0;
    while (0 <= i - d2[i] - 1 && i + d2[i] < n &&
           s[i - d2[i] - 1] == s[i + d2[i]]) {
      d2[i]++;
    }
  }
  for (long long i = 0; i < n; i++) {
    long long t = d1[i];
    long long len = 2 * t - 1;
    long long j = i - t + 1;
    while (j <= i) {
      if (j != i && d1[j] >= len) {
        break;
      }
      d1[j++] = len;
      len -= 2;
    }
  }
  for (long long i = 0; i < n; i++) {
    long long t = d2[i];
    long long len = 2 * t;
    long long j = i + t - 1;
    while (j >= i) {
      if (j != i && d2[j] >= len) {
        break;
      }
      d2[j--] = len;
      len -= 2;
    }
  }
  cout << "d2 ";
  for (long long i = 0; i < n; i++) {
    cout << d2[i] << " ";
  }
  cout << endl;
}
bool _checkPal(string s) {
  long long n = s.length();
  for (long long i = 0, j = n - 1; i < j; i++, j--) {
    if (s[i] != s[j]) {
      return 0;
    }
  }
  return 1;
}
void solve() {
  string s;
  cin >> s;
  long long n = s.length();
  long long l = 0, r = n - 1;
  bool fl = 1;
  bool fr = 1;
  string a = "";
  string b = "";
  while (l <= r) {
    if (s[l] == s[r]) {
      a += string(1, s[l]);
      if (l < r) {
        b = string(1, s[r]) + b;
      }
    } else {
      string tl = "";
      string mx_pal = "";
      long long i = l;
      while (i < r) {
        tl += string(1, s[i]);
        if (_checkPal(tl)) {
          mx_pal = tl;
        }
        i++;
      }
      i = r;
      string tr = "";
      while (i > l) {
        tr += string(1, s[i]);
        if (_checkPal(tr)) {
          if (mx_pal.length() < tr.length()) {
            mx_pal = tr;
          }
        }
        i--;
      }
      a += mx_pal;
      break;
    }
    l++;
    r--;
  }
  string ans = a + b;
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}