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 p = 53;
const long long mod = 2000000011;
map<long long, set<long long> > ind;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long q;
  cin >> q;
  for (long long i = 0; i < q; ++i) {
    string s;
    cin >> s;
    long long l = 0, r = s.size() - 1;
    string ans;
    while (l < r && s[l] == s[r]) {
      ans += s[l];
      l += 1;
      r -= 1;
    }
    string st;
    long long lk = l;
    while (lk <= r) {
      st += s[lk];
      lk += 1;
    }
    string stroka = st;
    reverse(st.begin(), st.end());
    string stroka2 = st;
    stroka2 += stroka;
    stroka += st;
    long long l1 = 0, r1 = 0;
    vector<long long> z(stroka.size(), 0);
    for (long long j = 1; j < stroka.size(); ++j) {
      if (j <= r1) {
        z[j] = min(z[j - l1], r1 - j + 1);
      }
      while (j + z[j] < stroka.size() && stroka[j + z[j]] == stroka[z[j]]) {
        z[j] += 1;
      }
      if (j + z[j] - 1 > r1) {
        r1 = j + z[j] - 1;
        l1 = j;
      }
    }
    string ans2;
    long long y = z.size() / 2;
    while (y < z.size()) {
      if (y + z[y] == z.size()) {
        while (y < z.size()) {
          ans2 += stroka[y];
          y += 1;
        }
        break;
      }
      y += 1;
    }
    string v1 = ans;
    v1 += ans2;
    reverse(ans.begin(), ans.end());
    v1 += ans;
    long long l2 = 0, r2 = 0;
    vector<long long> z2(stroka2.size(), 0);
    for (long long j = 1; j < stroka.size(); ++j) {
      if (j <= r2) {
        z2[j] = min(z2[j - l2], r2 - j + 1);
      }
      while (j + z2[j] < stroka2.size() &&
             stroka2[j + z2[j]] == stroka2[z2[j]]) {
        z2[j] += 1;
      }
      if (j + z2[j] - 1 > r2) {
        r2 = j + z2[j] - 1;
        l2 = j;
      }
    }
    string ans3;
    y = z.size() / 2;
    while (y < z.size()) {
      if (y + z2[y] == z2.size()) {
        while (y < z2.size()) {
          ans3 += stroka2[y];
          y += 1;
        }
        break;
      }
      y += 1;
    }
    string v2 = ans;
    v2 = ans3 + ans;
    reverse(ans.begin(), ans.end());
    v2 = ans + v2;
    if (v2.size() > v1.size()) {
      cout << v2 << "\n";
    } else {
      cout << v1 << "\n";
    }
  }
}