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;
char _buf[100000], *_op(_buf), *_ed(_buf);
template <typename T>
void in_s(T& x) {
  x = 0;
  int f = 1;
  char ch = (_op == _ed && (_ed = (_op = _buf) + fread(_buf, 1, 100000, stdin),
                            _op == _ed)
                 ? EOF
                 : *_op++);
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = (_op == _ed && (_ed = (_op = _buf) + fread(_buf, 1, 100000, stdin),
                         _op == _ed)
              ? EOF
              : *_op++);
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = (_op == _ed && (_ed = (_op = _buf) + fread(_buf, 1, 100000, stdin),
                         _op == _ed)
              ? EOF
              : *_op++);
  }
  x *= f;
}
template <>
void in_s(string& t) {
  cin >> t;
}
void cinput() {}
template <typename First, typename... Args>
void cinput(First& f, Args&... args) {
  in_s(f);
  cinput(args...);
}
int N;
char s[1000000];
bool match(int a, int b, int c, int d) {
  int len = b - a + 1;
  for (int i = 0; i < len; ++i) {
    if (s[a + i] != s[d - i]) return false;
  }
  return true;
}
void print(int a, int b) {
  for (int i = a; i < b + 1; ++i) printf("%c", s[i]);
  printf("\n");
}
void debug(int a, int b) {}
bool isPalind(string& s) {
  int sz = s.size();
  for (int i = 0; i < sz / 2; ++i) {
    if (s[i] != s[sz - 1 - i]) {
      return false;
    }
  }
  return true;
}
string solve_slow() {
  int sz = strlen(s);
  string ans;
  for (int i = 0; i < sz; ++i) {
    for (int j = i + 1; j < sz; ++j) {
      string ts;
      for (int a = 0; a < i; ++a) ts += s[a];
      for (int a = j + 1; a < sz; ++a) ts += s[a];
      if (isPalind(ts) && ts.size() > ans.size()) {
        ans = ts;
      }
    }
  }
  return ans;
}
string solve() {
  int leftMax = 0, sz = strlen(s);
  vector<int> sum(sz);
  for (int i = 0; i < sz / 2; ++i) {
    if (s[i] == s[sz - 1 - i])
      leftMax++;
    else
      break;
  }
  sum[0] = s[0];
  for (int i = 1; i < sz; ++i) {
    sum[i] = sum[i - 1] + s[i];
  }
  if (leftMax * 2 == sz || leftMax * 2 == sz - 1) {
    printf("%s\n", s);
    string ans;
    for (int i = 0; i < sz; ++i) ans += s[i];
    return ans;
  }
  int leftB, rightB;
  for (int i = sz / 2 - 1; i >= leftMax; i--) {
    int j = sz - i - 1;
    int cnt = i - leftMax + 1;
    if (sum[i] - sum[leftMax] + s[leftMax] == sum[i + cnt] - sum[i]) {
      if (match(leftMax, i, i + 1, i + cnt)) {
        debug(leftMax, i);
        leftB = i + cnt + 1;
        rightB = sz - leftMax - 1;
        break;
      }
    }
    if (sum[j - 1] - sum[j - cnt] + s[j - cnt] ==
        sum[j + cnt - 1] - sum[j - 1]) {
      if (match(j - cnt, j - 1, j, j + cnt - 1)) {
        debug(j - cnt, j - 1);
        leftB = leftMax;
        rightB = j - 1 - cnt;
        break;
      }
    }
    cnt = i - leftMax;
    if (cnt) {
      if (sum[i - 1] - sum[leftMax] + s[leftMax] == sum[i + cnt] - sum[i]) {
        if (match(leftMax, i - 1, i + 1, i + cnt)) {
          debug(leftMax, i - 1);
          leftB = i + cnt + 1;
          rightB = sz - leftMax - 1;
          break;
        }
      }
      if (sum[j - 1] - sum[j - cnt] + s[j - cnt] == sum[j + cnt] - sum[j]) {
        if (match(j - cnt, j - 1, j + 1, j + cnt)) {
          debug(j - cnt, j - 1);
          leftB = leftMax;
          rightB = j - 1 - cnt;
          break;
        }
      }
    } else {
      leftB = i + 1;
      rightB = sz - leftMax - 1;
    }
  }
  string ans;
  for (int i = 0; i < leftB; ++i) {
    printf("%c", s[i]);
    ans += s[i];
  }
  for (int i = rightB + 1; i < sz; ++i) {
    printf("%c", s[i]);
    ans += s[i];
  }
  printf("\n");
  return ans;
}
int main() {
  scanf("%d\n", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%s", s);
    string ans1 = solve();
  }
}