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;
vector<long long> HASHlist;
vector<vector<long long> > pow_;
void pow_ini() {
  pow_.clear();
  pow_.resize(10);
  for (int(i) = 0; (i) < (10); (i)++) {
    pow_[i].clear();
    pow_[i].push_back(HASHlist[i]);
    for (int(j) = 0; (j) < (62); (j)++) {
      long long nxt = pow_[i][j] * pow_[i][j];
      nxt %= 1000000007LL;
      pow_[i].push_back(nxt);
    }
  }
  return;
}
long long pow1(int i, long long b) {
  long long ans = 1;
  for (int(n) = 0; (n) < (62); (n)++) {
    if ((b >> n) % 2 == 1) {
      ans *= pow_[i][n];
      ans %= 1000000007LL;
    }
  }
  return ans;
}
class Rolling_hash {
 public:
  int AN, BN;
  vector<vector<long long> > hash_A;
  vector<vector<long long> > hash_B;
  vector<vector<long long> > hash_sum_A;
  vector<vector<long long> > hash_sum_B;
  vector<vector<long long> > pow_HASH_inv;
  void h_ini_from_array(vector<long long> A, vector<long long> B);
  void h_ini_from_string(string A, string B);
  vector<long long> hash_val_A(int Ai, int len);
  vector<long long> hash_val_B(int Bi, int len);
  bool same(int Ai, int Bi, int len);
};
void Rolling_hash::h_ini_from_array(vector<long long> A, vector<long long> B) {
  hash_A.resize(10);
  hash_B.resize(10);
  hash_sum_A.resize(10);
  hash_sum_B.resize(10);
  pow_HASH_inv.resize(10);
  HASHlist.resize(10);
  HASHlist[0] = 999999937LL;
  HASHlist[1] = 999999929LL;
  HASHlist[2] = 999999893LL;
  HASHlist[3] = 999999883LL;
  HASHlist[4] = 999999797LL;
  HASHlist[5] = 999999761LL;
  HASHlist[6] = 999999757LL;
  HASHlist[7] = 999999751LL;
  HASHlist[8] = 999999739LL;
  HASHlist[9] = 999999733LL;
  AN = A.size();
  BN = B.size();
  pow_ini();
  for (int(i) = 0; (i) < (10); (i)++) {
    hash_A[i].resize(AN);
    hash_B[i].resize(BN);
    hash_sum_A[i].resize(AN + 1);
    hash_sum_B[i].resize(BN + 1);
    hash_sum_A[i][0] = 0;
    hash_sum_B[i][0] = 0;
    long long hashtmp = 1;
    for (int(j) = 0; (j) < (AN); (j)++) {
      hash_A[i][j] = A[j] * hashtmp;
      hash_A[i][j] %= 1000000007LL;
      hash_sum_A[i][j + 1] = hash_sum_A[i][j] + hash_A[i][j];
      hash_sum_A[i][j + 1] %= 1000000007LL;
      hashtmp *= HASHlist[i];
      hashtmp %= 1000000007LL;
    }
    hashtmp = 1;
    for (int(j) = 0; (j) < (BN); (j)++) {
      hash_B[i][j] = B[j] * hashtmp;
      hash_B[i][j] %= 1000000007LL;
      hash_sum_B[i][j + 1] = hash_sum_B[i][j] + hash_B[i][j];
      hash_sum_B[i][j + 1] %= 1000000007LL;
      hashtmp *= HASHlist[i];
      hashtmp %= 1000000007LL;
    }
    int powN = max(AN, BN);
    pow_HASH_inv[i].resize(powN);
    pow_HASH_inv[i][powN - 1] = pow1(i, 1000000007LL - (long long)powN);
    for (int j = powN - 2; j >= 0; j--) {
      pow_HASH_inv[i][j] = pow_HASH_inv[i][j + 1] * HASHlist[i];
      pow_HASH_inv[i][j] %= 1000000007LL;
    }
  }
  return;
}
void Rolling_hash::h_ini_from_string(string A, string B) {
  AN = A.length();
  BN = B.length();
  vector<long long> Avec(AN);
  vector<long long> Bvec(BN);
  for (int(i) = 0; (i) < (AN); (i)++) {
    Avec[i] = (long long)A[i] - (long long)'a' + 1;
  }
  for (int(i) = 0; (i) < (BN); (i)++) {
    Bvec[i] = (long long)B[i] - (long long)'a' + 1;
  }
  h_ini_from_array(Avec, Bvec);
  return;
}
vector<long long> Rolling_hash::hash_val_A(int Aj, int len) {
  vector<long long> hash_val(10);
  for (int(i) = 0; (i) < (10); (i)++) {
    long long hash_tmp = hash_sum_A[i][Aj + len] - hash_sum_A[i][Aj];
    hash_tmp += 1000000007LL;
    hash_tmp *= pow_HASH_inv[i][Aj];
    hash_tmp %= 1000000007LL;
    hash_val[i] = hash_tmp;
  }
  return hash_val;
}
vector<long long> Rolling_hash::hash_val_B(int Bj, int len) {
  vector<long long> hash_val(10);
  for (int(i) = 0; (i) < (10); (i)++) {
    long long hash_tmp = hash_sum_B[i][Bj + len] - hash_sum_B[i][Bj];
    hash_tmp += 1000000007LL;
    hash_tmp *= pow_HASH_inv[i][Bj];
    hash_tmp %= 1000000007LL;
    hash_val[i] = hash_tmp;
  }
  return hash_val;
}
bool Rolling_hash::same(int Ai, int Bi, int len) {
  return (hash_val_A(Ai, len) == hash_val_B(Bi, len));
}
int main() {
  int T;
  cin >> T;
  for (int(t) = 0; (t) < (T); (t)++) {
    int N;
    string S;
    cin >> S;
    N = S.length();
    int L = 0;
    int R = N / 2 + 1;
    Rolling_hash hashlist;
    string Sinv = S;
    reverse(Sinv.begin(), Sinv.end());
    hashlist.h_ini_from_string(S, Sinv);
    while (1) {
      int M = (L + R) / 2;
      if (L == M) break;
      bool is_same = hashlist.same(0, 0, M);
      if (is_same)
        L = M;
      else
        R = M;
    }
    int len = L;
    int Ssublen = N - len * 2;
    string Sans1 = "";
    if (Ssublen > 0) {
      string Ssub = S.substr(len, Ssublen);
      string Ssubinv = Ssub;
      reverse(Ssubinv.begin(), Ssubinv.end());
      Rolling_hash hash_sub;
      hash_sub.h_ini_from_string(Ssub, Ssubinv);
      int ans1, ans2;
      string S1, S2;
      for (ans1 = Ssublen; ans1 >= 0; ans1--) {
        if (hash_sub.same(0, Ssublen - ans1, ans1)) {
          S1 = Ssub.substr(0, ans1);
          break;
        }
      }
      for (ans2 = Ssublen; ans2 >= 0; ans2--) {
        if (hash_sub.same(Ssublen - ans2, 0, ans2)) {
          S2 = Ssub.substr(Ssublen - ans2, ans2);
          break;
        }
      }
      Sans1 = (ans1 > ans2) ? S1 : S2;
    }
    string Sans0 = S.substr(0, len);
    string Sans2 = S.substr(N - len, len);
    string ansans = Sans0 + Sans1 + Sans2;
    cout << ansans << endl;
  }
  return 0;
}