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;
struct num {
  static const int MA = 1e9 + 7, MB = 1e9 + 9;
  int a, b;
  num() : a(0), b(0) {}
  num(int x) : a(x), b(x) {}
  num(int a, int b) : a(a), b(b) {}
  num operator+(const num &x) const {
    return num((a + x.a) % MA, (b + x.b) % MB);
  }
  num operator-(const num &x) const {
    return num((a + MA - x.a) % MA, (b + MB - x.b) % MB);
  }
  num operator*(int x) const {
    return num(((long long)a * x) % MA, ((long long)b * x) % MB);
  }
  num operator*(const num &x) const {
    return num(((long long)a * x.a) % MA, ((long long)b * x.b) % MB);
  }
  bool operator==(const num &x) const { return a == x.a && b == x.b; }
  explicit operator long long() const { return (long long)a * MB + b + 1; }
};
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const num P = 293 + (rnd() % (int)(5e8));
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int n = s.size();
    int len = 0;
    while (len < n - len - 1 && s[len] == s[n - len - 1]) {
      ++len;
    }
    string pref, suf;
    for (int i = 0; i < len; ++i) {
      pref += s[i];
    }
    for (int i = n - 1; i >= n - len; --i) {
      suf += s[i];
    }
    string t;
    for (int i = len; i < n - len; ++i) {
      t += s[i];
    }
    s = t;
    n = s.size();
    vector<num> deg(n + 1);
    vector<num> h(n + 1), rh(n + 1);
    deg[0] = 1;
    for (int i = 0; i < n; ++i) {
      deg[i + 1] = deg[i] * P;
    }
    for (int i = 0; i < n; ++i) {
      h[i + 1] = h[i] * P + s[i];
    }
    for (int i = n - 1; i >= 0; --i) {
      rh[i] = rh[i + 1] * P + s[i];
    }
    auto is_pal = [&](int l, int r) {
      int len = r - l + 1;
      num h1 = h[l + len] - h[l] * deg[len];
      num h2 = rh[l] - rh[l + len] * deg[len];
      return h1 == h2;
    };
    int max_pref = 0, max_suf = 0;
    for (int i = 0; i < n; ++i) {
      if (is_pal(0, i) && i + 1 > max_pref + max_suf) {
        max_pref = i + 1;
        max_suf = 0;
      }
    }
    for (int i = n - 1; i >= 0; --i) {
      if (is_pal(i, n - 1) && n - i > max_pref + max_suf) {
        max_pref = 0;
        max_suf = n - i;
      }
    }
    for (int i = 0; i < max_pref; ++i) pref += s[i];
    for (int i = n - 1; i >= n - max_suf; --i) suf += s[i];
    reverse(suf.begin(), suf.end());
    cout << pref << suf << '\n';
  }
}