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 maxn = 1e6 + 10, MOD = 1000000007;
string s;
long long prefix[maxn], suffix[maxn], power[maxn];
long long exp(long long int base, long long exponent) {
  long long res = 1;
  while (exponent) {
    if (exponent & 1) res = (base * res) % MOD;
    exponent = exponent / 2;
    base = (base * base) % MOD;
  }
  return res;
}
long long inverse(long long n) { return exp(n, MOD - 2); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  power[0] = 1;
  for (int i = 1; i < maxn - 5; i++) power[i] = (power[i - 1] * 101LL) % MOD;
  while (t--) {
    cin >> s;
    int n = (int)s.size();
    if (n == 1) {
      cout << s << "\n";
      continue;
    }
    int i = -1, j = n;
    string ans, ans2, ans3;
    while (i + 1 < j) {
      if (i + 1 == j - 1)
        ans += s[++i];
      else if (s[i + 1] == s[j - 1]) {
        ans += s[++i];
        j--;
      } else
        break;
    }
    if (i + 1 == j) {
      cout << s << "\n";
      continue;
    }
    for (int i = 0; i < ans.size(); i++) {
      ans2 += ans[i];
      ans3 += ans[i];
    }
    int l = i + 1, r = j - 1;
    prefix[0] = 0;
    prefix[1] = s[0];
    for (int i = 2; i <= n; i++) {
      prefix[i] = (prefix[i - 1] + (s[i - 1] * power[i - 1]) % MOD) % MOD;
    }
    suffix[0] = 0;
    suffix[1] = s[n - 1];
    for (int i = n - 2, j = 2; i >= 0 and j <= n; i--, j++) {
      suffix[j] = (suffix[j - 1] + (s[i] * power[j - 1]) % MOD) % MOD;
    }
    int id1 = l;
    for (int i = l; i <= r; i++) {
      long long hash1 =
          ((prefix[i + 1] - prefix[l] + MOD) % MOD * inverse(power[l])) % MOD;
      long long hash2 = ((suffix[n - l] - suffix[n - i - 1] + MOD) % MOD *
                         inverse(power[n - i - 1])) %
                        MOD;
      if (hash1 == hash2) {
        id1 = i;
      }
    }
    int id2 = r;
    for (int i = r; i >= l; i--) {
      long long hash1 =
          ((prefix[r + 1] - prefix[i] + MOD) % MOD * inverse(power[i])) % MOD;
      long long hash2 = ((suffix[n - i] - suffix[n - r - 1] + MOD) % MOD *
                         inverse(power[n - r - 1])) %
                        MOD;
      if (hash1 == hash2) {
        id2 = i;
      }
    }
    for (int i = l; i <= id1; i++) ans2 += s[i];
    for (int i = id2; i <= r; i++) ans3 += s[i];
    for (int i = (int)ans.size() - 1; i >= 0; i--) {
      ans2 += ans[i];
      ans3 += ans[i];
    }
    if ((int)ans2.size() > (int)ans3.size())
      cout << ans2 << "\n";
    else
      cout << ans3 << "\n";
  }
  return 0;
}