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;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
template <class T>
inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T>
inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
const ll MOD = 1e9 + 7;
void manacher(const string &st, vector<int> &re) {
  re.resize(st.size());
  int i = 0, j = 0, n = st.size();
  while (i < st.size()) {
    while (i - j >= 0 && i + j < n && st[i - j] == st[i + j]) ++j;
    re[i] = j;
    int k = 1;
    while (i - k >= 0 && i + k < st.size() && k + re[i - k] < j)
      re[i + k] = re[i - k], ++k;
    i += k;
    j = max(0, j - k);
  }
}
string solve(const string &s) {
  int n = s.size();
  {
    auto t = s;
    reverse((t).begin(), (t).end());
    if (t == s) return s;
  }
  int len = 0;
  for (int l = 0, r = n - 1; l < r && s[l] == s[r]; ++l, --r) ++len;
  vector<int> man;
  {
    string t(n * 2 + 1, '.');
    for (int(i) = 0; (i) < (n); ++(i)) t[i * 2 + 1] = s[i];
    manacher(t, man);
  }
  pii re(0, 0);
  for (int(c) = 0; (c) < (man.size()); ++(c)) {
    const int l = (c - 1 - man[c]) / 2 + 1, r = (c + man[c]) / 2 - 1;
    if (l > r) continue;
    pii mn(MOD, 0);
    if (l <= len) {
      const int ln = len + len + c - len * 2;
      MN(mn, pii(ln, ln - len));
    }
    if (n - 1 - len <= r) {
      const int ln = len + len + (n - len) * 2 - c;
      MN(mn, pii(ln, len));
    }
    if (mn.first != MOD) MX(re, mn);
  }
  int l = re.second;
  int r = re.first - l;
  return s.substr(0, l) + s.substr(n - r);
}
string bf(string s, string res) {
  int n = s.size();
  int ok = 0;
  string re;
  for (int(i) = 0; (i) < (n + 1); ++(i))
    for (int(j) = i; (j) < (n + 1); ++(j)) {
      string t = s.substr(0, i) + s.substr(j);
      if (t == res) ok = 1;
      auto tt = t;
      reverse((tt).begin(), (tt).end());
      if (t == tt && re.size() < t.size()) re = t;
    }
  if (!ok) return "";
  return re;
}
int main() {
  for (int(_) = 0; (_) < (0 * 10000); ++(_)) {
    int n = rand() % 10 + 1;
    string s(n, '0');
    for (int(i) = 0; (i) < (n); ++(i))
      if (rand() % 2) s[i]++;
    auto act = solve(s);
    auto exp = bf(s, act);
    if (act.size() != exp.size()) {
      ;
      exit(0);
    }
  }
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(0);
  int T;
  cin >> T;
  while (T--) {
    string s;
    cin >> s;
    cout << solve(s) << "\n";
  }
  return 0;
}