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;
const int N = 1e6 + 10, mod = 1e9 + 7, P = 727;
int h[N], h2[N], n, pw[N];
vector<int> st[N], ft[N];
inline int get1(int l, int r) {
  if (l == 0) return h[r - 1];
  int ret = h[r - 1] - 1ll * h[l - 1] * pw[r - l] % mod + mod;
  if (ret >= mod) ret -= mod;
  return ret;
}
inline int get2(int l, int r) {
  if (l == n - 1) return h2[r + 1];
  int ret = h2[r + 1] - 1ll * h2[l + 1] * pw[l - r] % mod + mod;
  if (ret >= mod) ret -= mod;
  return ret;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  pw[0] = 1;
  for (int i = 1; i < N; i++) pw[i] = 1ll * pw[i - 1] * P % mod;
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    n = s.length();
    for (int i = 0; i < n; i++) {
      st[i].clear();
      ft[i].clear();
    }
    h[0] = s[0];
    for (int i = 1; i < n; i++) h[i] = (1ll * h[i - 1] * P + s[i]) % mod;
    h2[n - 1] = s[n - 1];
    for (int i = n - 2; i >= 0; i--) h2[i] = (1ll * h2[i + 1] * P + s[i]) % mod;
    for (int i = 0; i < n; i++) {
      int l = 0, r = min(i, n - i - 1) + 1;
      while (r - l > 1) {
        int m = (l + r) / 2;
        if (get1(i - m, i) == get2(i + m, i))
          l = m;
        else
          r = m;
      }
      st[i - l].push_back(2 * l + 1);
      ft[i + l].push_back(2 * l + 1);
      if (i == n - 1 || s[i] != s[i + 1]) continue;
      l = 0, r = min(i, n - i - 2) + 1;
      while (r - l > 1) {
        int m = (l + r) / 2;
        if (get1(i - m, i + 1) == get2(i + 1 + m, i))
          l = m;
        else
          r = m;
      }
      st[i - l].push_back(2 * (l + 1));
      ft[i + 1 + l].push_back(2 * (l + 1));
    }
    int mx = 0;
    set<int> s1, s2;
    int rem1 = 0, rem2 = 0;
    int ans = 0;
    pair<int, int> ret = {-1, -1};
    for (int i = 0; i <= n / 2; i++) {
      if (i && s[i - 1] != s[n - i]) break;
      int mxlen = n - 2 * i;
      for (int x : st[i]) s1.insert(x + rem1);
      while (s1.size() && *s1.begin() <= rem1) s1.erase(s1.begin());
      while (s1.size() && *s1.rbegin() - rem1 > mxlen) {
        auto it = s1.end();
        it--;
        s1.erase(it);
      }
      int add = 0;
      if (s1.size()) add = *s1.rbegin() - rem1;
      if (add + 2 * i > ans) {
        ans = add + 2 * i;
        ret = {i + (*s1.rbegin() - rem1) - 1, n - i};
      }
      for (int x : ft[n - i - 1]) s2.insert(x + rem2);
      while (s2.size() && *s2.begin() <= rem2) s2.erase(s2.begin());
      while (s2.size() && *s2.rbegin() - rem2 > mxlen) {
        auto it = s2.end();
        it--;
        s2.erase(it);
      }
      add = 0;
      if (s2.size()) add = *s2.rbegin() - rem2;
      if (add + 2 * i > ans) {
        ans = add + 2 * i;
        ret = {i - 1, n - i - (*s2.rbegin() - rem2)};
      }
      rem1 += 2;
      rem2 += 2;
    }
    for (int i = 0; i <= ret.first; i++) cout << s[i];
    for (int i = ret.second; i < n; i++) cout << s[i];
    cout << "\n";
  }
  return 0;
}