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;
long long int mod1 = 1000000009;
long long int p1 = 53;
long long int mod2 = 998244353;
long long int p2 = 31;
long long int powe[1000005];
long long int pow2[1000005];
long long int hash2[1000005];
long long int hash1[1000005];
long long int rhash[1000005];
long long int rhash1[1000005];
long long int n;
bool check(int st, int l) {
  long long int k = l / 2;
  long long int v1, v10;
  if (st == 0) {
    v1 = hash1[st + k - 1] % mod1;
    v10 = hash2[st + k - 1] % mod2;
  } else {
    v1 = (hash1[st + k - 1] - hash1[st - 1] + mod1) % mod1;
    v10 = (hash2[st + k - 1] - hash2[st - 1] + mod2) % mod2;
  }
  long long int st2;
  st2 = n - (st + l);
  long long int v2, v20;
  if (st2 == 0) {
    v2 = rhash[st2 + k - 1] % mod1;
    v20 = rhash1[st2 + k - 1] % mod2;
  } else {
    v2 = (rhash[st2 + k - 1] - rhash[st2 - 1] + mod1) % mod1;
    v20 = (rhash1[st2 + k - 1] - rhash1[st2 - 1] + mod2) % mod2;
  }
  if (st < st2) {
    return (v1 * powe[st2 - st] % mod1 == v2 &&
            v10 * pow2[st2 - st] % mod2 == v20);
  } else
    return (v2 * powe[st - st2] % mod1 == v1 && v20 * pow2[st - st2] % mod2);
}
bool check1(int st, int l) {
  long long int k = l / 2;
  long long int v1, v10;
  if (st == 0) {
    v1 = rhash[st + k - 1] % mod1;
    v10 = rhash1[st + k - 1] % mod2;
  } else {
    v1 = (rhash[st + k - 1] - rhash[st - 1] + mod1) % mod1;
    v10 = (rhash1[st + k - 1] - rhash1[st - 1] + mod2) % mod2;
  }
  long long int st2;
  st2 = n - (st + l);
  long long int v2, v20;
  if (st2 == 0) {
    v2 = hash1[st2 + k - 1] % mod1;
    v20 = hash2[st2 + k - 1] % mod2;
  } else {
    v2 = (hash1[st2 + k - 1] - hash1[st2 - 1] + mod1) % mod1;
    v20 = (hash2[st2 + k - 1] - hash2[st2 - 1] + mod2) % mod2;
  }
  if (st < st2) {
    return (v1 * powe[st2 - st] % mod1 == v2 &&
            v10 * pow2[st2 - st] % mod2 == v20);
  } else
    return (v2 * powe[st - st2] % mod1 == v1 &&
            v20 * powe[st - st2] % mod2 == v10);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    powe[0] = 1;
    pow2[0] = 1;
    n = s.length();
    for (int i = 1; i < s.length(); i++) {
      powe[i] = (powe[i - 1] * p1) % mod1;
      pow2[i] = (pow2[i - 1] * p2) % mod2;
    }
    hash1[0] = (s[0] - 'a' + 1);
    hash2[0] = (s[0] - 'a' + 1);
    for (int i = 1; i < s.length(); i++) {
      hash1[i] =
          (hash1[i - 1] % mod1 + (powe[i] * (s[i] - 'a' + 1)) % mod1) % mod1;
      hash2[i] =
          (hash2[i - 1] % mod2 + (pow2[i] * (s[i] - 'a' + 1)) % mod2) % mod2;
    }
    rhash[0] = (s[n - 1] - 'a' + 1);
    rhash1[0] = (s[n - 1] - 'a' + 1);
    long long int j = 1;
    for (int i = n - 2; i >= 0; i--) {
      rhash[j] =
          (rhash[j - 1] % mod1 + (powe[j] * (s[i] - 'a' + 1)) % mod1) % mod1;
      rhash1[j] =
          (rhash1[j - 1] % mod2 + (pow2[j] * (s[i] - 'a' + 1)) % mod2) % mod2;
      j++;
    }
    int l = 0, r = n - 1;
    while (l < r) {
      if (s[l] == s[r]) {
        l++;
        r--;
      } else
        break;
    }
    if (l >= r) {
      cout << s << endl;
      continue;
    }
    long long int ansind = 0, anslen = 1;
    for (int len = 1; len <= n - 2 * l && len != n; len++) {
      if (check(l, len)) {
        if (len >= anslen) {
          anslen = len;
          ansind = l;
        }
      }
    }
    for (int len = 1; len <= n - 2 * l && len != n; len++) {
      if (check1(n - r - 1, len)) {
        if (len >= anslen) {
          anslen = len;
          ansind = n - (len + n - r - 1);
        }
      }
    }
    for (int i = 0; i < l; i++) cout << s[i];
    for (int i = ansind; i < ansind + anslen; i++) cout << s[i];
    for (int i = n - (l); i < n; i++) cout << s[i];
    cout << endl;
  }
  return 0;
}