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.*;
import java.lang.*;
import java.math.*;
public class D {
    public static void main(String[] args) throws Exception {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        // Scanner scan = new Scanner(System.in);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int test_cases = Integer.parseInt(bf.readLine());
        next_case: for(int t=0; t<test_cases; t++) {
            String s = bf.readLine();
            int counter1 = 0;
            int counter2 = s.length()-1;
            while(counter1 < counter2) {
                if(s.charAt(counter1) == s.charAt(counter2)) {
                    counter1++;
                    counter2--;
                }
                else {
                    StringBuilder middle = new StringBuilder();
                    for(int i=counter1; i<=counter2; i++) middle.append(s.charAt(i));
                    String val = middle.toString();
                    String reverseVal = reverse(val);
                    String ans1 = longestPalindrome(val);
                    String ans2 = longestPalindrome(reverseVal);
                    if(ans1.length() > ans2.length()) {
                        StringBuilder ans = new StringBuilder();
                        ans.append(s.substring(0, counter1));
                        ans.append(ans1);
                        ans.append(s.substring(counter2+1, s.length()));
                        out.println(ans.toString());
                    }
                    else {
                        StringBuilder ans = new StringBuilder();
                        ans.append(s.substring(0, counter1));
                        ans.append(ans2);
                        ans.append(s.substring(counter2+1, s.length()));
                        out.println(ans.toString());
                    }
                    continue next_case;
                }
            }
            out.println(s);
        }
        // StringTokenizer st = new StringTokenizer(bf.readLine());
        // int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken());
        // int n = Integer.parseInt(st.nextToken());
        // int n = scan.nextInt();

        out.close(); System.exit(0);
    }
    // Exponentation
    public static int exp(int base, int e, int mod) {
      if(e == 0) return 1;
      if(e == 1) return base;
      int val = exp(base, e/2, mod);
      int ans = (int)(1L*val*val % mod);
      if(e % 2 == 1)
        ans = (int)(1L*ans*base % mod);
      return ans;
    }
    public static int inv(int base, int mod) {
      return exp(base, mod-2, mod);
    }
    public static long[] hash(String s) {
        long[] a = new long[s.length()+1];
        a[0] = 0;
        for(int i=1; i<=s.length(); i++) {
            a[i] = (a[i-1] * 12207031L + (int)(s.charAt(i-1)-'a'+2293)) % (998244353L);
        }
        return a;
    }
    public static String reverse(String s) {
        StringBuilder reversed = new StringBuilder();
        for(int i=0; i<s.length(); i++) reversed.append(s.charAt(s.length()-1-i));
        return reversed.toString();
    }
    public static String longestPalindrome(String s) {
        int cursize = 1; int begin = 0; int end = 1;

        long[] hash1 = hash(s);
       
        long[] hash2 = hash(reverse(s));
        
        for(int i=0; i<s.length()/2; i++) {
            long offset = (long)exp(12207031, i+1, 998244353);
            long mod = 998244353;
            if((hash1[i+1]+hash2[hash2.length-1-(2*i+2)]*offset % mod) % mod ==hash2[hash2.length-1-(i+1)] % mod ) {
                if(2*i+2 > cursize) {
                    cursize = 2*i+2;
                    begin = 0;
                    end = 2*i+2;
                }
            }
            if((2*i+4 <= hash2.length) && ((hash1[i+1]+ hash2[hash2.length-1-(2*i+3)]*offset % mod) % mod == hash2[hash2.length-1-(i+2)] % mod)) {
                if(2*i+3 > cursize) {
                    cursize = 2*i+3;
                    begin = 0;
                    end = 2*i+3;
                }
            }
        
        }
        return s.substring(0, end);
    }
}