Problem: This is the hard 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 ≤ 10^5), 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 10^6.

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;
typedef std::priority_queue<long long int, std::vector<long long int>,
                            std::greater<long long int> >
    min_pq;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  std::cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
inline string inttos(long long int a) {
  std::stringstream soso;
  soso << a;
  return soso.str();
}
inline long long int stoint(const std::string& s) {
  std::stringstream soso;
  soso << s;
  long long x;
  soso >> x;
  return x;
}
inline std::string inttobin(long long int x) {
  return std::bitset<40>(x).to_string();
}
inline void swp(long long int& a, long long int& b) {
  auto temp = a;
  a = b;
  b = temp;
}
long long int b1 = 4391;
long long int b2 = 1741;
long long int m = 1000000009;
const long long int N = 1000006;
long long int ph1[N], sh1[N], po1[N];
long long int ph2[N], sh2[N], po2[N];
bool ispalin1(long long int l, long long int r, long long int n) {
  long long int x1 = (ph1[r] - ph1[l] + m) % m;
  long long int x2 = (sh1[l] - sh1[r] + m) % m;
  if (l < n - r) {
    x1 = x1 * po1[n - r - l] % m;
  } else {
    x2 = x2 * po1[l - n + r] % m;
  }
  if (x1 == x2)
    return true;
  else {
    return false;
  }
}
bool ispalin2(long long int l, long long int r, long long int n) {
  long long int x1 = (ph2[r] - ph2[l] + m) % m;
  long long int x2 = (sh2[l] - sh2[r] + m) % m;
  if (l < n - r) {
    x1 = x1 * po2[n - r - l] % m;
  } else {
    x2 = x2 * po2[l - n + r] % m;
  }
  if (x1 == x2)
    return true;
  else {
    return false;
  }
}
void solve() {
  string s;
  cin >> s;
  long long int lo = 0, hi = (long long int)s.size() - 1;
  while (lo <= hi && s[lo] == s[hi]) {
    lo++, hi--;
  }
  long long int n = (long long int)s.size();
  if (lo > hi) {
    cout << s << "\n";
    return;
  }
  po1[0] = 1;
  po2[0] = 1;
  for (long long int i = 0; i < n; i++) {
    ph1[i + 1] = (ph1[i] + m + (s[i] - 'a' + 1) * po1[i]) % m;
    po1[i + 1] = po1[i] * b1 % m;
    ph2[i + 1] = (ph2[i] + m + (s[i] - 'a' + 1) * po2[i]) % m;
    po2[i + 1] = po2[i] * b2 % m;
  }
  for (long long int i = n - 1; i >= 0; i--) {
    sh1[i] = (sh1[i + 1] + m + (s[i] - 'a' + 1) * po1[n - 1 - i]) % m;
    sh2[i] = (sh2[i + 1] + m + (s[i] - 'a' + 1) * po2[n - 1 - i]) % m;
  }
  long long int maxi1 = -1;
  long long int till1;
  for (long long int i = lo; i < hi; i++) {
    if (ispalin1(lo, i + 1, n) && ispalin2(lo, i + 1, n)) {
      maxi1 = max(maxi1, i + 1 - lo);
      till1 = i + 1;
    }
  }
  long long int maxi2 = -1;
  long long int till2;
  for (long long int i = hi; i >= lo; i--) {
    if (ispalin1(i, hi + 1, n) && ispalin2(i, hi + 1, n)) {
      maxi2 = max(maxi2, hi + 1 - i);
      till2 = i;
    }
  }
  if (maxi1 >= maxi2)
    cout << s.substr(0, till1) + s.substr(hi + 1) << "\n";
  else {
    cout << s.substr(0, lo) + s.substr(till2) << "\n";
  }
  for (long long int i = 0; i <= s.length(); i++)
    ph1[i] = sh1[i] = ph2[i] = sh2[i] = 0;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  cin >> t;
  for (long long int i = 0; i < t; i++) {
    solve();
  }
}