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;
const int MX = 1000005;
int pal[2 * MX], t[2 * MX];
vector<int> izq[2 * MX], der[2 * MX];
void manacher(string &s) {
  int n = s.size(), m = 2 * n - 1;
  for (int i = 0; i < m; i++) pal[i] = 0, t[i] = -1;
  for (int i = 0; i < n; i++) t[2 * i] = s[i];
  int x = 0;
  for (int i = 1; i < m; i++) {
    int &r = pal[i];
    if (i <= x + pal[x]) r = min(pal[2 * x - i], x + pal[x] - i);
    while (i - r - 1 >= 0 && i + r + 1 < m && t[i - r - 1] == t[i + r + 1]) r++;
    if (i + r >= x + pal[x]) x = i;
  }
  for (int i = 0; i < m; i++)
    if (i - pal[i] == 0 || i + pal[i] == m - 1) pal[i]++;
  for (int i = 0; i < m; i++) {
    pal[i] /= 2;
    pal[i] = pal[i] + pal[i] + 1 - i % 2;
  }
}
int n;
string S;
void solve() {
  cin >> S;
  manacher(S);
  n = S.size();
  for (int i = 0; i < 2 * n - 1; i++) {
    izq[i].clear();
    der[i].clear();
  }
  for (int i = 0; i < 2 * n - 1; i++) {
    if (!pal[i]) continue;
    if (i < (2 * n - 1) / 2) {
      izq[i - pal[i] + 1].push_back(i);
    } else {
      der[i + pal[i] - 1].push_back(i);
    }
  }
  int p = -1, s = 2 * n, res = 1, ind = 0, f = 0, off = 1;
  for (int i = 0, j = 2 * n - 2; i <= j; i += 2, j -= 2) {
    for (int k : izq[i]) p = max(p, k);
    for (int k : der[j]) s = min(s, k);
    int x = 0;
    if (p >= i) {
      if (p % 2)
        x = max(x, (p - i + 1));
      else
        x = max(x, (p - i) + 1);
    }
    if (i + x > res) {
      res = i + x;
      ind = i;
      f = 0;
      off = x;
    }
    x = 0;
    if (s <= j) {
      if (s % 2)
        x = max(x, j - s + 1);
      else
        x = max(x, (j - s) + 1);
    }
    if (i + x > res) {
      res = i + x;
      ind = i;
      f = 1;
      off = x;
    }
    if (S[i / 2] != S[j / 2]) break;
  }
  string r;
  for (int i = 0; i < ind / 2; i++) r.push_back(S[i]);
  if (!f) {
    for (int i = 0; i < off; i++) r.push_back(S[ind / 2 + i]);
  }
  reverse(S.begin(), S.end());
  string aux;
  for (int i = 0; i < ind / 2; i++) aux.push_back(S[i]);
  if (f) {
    for (int i = 0; i < off; i++) aux.push_back(S[ind / 2 + i]);
  }
  reverse(aux.begin(), aux.end());
  r.insert(r.end(), aux.begin(), aux.end());
  cout << r << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}