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>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
using namespace std;
struct UF {
  vector<long long int> e;
  UF(int n) : e(n, -1) {}
  bool same_set(int a, int b) { return find(a) == find(b); }
  int size(int x) { return -e[find(x)]; }
  int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
  void join(int a, int b) {
    a = find(a), b = find(b);
    if (a == b) return;
    if (e[a] > e[b]) swap(a, b);
    e[a] += e[b];
    e[b] = a;
  }
};
long long int p = 1e9 + 7;
long long int power_(long long int a, long long int b) {
  if (!b) return 1;
  long long int ans = power_(a, b / 2);
  ans = (ans * ans) % p;
  if (b % 2) ans = (ans * a) % p;
  return ans;
}
class string_ : public string {
 public:
  string s;
  vector<long long int> hash_arr;
  vector<long long int> z_arr;
  vector<long long int> pre_arr;
  vector<long long int> pre_cal;
  string_(string s1) { s = s1; }
  void z_array() {
    int n = (int)s.length();
    vector<long long int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
      if (i <= r) z[i] = min((r - i + 1) * 1ll, z[i - l]);
      while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
      if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
    }
    z_arr = z;
  }
  void pre_array() {
    int n = (int)s.length();
    vector<long long int> pi(n);
    for (int i = 1; i < n; i++) {
      int j = pi[i - 1];
      while (j > 0 && s[i] != s[j]) j = pi[j - 1];
      if (s[i] == s[j]) j++;
      pi[i] = j;
    }
    pre_arr = pi;
  }
  long long compute_hash() {
    const int p = 31;
    const int m = 1e9 + 7;
    long long hash_value = 0;
    long long p_pow = 1;
    for (char c : s) {
      hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
      p_pow = (p_pow * p) % m;
    }
    return hash_value;
  }
  void compute_hash_array() {
    const int p = 31;
    vector<long long int> arr;
    const int m = 1e9 + 7;
    long long hash_value = 0;
    long long p_pow = 1;
    for (char c : s) {
      hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
      arr.push_back(hash_value);
      p_pow = (p_pow * p) % m;
    }
    hash_arr = arr;
  }
  void cal_pre() {
    int n = s.size();
    long long int p = 31, a = 1;
    long long int m = 1e9 + 7;
    for (int i = 0; i < n + 1; i++) {
      pre_cal.push_back(power_(a, m - 2));
      a = (a * p) % m;
    }
  }
  long long int hash_substr(int l, int r) {
    long long int m = 1e9 + 7;
    if (l == 0) {
      return hash_arr[r];
    } else {
      long long int a = (hash_arr[r] - hash_arr[l - 1] + m) % m;
      a = (a * pre_cal[l]) % m;
      return a;
    }
  }
  vector<pair<int, int>> mana_() {
    vector<pair<int, int>> temp(s.size(), {0, 0});
    int n = s.size();
    for (int i = 0, l = 0, r = -1; i < n; i++) {
      int k = (i > r) ? 1 : min(temp[l + r - i].first, r - i + 1);
      while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) {
        k++;
      }
      temp[i].first = k--;
      if (i + k > r) {
        l = i - k;
        r = i + k;
      }
    }
    for (int i = 0, l = 0, r = -1; i < n; i++) {
      int k = (i > r) ? 0 : min(temp[l + r - i + 1].second, r - i + 1);
      while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) {
        k++;
      }
      temp[i].second = k--;
      if (i + k > r) {
        l = i - k - 1;
        r = i + k;
      }
    }
    return temp;
  }
};
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int t;
  cin >> t;
  while (t--) {
    int a, b, c, d, e = 0, f;
    string s1;
    cin >> s1;
    string_ te(s1);
    int n = s1.size();
    int i = 0, j = n - 1;
    while (i < n && j >= 0) {
      if (s1[i] == s1[j]) {
        i++;
        j--;
      } else
        break;
    }
    if (i >= j) {
      cout << te.s << endl;
      continue;
    } else {
      string s2 = s1.substr(i, j - i + 1);
      string_ s3(s2);
      vector<pair<int, int>> temp = s3.mana_();
      c = 0;
      d = 0;
      for (int i = 0; i < s3.s.size(); i++) {
        a = temp[i].first;
        b = temp[i].second;
        if (a - 1 == i) {
          c = max(c, a + (a - 1));
        }
        if (b == i) {
          c = max(c, b + b);
        }
        if (i + a - 1 == s3.s.size() - 1) {
          d = max(d, a + a - 1);
        }
        if (i + b == s3.s.size()) {
          d = max(d, 2 * b);
        }
      }
      string ans = "";
      if (c >= d)
        i += c;
      else {
        j -= d;
      }
      for (int k = 0; k < i; k++) {
        ans += s1[k];
      }
      for (int k = j + 1; k < n; k++) {
        ans += s1[k];
      }
      cout << ans << endl;
    }
  }
}