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;
inline void normal(long long int &a) { a = (a + 998244353) % 998244353; }
inline int modMul(long long int a, long long int b) {
  a %= 998244353, b %= 998244353;
  normal(a), normal(b);
  return (a * b) % 998244353;
}
inline int modAdd(long long int a, long long int b) {
  a %= 998244353, b %= 998244353;
  normal(a), normal(b);
  return (a + b) % 998244353;
}
inline int modSub(long long int a, long long int b) {
  a %= 998244353, b %= 998244353;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline int modPow(long long int b, long long int p) {
  int r = 1;
  while (p) {
    if (p & 1) r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline int modInv(int a) { return modPow(a, 998244353 - 2); }
inline int modDiv(int a, int b) { return modMul(a, modInv(b)); }
inline int fact(int r) {
  int ans = 1;
  while (r) ans = modMul(ans, r), r--;
  return ans;
}
inline int invfact(int r) {
  int ans = 1;
  while (r) ans = modMul(ans, modInv(r)), r--;
  return ans;
}
inline double dgcd(double a, double b) {
  if (a < b) return dgcd(b, a);
  if (b < .0001) return a;
  return dgcd(b, fmod(a, b));
}
const long long int N = 5e6 + 5;
const long long int INF = (long long int)1e12;
long long int power(long long int base, long long int exp) {
  long long int res = 1;
  while (exp > 0) {
    if (exp % 2 == 1) res = (res * base) % 998244353;
    base = (base * base) % 998244353;
    exp /= 2;
  }
  return res % 998244353;
}
bool isPrime(int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
void Abesaale() {
  string ss;
  cin >> ss;
  if (ss.size() == 1) {
    cout << ss << endl;
    return;
  }
  string sss = ss;
  reverse((sss).begin(), (sss).end());
  if (ss == sss) {
    cout << ss << endl;
    return;
  }
  long long int ii = 0;
  while (ss[ii] == ss[(long long int)((ss).size()) - ii - 1] &&
         (ii < (long long int)((ss).size()) - ii - 1))
    ii++;
  string ans1 = "";
  for (long long int i = (long long int)((ss).size()) - ii - 1; i >= 0; i--) {
    string a = ss.substr(0, i + 1);
    string b = ss.substr((long long int)((ss).size()) - ii, ii);
    string c = a + b;
    string d = c;
    reverse((c).begin(), (c).end());
    if (c == d) {
      ans1 += c;
      break;
    }
  }
  reverse((ss).begin(), (ss).end());
  string ans2 = "";
  for (long long int i = (long long int)((ss).size()) - ii - 1; i >= 0; i--) {
    string a = ss.substr(0, i + 1);
    string b = ss.substr((long long int)((ss).size()) - ii, ii);
    string c = a + b;
    string d = c;
    reverse((c).begin(), (c).end());
    if (c == d) {
      ans2 += c;
      break;
    }
  }
  if ((long long int)((ans1).size()) < (long long int)((ans2).size()))
    cout << ans2 << endl;
  else
    cout << ans1 << endl;
  return;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  clock_t start, end;
  start = clock();
  cout << fixed << setprecision(11);
  cin >> t;
  while (t--) {
    Abesaale();
  }
  end = clock();
  double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
}