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;
inline long long gcd(long long a, long long b) {
  while (b != 0) {
    long long c = a % b;
    a = b;
    b = c;
  }
  return a < 0 ? -a : a;
}
inline long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
inline long long lowbit(long long x) { return x & (-x); }
const double PI = 3.14159265358979323846;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long mod = 1e9 + 7;
inline char nc() {
  static char buf[1 << 21], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long rd() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const double eps = 1e-6;
const int M = 1e6 + 10;
const int N = 1e6 + 10;
unsigned long long p[N] = {1};
unsigned long long f1[N], f2[N];
unsigned long long gethash1(int l, int r) {
  if (l > r) return 0;
  return (f1[r] % mod - f1[l - 1] * p[r - l + 1] % mod + mod) % mod;
}
unsigned long long gethash2(int l, int r) {
  if (l > r) return 0;
  return (f2[l] % mod - f2[r + 1] * p[r - l + 1] % mod + mod) % mod;
}
char s[N];
int main() {
  for (int i = 1; i <= 1e6; i++) p[i] = p[i - 1] * 131 % mod;
  int T = rd();
  while (T--) {
    scanf("%s", s + 1);
    int n = strlen(s + 1);
    f2[n + 1] = 0;
    for (int i = 1, j = n; i <= n; j--, i++) {
      f1[i] = (f1[i - 1] * 131 + s[i] - 'a' + 1) % mod;
      f2[j] = (f2[j + 1] * 131 + s[j] - 'a' + 1) % mod;
    }
    int maxx = 1;
    int mid = (n + 1) >> 1;
    for (int i = 1; i <= mid; i++) {
      if (2 * i <= n && gethash1(1, i) == gethash2(i + 1, 2 * i))
        maxx = max(maxx, 2 * i);
      if (2 * i - 1 <= n && gethash1(1, i - 1) == gethash2(i + 1, 2 * i - 1))
        maxx = max(maxx, 2 * i - 1);
    }
    string ans = "";
    for (int i = 1; i <= maxx; i++) ans += s[i];
    int minn = n;
    for (int i = n; i >= mid; i--) {
      if (2 * i - n - 1 >= 1 &&
          gethash1(2 * i - n - 1, i - 1) == gethash2(i, n))
        minn = min(minn, 2 * i - n - 1);
      if (2 * i - n >= 1 && gethash1(2 * i - n, i - 1) == gethash2(i + 1, n))
        minn = min(minn, i * 2 - n);
    }
    if (n - minn + 1 > ans.size()) {
      ans = "";
      for (int i = minn; i <= n; i++) ans += s[i];
    }
    int len = 0;
    string be = "", to = "";
    for (int i = 1, j = n; i <= (n >> 1); i++, j--) {
      if (s[i] != s[j]) break;
      be += s[i];
      len++;
    }
    for (int i = be.length() - 1; i >= 0; i--) to += be[i];
    int st = len + 1, ed = n - len;
    if (st <= ed) {
      int mid = (st + ed) >> 1;
      int maxx = st;
      for (int i = st; i <= mid; i++) {
        if (2 * i - st + 1 <= ed &&
            gethash1(st, i) == gethash2(i + 1, 2 * i - st + 1))
          maxx = 2 * i - st + 1;
        if (2 * i - st <= ed &&
            gethash1(st, i - 1) == gethash2(i + 1, 2 * i - st))
          maxx = max(maxx, 2 * i - st);
      }
      if (maxx - st + 1 + 2 * len > ans.size()) {
        ans = be;
        for (int i = st; i <= maxx; i++) ans += s[i];
        ans += to;
      }
      int minn = ed;
      for (int i = ed; i >= mid; i--) {
        if (2 * i - ed - 1 >= st &&
            gethash1(2 * i - ed - 1, i - 1) == gethash2(i, ed))
          minn = min(minn, 2 * i - ed - 1);
        if (2 * i - ed >= st &&
            gethash1(2 * i - ed, i - 1) == gethash2(i + 1, ed))
          minn = min(minn, i * 2 - ed);
      }
      if (ed - minn + 1 + len * 2 > ans.size()) {
        ans = be;
        for (int i = minn; i <= ed; i++) ans += s[i];
        ans += to;
      }
    }
    cout << ans << endl;
  }
}