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 LETTER = 26;
static int C = 13331;
template <int M, class B>
struct A {
  int x;
  B b;
  A(int x = 0) : x(x), b(x) {}
  A(int x, B b) : x(x), b(b) {}
  A operator+(A o) {
    int y = x + o.x;
    return {y - (y >= M) * M, b + o.b};
  }
  A operator-(A o) {
    int y = x - o.x;
    return {y + (y < 0) * M, b - o.b};
  }
  A operator*(A o) { return {(int)(1LL * x * o.x % M), b * o.b}; }
  explicit operator uint64_t() { return x ^ (uint64_t)b << 21; }
  bool operator==(const A<M, B>& rhs) const { return x == rhs.x && b == rhs.b; }
};
struct HashInterval {
  vector<A<1000000007, A<1000000009, unsigned>>> ha, pw;
  HashInterval(string& str) : ha(str.length() + 1), pw(ha) {
    pw[0] = 1;
    for (int i = 0; i < str.length(); ++i)
      ha[i + 1] = ha[i] * C + str[i], pw[i + 1] = pw[i] * C;
  }
  A<1000000007, A<1000000009, unsigned>> hashInterval(int a, int b) {
    return ha[b] - ha[a] * pw[b - a];
  }
};
const int MAXN = 5010100;
struct Data {
  int l, r;
  bool operator<(const Data& rhs) const { return r - l > rhs.r - rhs.l; }
};
Data gao(const char* s) {
  int n = strlen(s), mxok = 0;
  Data ans;
  ans.l = 0, ans.r = n;
  while (mxok < n && s[mxok] == s[n - 1 - mxok]) mxok++;
  string s1, s2;
  for (int i = mxok; i < n - mxok; ++i) s1 += s[i], s2 += s[i];
  reverse(s2.begin(), s2.end());
  HashInterval h1(s1), h2(s2);
  for (int i = 0; i < s1.length(); ++i) {
    if (h1.hashInterval(0, i + 1) ==
        h2.hashInterval(s1.length() - 1 - i, s1.length()))
      ans = max(ans, (Data){i + mxok + 1, n - mxok - 1});
  }
  return ans;
}
char s[MAXN], t[MAXN];
void solve() {
  scanf("%s", s);
  int n = strlen(s), mxok = 0;
  while (mxok < n && s[mxok] == s[n - 1 - mxok]) mxok++;
  if (mxok >= n) {
    puts(s);
    return;
  }
  for (int i = 0; i < n; ++i) t[i] = s[n - 1 - i];
  t[n] = 0;
  auto ans = gao(s);
  auto ans1 = gao(t);
  char* tt = s;
  if (ans < ans1) {
    swap(ans1, ans);
    tt = t;
  }
  if (ans.l > 0) tt[ans.l] = 0, printf("%s", tt);
  if (ans.r < n - 1) printf("%s", tt + ans.r + 1);
  printf("\n");
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}