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;
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int bin(long int a[], long int low, long int high, long d, long day[]) {
  if (low > high) return -1;
  long mid = (low + high) / 2;
  if (a[mid] > (a[low] + d) && (a[mid - 1] <= (a[low] + d) || day[mid - 1]) &&
      day[mid] == 0)
    return mid;
  else if (a[mid] > (a[low] + d) && day[mid] == 0)
    return bin(a, low, mid - 1, d, day);
  else
    return bin(a, mid + 1, high, d - a[mid + 1] + a[low], day);
}
long long power(long long a, long long b) {
  if (b == 0) return 1;
  if (b % 2 == 0) return (power((a * a), b / 2));
  return ((a) * (power((a * a), b / 2)));
}
bool sortbysecdesc(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first > b.first;
}
void lcs(string X, string Y, int m, int n) {
  int L[m + 1][n + 1];
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 || j == 0)
        L[i][j] = 0;
      else if (X[i - 1] == Y[j - 1])
        L[i][j] = L[i - 1][j - 1] + 1;
      else
        L[i][j] = max(L[i - 1][j], L[i][j - 1]);
    }
  }
  int index = L[m][n];
  char lcs[index + 1];
  lcs[index] = '\0';
  int i = m, j = n;
  while (i > 0 && j > 0) {
    if (X[i - 1] == Y[j - 1]) {
      lcs[index - 1] = X[i - 1];
      i--;
      j--;
      index--;
    } else if (L[i - 1][j] > L[i][j - 1])
      i--;
    else
      j--;
  }
  cout << lcs;
}
int main() {
  int t = 1;
  cin >> t;
  for (int X = 1; X <= t; X++) {
    string s, t, a, b, ans1, ans2;
    cin >> s;
    int i, k, n = s.size(), m, j;
    for (i = 0; i < n / 2; i++) {
      if (s[i] != s[n - i - 1]) break;
    }
    k = i;
    if (i) cout << s.substr(0, i);
    if (n > (2 * i)) t = s.substr(i, n - 2 * i);
    a = t;
    reverse((a).begin(), (a).end());
    b = a;
    a = t + '@' + a;
    b = b + '#' + t;
    m = a.size();
    int pi[m];
    pi[0] = 0;
    for (i = 1; i < m; i++) {
      j = pi[i - 1];
      while (j > 0 && a[j] != a[i]) {
        j = pi[j - 1];
      }
      if (a[i] == a[j]) j++;
      pi[i] = j;
    }
    ans1 = a.substr(0, pi[m - 1]);
    for (i = 1; i < m; i++) {
      j = pi[i - 1];
      while (j > 0 && b[j] != b[i]) {
        j = pi[j - 1];
      }
      if (b[i] == b[j]) j++;
      pi[i] = j;
    }
    ans2 = b.substr(0, pi[m - 1]);
    if ((int)ans1.size() > (int)ans2.size())
      cout << ans1;
    else {
      cout << ans2;
    }
    if (k)
      cout << s.substr(n - k, k) << "\n";
    else
      cout << "\n";
  }
  return 0;
}