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.io.PrintWriter;
import java.util.Scanner;
//https://codeforces.com/contest/1326/problem/D2
public class PrefixSuffixPalindrome_hard {
    final static int[] MOD =  {1000000007, 2117566807,1000000007,1000000037};
    final static int[] BASE = {1572872831, 1971536491,1000003,31};
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            solve(sc, pw);
        }
        pw.close();
    }

    static void solve(Scanner in, PrintWriter out){
        char[] cs = in.next().toCharArray();
        String s = new String(cs);
        int n = cs.length;
        int l = 0, r = n - 1;
        while (l < r && cs[l] == cs[r]){
            l++;r--;
        }
        if (l >= r){
            out.println(new String(cs));
        }else{
            String left = s.substring(l, r+1);
            StringHash s1 = new StringHash();
            StringHash s2 = new StringHash();
            s1.GenerateFH(left,Long.valueOf(BASE[0]),Long.valueOf(MOD[0]));
            s2.GenerateRH(left,Long.valueOf(BASE[0]),Long.valueOf(MOD[0]));
            String temp = "";
            for(int i= left.length()-1; i>=0;i--) {
                if(s1.Fhash(0, i).equals(s2.Rhash(0, i))) {
                    temp = left.substring(0,i+1);
                    break;
                }
            }
            for(int i= 0;i<left.length();i++) {
                if(s1.Fhash(i,left.length()-1) == s2.Rhash(i,left.length()-1)) {
                    if(left.substring(i,left.length()).length() > temp.length()) {
                        temp = left.substring(i,left.length());
                    }
                    break;
                }
            }
            out.println(s.substring(0, l) + temp + s.substring(r + 1, n));
        }
    }

//  credits to Moonlit
    static class StringHash{
        Long base, mod; int len;
        Long ar[], p[];

        public void GenerateFH(String s, Long b, Long m) {
            len = s.length();
            p = new Long[len];
            ar = new Long[len];
            p[0] = 1L;

            base = b;
            mod = m;
            for(int i = 1; i<len;i++) {
                p[i] = p[i-1] * base;
                p[i]%=m;
            }

            long h = 0L;
            for(int i = 0;i<len;i++) {
                h = (h*base) + s.charAt(i);
                h%=mod;
                ar[i] = h;
            }
        }

        public Long Fhash(int x,int y){
            Long h=ar[y];
            if(x>0){
                h=(h-p[y-x+1]*ar[x-1])%mod;
                if(h<0) h+=mod;
            }
            return h;
        }

        public void GenerateRH(String s, Long b, Long m) {
            len = s.length();
            p = new Long[len+1];
            ar = new Long[len+1];
            p[0] = Long.valueOf(1);
            base = b;
            mod = m;

            for(int i = 1; i<len;i++) {
                p[i] = p[i-1] * base;
                p[i]%=m;
            }

            Long h = 0L;

            for(int i = len-1;i>=0;i--) {
                h = (h*base) + s.charAt(i);
                h%=mod;
                ar[i] = h;
            }
        }

        public long Rhash(int x, int y){
            Long h=ar[x];
            if(y<len-1){
                h=(h-p[y-x+1] *ar[y+1])%mod;
                if(h<0) h+=mod;
            }
            return h;
        }
    }
}