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 long long p1 = 1000000007, p2 = 1000000009;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    char s[1000010];
    scanf("%s", s);
    int len = strlen(s);
    int cont = 0;
    while ((s[cont] == s[len - cont - 1]) && (cont < len - cont - 1)) cont++;
    int tamIda = 0, tamVolta = 0;
    if (cont > len - cont - 1)
      printf("%s\n", s);
    else {
      int inicio = cont, fim = len - cont - 1;
      long long alfa1 = 1, alfa2 = 1;
      long long ida1 = 0, volta1 = 0, ida2 = 0, volta2 = 0;
      for (int i = inicio; i <= fim; i++) {
        ida1 = (alfa1 * (s[i] - 'a') + ida1) % p1;
        volta1 = (27 * volta1 + s[i] - 'a') % p1;
        ida2 = (alfa2 * (s[i] - 'a') + ida2) % p2;
        volta2 = (27 * volta2 + (s[i] - 'a')) % p2;
        if ((ida1 % p1) == (volta1 % p1) && (ida2 % p2) == (volta2 % p2))
          tamIda = i - inicio + 1;
        alfa1 = (alfa1 * 27) % p1;
        alfa2 = (alfa2 * 27) % p2;
      }
      alfa1 = 1;
      alfa2 = 1;
      ida1 = 0;
      volta1 = 0;
      ida2 = 0;
      volta2 = 0;
      for (int i = fim; i >= inicio; i--) {
        ida1 = (alfa1 * (s[i] - 'a') + ida1) % p1;
        volta1 = (27 * volta1 + (s[i] - 'a')) % p1;
        ida2 = (alfa2 * (s[i] - 'a') + ida2) % p2;
        volta2 = (27 * volta2 + (s[i] - 'a')) % p2;
        if ((ida1 % p1) == (volta1 % p1) && (ida2 % p2) == (volta2 % p2))
          tamVolta = fim - i + 1;
        alfa1 = (alfa1 * 27) % p1;
        alfa2 = (alfa2 * 27) % p2;
      }
      if (tamIda >= tamVolta) {
        for (int i = 0; i < cont; i++) printf("%c", s[i]);
        for (int i = cont; i < cont + tamIda; i++) printf("%c", s[i]);
        for (int i = len - cont; i < len; i++) printf("%c", s[i]);
        printf("\n");
      } else {
        for (int i = 0; i < cont; i++) printf("%c", s[i]);
        for (int i = fim - tamVolta + 1; i < fim + 1; i++) printf("%c", s[i]);
        for (int i = len - cont; i < len; i++) printf("%c", s[i]);
        printf("\n");
      }
    }
  }
}