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 std::bitset;
using std::cerr;
using std::cin;
using std::cout;
using std::deque;
using std::endl;
using std::fill;
using std::fixed;
using std::ios_base;
using std::iterator;
using std::map;
using std::max_element;
using std::min_element;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::reverse;
using std::set;
using std::setprecision;
using std::sort;
using std::stable_sort;
using std::stack;
using std::string;
using std::swap;
using std::unique;
using std::vector;
long long min(long long a, long long b) { return a < b ? a : b; }
long long min(int a, long long b) { return a < b ? a : b; }
long long min(long long a, int b) { return a < b ? a : b; }
long long min(int a, int b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
long long max(int a, long long b) { return a > b ? a : b; }
long long max(long long a, int b) { return a > b ? a : b; }
long long max(int a, int b) { return a > b ? a : b; }
namespace MySpace {};
const long long N = 1510200;
const long long MOD = 998244353;
long long n, k;
long long dd[N], d[N];
void solve() {
  string s;
  cin >> s;
  fill(dd, dd + s.size(), 0);
  fill(d, d + s.size(), 0);
  long long L = 0, R = s.size() - 1;
  string c = "";
  while (L < R && s[L] == s[R]) {
    c += s[L];
    L++, R--;
  }
  string g = "";
  for (long long i = L; i <= R; i++) {
    g += s[i];
  }
  if (g.size() == 0) {
    cout << c;
    reverse((c).begin(), (c).end());
    cout << c << "\n";
    return;
  }
  long long l = 0, r = -1;
  for (long long i = 1; i < g.size(); i++) {
    long long k = 0;
    if (i <= r) k = min(r - i + 1, dd[r - i + l + 1]);
    while (i + k < g.size() && i - k - 1 >= 0 && g[i - k - 1] == g[i + k]) k++;
    dd[i] = k;
    if (i + k - 1 > r) {
      l = i - k;
      r = i + k - 1;
    }
  }
  l = 0, r = -1;
  for (long long i = 1; i < g.size(); i++) {
    long long k = 0;
    if (i <= r) k = min(r - i, d[r - i + l]);
    while (i + k + 1 < g.size() && i - k - 1 >= 0 &&
           g[i - k - 1] == g[i + k + 1])
      k++;
    d[i] = k;
    if (i + k > r) {
      l = i - k;
      r = i + k;
    }
  }
  long long anspref = 0, anssuff = 0;
  for (long long i = 0; i < g.size(); i++) {
    if (d[i] == i) {
      anspref = max(anspref, 2 * d[i] + 1);
    }
    if (i > 0 && dd[i] == i) {
      anspref = max(anspref, 2 * dd[i]);
    }
  }
  for (long long i = 0; i < g.size(); i++) {
    if (i + d[i] == g.size() - 1) {
      anssuff = max(anssuff, 2 * d[i] + 1);
    }
    if (i + dd[i] == g.size()) {
      anssuff = max(anssuff, 2 * dd[i]);
    }
  }
  if (anspref > anssuff) {
    for (long long i = 0; i < c.size(); i++) cout << c[i];
    for (long long i = 0; i < anspref; i++) cout << g[i];
    for (long long i = 0; i < c.size(); i++) cout << c[c.size() - 1 - i];
    cout << "\n";
  } else {
    for (long long i = 0; i < c.size(); i++) cout << c[i];
    for (long long i = g.size() - anssuff; i < g.size(); i++) cout << g[i];
    for (long long i = 0; i < c.size(); i++) cout << c[c.size() - 1 - i];
    cout << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}