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 JAVA):

Solution:
import java.util.*;
import java.io.*;

public class CFA {
  BufferedReader br;
  PrintWriter out;
  StringTokenizer st;
  boolean eof;
//  private static final long MOD = 1000L * 1000L * 1000L + 7;
  private static final long MOD = 998244353L;
  private static final int[] dx = {0, -1, 0, 1};
  private static final int[] dy = {1, 0, -1, 0};
  private static final String yes = "Yes";
  private static final String no = "No";

  void solve() {
    int T = nextInt();
//    int T = 1;
    for (int i = 0; i < T; i++) {
      helper();
    }
  }

  void helper() {
    char[] arr = nextString().toCharArray();
    int n = arr.length;
    List<Long> ls = new ArrayList<>();
    for (char c : arr) {
      ls.add(c * 1L);
    }

    int[] parLen = manacher(ls);
    Pair tmp = null;
    int res = -1;
    for (int i = 2; i < parLen.length; i++) {
      int left;
      int right;
      int p = parLen[i];
      if (p == 0) {
        continue;
      }
      if (i % 2 == 0) {
        // [(i - 2) / 2 - (p - 1) / 2, (i - 2) / 2 + (p - 1) / 2]
        left = (i - 2) / 2 - (p - 1) / 2;
        right = (i - 2) / 2 + (p - 1) / 2;
      } else {
        left = (i - 2) / 2 - p / 2 + 1;
        right = (i - 2) / 2 + 1 + p / 2 - 1;
      }
      if (left == 0) {
        if (res < right + 1) {
          // [left, right]
          res = right + 1;
          tmp = new Pair(right, n);
        }
      }
      if (right == n - 1) {
        if (res < n - 1 - left + 1) {
          res = n - 1 - left + 1;
          tmp = new Pair(-1, left);
        }
      }
    }
    boolean[] pref = new boolean[n];
    boolean valid = true;
    for (int i = 0; i < n - 1 - i; i++) {
      valid = valid && (arr[i] == arr[n - 1 - i]);
      pref[i] = valid;
    }

    int max = -1;
    for (int i = 0; i < n; i++) {
      if (pref[i]) {
        max = i;
      }
    }

    if (max != -1) {
      max++;
      // [0, max - 1] [n - max, n - 1]
      // Start from either end in [max, n - max - 1].
      for (int i = 2; i < parLen.length; i++) {
        int left;
        int right;
        int p = parLen[i];
        if (i % 2 == 0) {
          // [(i - 2) / 2 - (p - 1) / 2, (i - 2) / 2 + (p - 1) / 2]
          left = (i - 2) / 2 - (p - 1) / 2;
          right = (i - 2) / 2 + (p - 1) / 2;
        } else {
          left = (i - 2) / 2 - p / 2 + 1;
          right = (i - 2) / 2 + 1 + p / 2 - 1;
        }

        int d1 = Math.max(0, max - left);
        int d2 = Math.max(0, right - (n - max - 1));
        int d = Math.max(d1, d2);
        left += d;
        right -= d;
        int cnt = Integer.MIN_VALUE;
        if (left == max && right <= n - max - 1) {
          cnt = right + 1 + max;
          if (res < cnt) {
            res = cnt;
            tmp = new Pair(right, n - max);
          }
        } else if (right == n - max - 1 && left >= max) {
          cnt = n - 1 - left + 1 + max;
          if (res < cnt) {
            res = cnt;
            tmp = new Pair(max - 1, left);
          }
        }
      }
    }

    StringBuilder can = new StringBuilder();
    for (int i = 0; i <= tmp.start; i++) {
      can.append(arr[i]);
    }
    for (int i = tmp.end; i < n; i++) {
      can.append(arr[i]);
    }
    outln(can);
  }

  class Pair {
    int start;
    int end;
    public Pair(int start, int end) {
      this.start = start;
      this.end = end;
    }
  }

  int[] manacher(List<Long> arr) {
    int len = arr.size();
    long[] t = new long[len * 2 + 3];
    t[0] = -1;
    t[len * 2 + 2] = -2;
    for (int i = 0; i < len; i++) {
      t[2 * i + 1] = -3;
      t[2 * i + 2] = arr.get(i);
    }

    t[len * 2 + 1] = -3;
    int[] p = new int[t.length];
    int center = 0, right = 0;
    for (int i = 1; i < t.length - 1; i++) {
      int mirror = 2 * center - i;
      if (right > i) {
        p[i] = Math.min(right - i, p[mirror]);
      }

      // attempt to expand palindrome centered at i
      while (t[i + (1 + p[i])] == t[i - (1 + p[i])]) {
        p[i]++;
      }

      // if palindrome centered at i expands past right,
      // adjust center based on expanded palindrome.
      if (i + p[i] > right) {
        center = i;
        right = i + p[i];
      }
    }

//    long res = 0;
//    for (int i = 0; i < 2 * len; i++) {
//      int parLength = p[i + 2]; // longest palindrome centered at i
//      if (i % 2 == 0) {
//        res += (parLength + 1) / 2;
//      }
//      else {
//        res += parLength / 2;
//      }
//    }
    return p;
  }

  void shuffle(int[] a) {
    int n = a.length;
    for(int i = 0; i < n; i++) {
      int r = i + (int) (Math.random() * (n - i));
      int tmp = a[i];
      a[i] = a[r];
      a[r] = tmp;
    }
  }

  long gcd(long a, long b) {
    while(a != 0 && b != 0) {
      long c = b;
      b = a % b;
      a = c;
    }
    return a + b;
  }
  int gcd(int a, int b) {
    while(a != 0 && b != 0) {
      int c = b;
      b = a % b;
      a = c;
    }
    return a + b;
  }
  private void outln(Object o) {
    out.println(o);
  }
  private void out(Object o) {
    out.print(o);
  }
  private void formatPrint(double val) {
    outln(String.format("%.9f", val));
  }
  public CFA() {
    br = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(System.out);
    solve();
    out.close();
  }
  public static void main(String[] args) {
    new CFA();
  }

  public long[] nextLongArr(int n) {
    long[] res = new long[n];
    for(int i = 0; i < n; i++)
      res[i] = nextLong();
    return res;
  }
  public int[] nextIntArr(int n) {
    int[] res = new int[n];
    for(int i = 0; i < n; i++)
      res[i] = nextInt();
    return res;
  }
  public String nextToken() {
    while (st == null || !st.hasMoreTokens()) {
      try {
        st = new StringTokenizer(br.readLine());
      } catch (Exception e) {
        eof = true;
        return null;
      }
    }
    return st.nextToken();
  }
  public String nextString() {
    try {
      return br.readLine();
    } catch (IOException e) {
      eof = true;
      return null;
    }
  }
  public int nextInt() {
    return Integer.parseInt(nextToken());
  }
  public long nextLong() {
    return Long.parseLong(nextToken());
  }
  public double nextDouble() {
    return Double.parseDouble(nextToken());
  }
}