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 pow1[1000005], pow2[1000005];
inline long long binex(long long a, long long b) {
  long long ans = 1, temp = a % 1000000007;
  while (b != 0) {
    if (b & 1) ans = (ans * temp) % 1000000007;
    temp = (temp * temp) % 1000000007;
    b = b >> 1;
  }
  return ans;
}
inline string solve(string s, long long n) {
  long long p1 = 51, p2 = 41, t1 = 1, t2 = 1;
  pair<long long, long long> hash1[n], hash2[n];
  hash1[0].first = hash1[0].second = s[0];
  for (long long i = 1; i < n; i++) {
    t1 = (p1 * t1) % 1000000007;
    t2 = (t2 * p2) % 1000000007;
    hash1[i].first = (hash1[i - 1].first + t1 * s[i]) % 1000000007;
    hash1[i].second = (hash1[i - 1].second + t2 * s[i]) % 1000000007;
  }
  hash2[n - 1].first = hash2[n - 1].second = s[n - 1];
  t1 = t2 = 1;
  for (long long i = n - 2; i >= 0; i--) {
    t1 = (p1 * t1) % 1000000007;
    t2 = (t2 * p2) % 1000000007;
    hash2[i].first = (hash2[i + 1].first + t1 * s[i]) % 1000000007;
    hash2[i].second = (hash2[i + 1].second + t2 * s[i]) % 1000000007;
  }
  long long idx = 1;
  for (long long i = n - 1; i >= 2; i--) {
    long long x = i / 2, l = (i % 2 == 0) ? x : x + 1, r = i, x1 = n - r;
    pair<long long, long long> p;
    p.first = (hash2[l].first - hash2[r].first + 1000000007) % 1000000007;
    p.first = (p.first * pow1[x1]) % 1000000007;
    p.second = (hash2[l].second - hash2[r].second + 1000000007) % 1000000007;
    p.second = (p.second * pow2[x1]) % 1000000007;
    if (p == hash1[x - 1]) {
      idx = i;
      break;
    }
  }
  string ans;
  for (long long i = 0; i < idx; i++) ans.push_back(s[i]);
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long p1 = 51, p2 = 41, t1 = 1, t2 = 1;
  pow1[0] = pow2[0] = 1;
  for (long long i = 1; i < 1000005; i++) {
    t1 = (t1 * p1) % 1000000007;
    t2 = (t2 * p2) % 1000000007;
    pow1[i] = binex(t1, 1000000007 - 2);
    pow2[i] = binex(t2, 1000000007 - 2);
  }
  long long t;
  cin >> t;
  while (t--) {
    string s, ans;
    cin >> s;
    long long n = (long long)s.size(), l = 0, r = n - 1;
    while (l < r && s[l] == s[r]) {
      ans.push_back(s[l]);
      l++;
      r--;
    }
    if (r <= l) {
      cout << s << '\n';
      continue;
    }
    string s1;
    for (long long i = l; i <= r; i++) s1.push_back(s[i]);
    string t1 = solve(s1, (long long)s1.size());
    reverse(s1.begin(), s1.end());
    string t2 = solve(s1, (long long)s1.size());
    string ans1 = ans;
    if ((long long)t1.size() > (long long)t2.size())
      ans1 += t1;
    else
      ans1 += t2;
    reverse(ans.begin(), ans.end());
    ans1 += ans;
    cout << ans1 << '\n';
  }
}