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 infi = 1e9;
const long long int infl = 1e18;
const int MOD = 1e9 + 7;
long long int log_2(long long int n) {
  return (n > 1ll) ? 1ll + log_2(n / 2) : 0ll;
}
inline void add(long long int &a, long long int b) {
  a += b;
  if (a >= MOD) a -= MOD;
}
inline void sub(long long int &a, long long int b) {
  a -= b;
  if (a < 0) a += MOD;
}
inline string IntToString(long long int a) {
  char x[100];
  sprintf(x, "%lld", a);
  string s = x;
  return s;
}
inline long long int StringToInt(string a) {
  char x[100];
  long long int res;
  strcpy(x, a.c_str());
  sscanf(x, "%lld", &res);
  return res;
}
inline string GetString(void) {
  char x[1000005];
  scanf("%s", x);
  string s = x;
  return s;
}
inline string uppercase(string s) {
  int n = (int)s.size();
  for (int(i) = (0); (i) < (n); ++(i))
    if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}
inline string lowercase(string s) {
  int n = (int)s.size();
  for (int(i) = (0); (i) < (n); ++(i))
    if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}
inline void OPEN(string s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
void showarr(long long int arr[], long long int n) {
  int i;
  for (i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  cout << endl;
}
void showvec(vector<long long int> v) {
  int i;
  for (i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}
int isPalindrome(string str) {
  int l = 0;
  int h = str.length() - 1;
  while (h > l) {
    if (str[l++] != str[h--]) {
      return 0;
    }
  }
  return 1;
}
void showvecpair(vector<pair<long long int, long long int> > v) {
  for (int i = 0; i < (int)v.size(); i++) {
    cout << v[i].first << " " << v[i].second << endl;
  }
}
bool sortbysec(const pair<long long int, long long int> &a,
               const pair<long long int, long long int> &b) {
  return (a.second < b.second);
}
string maxpal(string s) {
  int i;
  char ch = s[0];
  for (i = s.length() - 1; i >= 0; i--) {
    if (s[i] == s[0])
      if (isPalindrome(s.substr(0, i + 1))) return s.substr(0, i + 1);
  }
}
void solve() {
  long long int l, n, i, j, max = INT_MIN, min = INT_MAX, ctr = 0, cnt = 0,
                            cnt1 = 0, cnt2 = 0;
  string s;
  cin >> s;
  string str1, str2, str = "", strr;
  n = s.length();
  if (n == 1) {
    cout << s << endl;
  } else {
    for (i = 0; i < n; i++) {
      if (i < (n - 1 - i)) {
        if (s[i] == s[n - 1 - i]) {
          cnt++;
        } else {
          break;
        }
      }
    }
    str1 = s.substr(cnt, n - 2 * cnt);
    str2 = str1;
    reverse(str2.begin(), str2.end());
    str = str1 + "#" + str2;
    strr = str2 + "#" + str1;
    vector<long long int> pi(str.length() + 1, 0), pir(str.length() + 1, 0);
    pi[0] = 0;
    pir[0] = 0;
    for (i = 1; i < str.length(); i++) {
      j = pi[i - 1];
      while (j > 0 && str[i] != str[j]) j = pi[j - 1];
      if (str[i] == str[j]) {
        j++;
      }
      pi[i] = j;
    }
    for (i = 1; i < strr.length(); i++) {
      j = pir[i - 1];
      while (j > 0 && strr[i] != strr[j]) j = pir[j - 1];
      if (strr[i] == strr[j]) {
        j++;
      }
      pir[i] = j;
    }
    if (pi[str.length() - 1] > pir[str.length() - 1]) {
      if (pi[str.length() - 1] == 0) {
        for (i = 0; i < cnt + pi[str.length() - 1]; i++) {
          cout << s[i];
        }
        if (cnt < n) cout << s[i];
        for (i = n - cnt; i < n; i++) cout << s[i];
        cout << endl;
      } else {
        for (i = 0; i < cnt + pi[str.length() - 1]; i++) {
          cout << s[i];
        }
        for (i = n - cnt; i < n; i++) cout << s[i];
        cout << endl;
      }
    } else {
      if (pir[str.length() - 1] == 0) {
        for (i = 0; i < cnt + pir[str.length() - 1]; i++) {
          cout << s[i];
        }
        if (2 * cnt < n) cout << s[i];
        for (i = n - cnt; i < n; i++) cout << s[i];
        cout << endl;
      } else {
        for (i = 0; i < cnt; i++) {
          cout << s[i];
        }
        for (i = n - cnt - pir[str.length() - 1]; i < n; i++) cout << s[i];
        cout << endl;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, T;
  cin >> T;
  for (i = 1; i <= T; i++) {
    solve();
  }
  return 0;
}