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;
template <class T>
inline T bigMod(T p, T e, T M) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T modInverse(T a, T M) {
  return bigMod(a, M - 2, M);
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
struct FindHash {
  int b1 = 129, b2 = 371;
  vector<int> bp1, bp2;
  vector<int> h1, h2;
  string s;
  int sz;
  long long int mod1 = 998244353LL, mod2 = 1000000009LL;
  void init(string _s) {
    bp1.clear();
    bp2.clear();
    h1.clear();
    h2.clear();
    s = _s;
    sz = s.size();
    bp1.push_back(1);
    bp2.push_back(1);
    for (int i = 1; i < sz + 2; i++) {
      bp1.push_back((1LL * bp1.back() * b1) % mod1);
      bp2.push_back((1LL * bp2.back() * b2) % mod2);
    }
    h1.push_back(s[0] - 'a');
    h2.push_back(s[0] - 'a');
    for (int i = 1; i < sz; i++) {
      h1.push_back((1LL * h1.back() * b1 + s[i] - 'a') % mod1);
      h2.push_back((1LL * h2.back() * b2 + s[i] - 'a') % mod2);
    }
  }
  pair<int, int> Get(int i, int j) {
    pair<int, int> res = make_pair(h1[j], h2[j]);
    if (i > 0)
      res = make_pair((res.first - 1LL * h1[i - 1] * bp1[j - i + 1]) % mod1,
                      (res.second - 1LL * h2[i - 1] * bp2[j - i + 1]) % mod2);
    res = make_pair((res.first % mod1 + mod1) % mod1,
                    (res.second % mod2 + mod2) % mod2);
    return res;
  }
};
int main() {
  FindHash f1, f2;
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int n;
    n = s.size();
    n--;
    int lo = 0, hi = n;
    while (lo < hi) {
      if (s[lo] == s[hi]) {
        lo++, hi--;
      } else
        break;
    }
    if (lo >= hi)
      cout << s << '\n';
    else {
      int mxl = -1, mxh = -1;
      f1.init(s);
      reverse(s.begin(), s.end());
      f2.init(s);
      reverse(s.begin(), s.end());
      for (int i = 0; i < lo; i++) cout << s[i];
      for (int j = lo; j <= hi; j++) {
        if (f1.Get(lo, j) == f2.Get(n - j, n - lo)) mxl = j;
      }
      for (int j = hi; j >= lo; j--) {
        if (f1.Get(j, hi) == f2.Get(n - hi, n - j)) mxh = j;
      }
      if (mxl - lo >= hi - mxh) {
        for (int i = lo; i <= mxl; i++) cout << s[i];
      } else {
        for (int i = mxh; i <= hi; i++) cout << s[i];
      }
      for (int i = hi + 1; i <= n; i++) cout << s[i];
      cout << '\n';
    }
  }
  return 0;
}