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>
template <class T>
bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
vector<ll> manacher(vector<ll> &ve, bool gusu = false) {
  ll n = ve.size();
  if (gusu) {
    vector<ll> ve2(n * 2 - 1), res;
    for (ll i = 0; i < n * 2 - 1; i++) {
      if (i % 2 == 0)
        ve2[i] = ve[i / 2];
      else
        ve2[i] = -1000000007;
    }
    res = manacher(ve2);
    for (ll i = 0; i < n * 2 - 1; i++) {
      if (i % 2 == 0)
        res[i] = (res[i] + 1) / 2;
      else
        res[i] /= 2;
    }
    return res;
  }
  vector<ll> res(n);
  ll left = 0, right = 0;
  while (left < n) {
    while (left - right >= 0 and left + right < n and
           ve[left - right] == ve[left + right])
      ++right;
    res[left] = right;
    ll k = 1;
    while (left - k >= 0 and left + k < n and k + res[left - k] < right)
      res[left + k] = res[left - k], ++k;
    left += k;
    right -= k;
  }
  return res;
}
vector<ll> stove(string &str) {
  ll n = str.size();
  vector<ll> res(n);
  for (ll i = 0; i < n; i++) res[i] = str[i] - 'a';
  return res;
}
void Main() {
  ll Q;
  cin >> Q;
  while (Q--) {
    string S;
    cin >> S;
    ll N = S.size();
    ll now = 0;
    string mae, ato;
    while (now * 2 < N - 2 and S[now] == S[N - 1 - now]) {
      mae += S[now];
      ato += S[N - 1 - now];
      ++now;
    }
    auto naka = S.substr(now, N - now - now);
    auto nakas = stove(naka);
    auto mana = manacher(nakas, true);
    ll ma = 0, mi = 0;
    for (ll i = 0; i < mana.size(); i++) {
      if (i % 2 == 0) {
        ll xi = i / 2 + 1;
        if (xi == mana[i]) maxi(ma, xi * 2 - 1);
      } else {
        ll xi = i / 2 + 1;
        if (xi == mana[i]) maxi(ma, xi * 2);
      }
    }
    for (ll j = (ll)mana.size() - 1; j >= 0; j--) {
      ll i = mana.size() - 1 - j;
      if (i % 2 == 0) {
        ll xi = i / 2 + 1;
        if (xi == mana[j]) maxi(mi, xi * 2 - 1);
      } else {
        ll xi = i / 2 + 1;
        if (xi == mana[j]) maxi(mi, xi * 2);
      }
    }
    if (ma > mi) {
      for (ll i = now; i < now + ma; i++) {
        mae += S[i];
      }
    } else {
      for (ll i = N - 1 - now; i >= N - 1 - now - mi + 1; i--) {
        ato += S[i];
      }
    }
    reverse((ato).begin(), (ato).end());
    mae += ato;
    cout << mae << "\n";
  }
}
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}