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 int> p[200005], q[200005], f(400005, 0), b(400005, 0),
    d(400005, 0);
map<pair<long long int, long long int>, long long int> mp;
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int bpow(long long int a, long long int b) {
  long long int res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % 1000000007;
    a = (a * a) % 1000000007;
    b >>= 1;
  }
  return res % 1000000007;
}
void fact(long long int i) {
  f[0] = 1;
  for (long long int k = 1; k <= i; k++) {
    (f[k] = f[k - 1] * k) %= 1000000007;
  }
}
long long int isprime(long long int n) {
  if (n == 1) return 0;
  for (long long int i = 2; i <= sqrt(n); i++)
    if (n % i == 0) return 0;
  return 1;
}
long long int find(long long int x) {
  if (f[x] == x)
    return x;
  else
    return f[x] = find(f[x]);
}
bool cmp(long long int x, long long int y) { return x < y; }
long long int comb(long long int i, long long int j) {
  long long int k = f[i];
  long long int g = (f[j] * (f[i - j])) % 1000000007;
  long long int h = bpow(g, 1000000007 - 2);
  return (k * h) % 1000000007;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    string s, b, c;
    cin >> s;
    long long int cnt = 0, n = s.size();
    for (long long int i = 0; i < s.size() / 2; i++) {
      if (s[i] == s[n - 1 - i]) {
        cnt += 2;
        b += s[i];
      } else
        break;
    }
    long long int k = cnt / 2;
    while (k--) {
      s.pop_back();
    }
    reverse(s.begin(), s.end());
    k = cnt / 2;
    while (s.size() > 0 && k--) {
      s.pop_back();
    }
    reverse(s.begin(), s.end());
    string a;
    long long int mx = 0;
    for (long long int i = 0; i < s.size(); i++) {
      a += s[i];
      long long int h = 0;
      for (long long int j = 0; j < a.size() / 2 + (a.size() % 2); j++) {
        if (a[j] != a[a.size() - 1 - j]) h = 1;
      }
      if (!h && a.size() > mx) {
        mx = max(mx, (long long int)a.size());
        c = a;
      }
    }
    reverse(s.begin(), s.end());
    a = "";
    for (long long int i = 0; i < s.size(); i++) {
      a += s[i];
      long long int h = 0;
      for (long long int j = 0; j < a.size() / 2 + (a.size() % 2); j++) {
        if (a[j] != a[a.size() - 1 - j]) h = 1;
      }
      if (!h && a.size() > mx) {
        mx = max(mx, (long long int)a.size());
        c = a;
      }
    }
    string u = b;
    reverse(u.begin(), u.end());
    b += c;
    b += u;
    cout << b << "\n";
  }
}