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 Solution{
    public static class pair{
        int x;
        int y;
        int z;
    }
    public static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
        public String next() {
			while (st == null || !st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) {
				a[i]=nextInt();
			}
			return a;
		}public double nextDouble() {
			return Double.parseDouble(next());
		}
    }
    public static void p(String s){
        System.out.print(s+" ");
    }
    
    public static void main(String[] args) throws IOException{
        FastScanner fs=new FastScanner();
		int t=fs.nextInt();
		PrintWriter out=new PrintWriter(System.out);
		while(t-->0){
		    String s=fs.next();
		    int i=0,j=s.length()-1;
		    while(i<j&&s.charAt(i)==s.charAt(j)){
		        i++;
		        j--;
		    }
		    if(i>=j){
		        System.out.println(s);
		        continue;
		    }
		    String rem=s.substring(i,j+1);
		    String pre=go(rem);
		    StringBuffer sb=new StringBuffer(rem);
		    String suf=go(sb.reverse().toString());
		  //  System.out.println(pre+" "+suf);
		    if(pre.length()<suf.length()){
		        System.out.println(s.substring(0,i)+suf+s.substring(j+1,s.length()));
		    }else{
		        System.out.println(s.substring(0,i)+pre+s.substring(j+1,s.length()));
		    }
		}
        out.close();
    }
    public static String go(String s){
        // System.out.println(s);
        StringBuffer sb=new StringBuffer(s);
        String a=s+'#'+sb.reverse().toString();
        int[] arr=new int[a.length()];
        for(int i=1;i<arr.length;i++){
            if(a.charAt(arr[i-1])==a.charAt(i)){
                arr[i]=arr[i-1]+1;
            }else{
                int pi=arr[i-1];
                while(pi>0&&a.charAt(i)!=a.charAt(pi)){
                    pi=arr[pi-1];
                }
                if(a.charAt(pi)==a.charAt(i)){
                    arr[i]=pi+1;
                }else{
                    arr[i]=pi;
                }
            }
        }
        // if(s.equals("swtohotwssgy")){
        //     for(int i=0;i<arr.length;i++){
        //         System.out.print(a.charAt(i)+" ");
        //     }
        //     System.out.println();
        //     for(int i=0;i<arr.length;i++){
        //         System.out.print(arr[i]+" ");
        //     }
        //     System.out.println();
        // }
        int max=arr[arr.length-1];
        return s.substring(0,max);
    }
    public static long east(int l1,int r1,int l2,int r2,int n,int k){
        if(n==0){
            return Integer.MAX_VALUE;
        }
        if(l2>=l1&&r2<=r1){
		        long cost=(l2-l1)+(r1-r2);
		        if(k<=(r1-l1)){
		            return (long)k;
		        }
		        k-=(r1-l1);
		        cost=Math.min(2*k,east(l1,r1,l2,r2,n-1,k))+cost;
		        return cost;
		    }else if(r1<l2){
		        long pc=l2-r1;
		        long cost=(r1-l1)+(r2-r1);
		        if(k<=(r2-l1)){
		            return (long)k+pc;
		        }
		        k-=(r2-l1);
		        cost=Math.min(2*k,east(l1,r1,l2,r2,n-1,k))+pc+cost;
		        return cost;
		    }else{
		        long cost=(l2-l1)+(r2-r1);
		        if(k<=(r2-l1)){
		            return k;
		        }
		        k-=(r2-l1);
		        cost=Math.min(2*k,east(l1,r1,l2,r2,n-1,k))+cost;
		        return cost;
		    }
    }
    static long maxSubArraySum(int a[]) 
    { 
        int size = a.length; 
        long max_so_far = Long.MIN_VALUE, max_ending_here = 0; 
        int max=Integer.MIN_VALUE;
        for (int i = 0; i < size; i++) 
        { 
            max_ending_here = max_ending_here + a[i]; 
            if (max_so_far < max_ending_here) 
                max_so_far = max_ending_here; 
            if (max_ending_here < 0) 
                max_ending_here = 0; 
            max=Math.max(max,a[i]);
        } 
        if(max<0){
            return (long)0;
        }
        return max_so_far; 
    } 
    public static class Comp implements Comparator<pair>{
        public int compare(pair a,pair b){
            if(a.x!=b.x){
                return b.x-a.x;
            }else{
                return a.y-b.y;
            }
        }
    }
    public static long gcd(long a,long b){
        if (b == 0) 
        return a; 
        return gcd(b, a % b);
    }
    public static int lcm(int a,int b){
        int x=Math.max(a,b);
        int y=Math.min(a,b);
        long ans=x;
        while(ans%y!=0){
            ans+=x;
        }
        if(ans>Integer.MAX_VALUE){
            return -1;
        }
        return (int)ans;
    }
    public static long fact(int n){
        long ans=1;
        for(int i=1;i<=n;i++){
            ans*=i;
        }
        return ans;
    }
}