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 maxn = 2e5 + 5;
const int INF = 0x3f3f3f3f;
string Manacher(string s) {
  string ss = s;
  string t = "$#";
  for (int i = 0; i < s.size(); ++i) {
    t += s[i];
    t += "#";
  }
  vector<int> p(t.size(), 0);
  int mx = 0, id = 0, resLen = 0, resCenter = 0;
  for (int i = 1; i < t.size(); ++i) {
    p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
    while (t[i + p[i]] == t[i - p[i]]) ++p[i];
    if (mx < i + p[i]) {
      mx = i + p[i];
      id = i;
    }
    if (resLen < p[i]) {
      resLen = p[i];
      resCenter = i;
    }
  }
  int mxx = 0, k = 0;
  for (int i = 2; i <= s.size() * 2; i += 2) {
    int len = p[i] - 1;
    int dis = (len + 1) / 2;
    if (dis == i / 2) {
      if (dis > mxx) {
        mxx = dis;
        k = 0;
      }
    }
    if (dis == s.size() - i / 2 + 1) {
      if (dis > mxx) {
        mxx = dis;
        k = 1;
      }
    }
  }
  int kk = 0;
  for (int i = 3; i <= s.size() * 2; i += 2) {
    int len = p[i] - 1;
    len = len / 2;
    if (len == i / 2) {
      if (len >= mxx) {
        mxx = len;
        k = 0;
        kk = 1;
      }
    }
    if (len == s.size() - i / 2) {
      if (len >= mxx) {
        mxx = len;
        k = 1;
        kk = 1;
      }
    }
  }
  string tt;
  if (kk == 0) {
    if (!k) {
      for (int i = 0; i < mxx * 2 - 1; ++i) tt += s[i];
    } else {
      for (int i = s.size() - mxx * 2 + 1; i < s.size(); ++i) tt += s[i];
    }
  } else {
    if (!k) {
      for (int i = 0; i < mxx * 2; ++i) tt += s[i];
    } else {
      for (int i = s.size() - mxx * 2; i < s.size(); ++i) tt += s[i];
    }
  }
  return tt;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s, ss;
    cin >> s;
    int cnt = 0, l = s.size();
    for (int i = 0; i < (l + 1) / 2; ++i) {
      if (s[i] == s[l - i - 1])
        cnt++, ss += s[i];
      else
        break;
    }
    if (cnt == (l + 1) / 2) {
      cout << s << endl;
      continue;
    }
    string t;
    for (int i = cnt; i < l - cnt; ++i) t += s[i];
    ss += Manacher(t);
    for (int i = l - cnt; i < s.size(); ++i) ss += s[i];
    cout << ss << endl;
  }
  return 0;
}