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>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const long long mod1 = 998244353;
const long long mod = 1e9 + 7;
long long mod_mul(long long a, long long b) {
  a = a % mod;
  b = b % mod;
  return (((a * b) % mod) + mod) % mod;
}
long long inv(long long i) {
  if (i == 1) return 1;
  return (mod - ((mod / i) * inv(mod % i)) % mod) % mod;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long pwr(long long a, long long b) {
  a %= mod;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
long long findMinNumber(long long n) {
  long long count = 0, ans = 1;
  while (n % 2 == 0) {
    count++;
    n /= 2;
  }
  if (count % 2) ans *= 2;
  for (int i = 3; i <= sqrt(n); i += 2) {
    count = 0;
    while (n % i == 0) {
      count++;
      n /= i;
    }
    if (count % 2) ans *= i;
  }
  if (n > 2) ans *= n;
  return ans;
}
long long binarySearch(long long arr[], long long l, long long r, long long x) {
  if (r >= l) {
    long long mid = l + (r - l) / 2;
    if (arr[mid] == x) return mid;
    if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
    return binarySearch(arr, mid + 1, r, x);
  }
  return -1;
}
long long nCrModp(long long n, long long r, long long p) {
  if (r > n - r) r = n - r;
  long long C[r + 1];
  memset(C, 0, sizeof(C));
  C[0] = 1;
  for (long long i = 1; i <= n; i++) {
    for (long long j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p;
  }
  return C[r];
}
long long count_set_bit(long long n) {
  long long count = 0;
  while (n) {
    count += n & 1;
    n >>= 1;
  }
  return count;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long k;
bool ispalindrome(string &s, long long n) {
  long long d = n / 2;
  for (k = 0; k < d; k++) {
    if (s[k] != s[n - k - 1]) return false;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long n = s.length(), d, j, i;
    if (n == 1)
      cout << s[0] << '\n';
    else {
      long long ans = n - 1, ans1 = 1, ans2 = 1, ans3 = 1, idx, ans4 = 1;
      for (i = 0; i < n / 2; i++) {
        if (s[i] != s[n - 1 - i]) {
          ans = 2 * i;
          idx = i;
          for (j = i; j < n - idx; j++) {
            string a;
            a = s.substr(idx, j - idx + 1);
            d = a.length();
            if (ispalindrome(a, d)) ans1 = d;
          }
          for (j = n - idx - 1; j >= idx; j--) {
            string a;
            a = s.substr(j, n - idx - j);
            d = a.length();
            if (ispalindrome(a, d)) ans4 = d;
          }
          break;
        }
      }
      ans += max(ans1, ans4);
      for (i = 1; i < n; i++) {
        string a;
        a = s.substr(0, i + 1);
        d = a.length();
        if (ispalindrome(a, d)) {
          ans2 = d;
        }
      }
      for (i = n - 2; i >= 0; i--) {
        string a;
        a = s.substr(i, n - i);
        d = a.length();
        if (ispalindrome(a, d)) {
          ans3 = d;
        }
      }
      if (ans == max(max((ans), (ans2)), (ans3))) {
        if (ans == n)
          for (i = 0; i < n; i++) cout << s[i];
        else {
          for (i = 0; i < idx; i++) cout << s[i];
          if (ans1 >= ans4)
            for (i = idx; i < idx + ans1; i++) cout << s[i];
          else
            for (i = n - idx - ans4; i < n - idx; i++) cout << s[i];
          for (i = n - idx; i < n; i++) cout << s[i];
        }
        cout << '\n';
      } else if (ans2 == max(max((ans), (ans2)), (ans3))) {
        for (i = 0; i < ans2; i++) cout << s[i];
        cout << '\n';
      } else {
        for (i = 0; i < ans3; i++) cout << s[n - (ans3 - i)];
        cout << '\n';
      }
    }
  }
  return 0;
}