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".
import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
	static int mod = 998244353;
	public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
	public static void main(String sp[])throws IOException{
		FastReader sc = new FastReader();
		int t = sc.nextInt();
		while(t-->0){
			String st = sc.next();
			String gh = st;
			int n = st.length();
			
			StringBuilder s1 = new StringBuilder();
			StringBuilder s2 = new StringBuilder();
			int l=0;
			int r = n-1;
			while(l<r){
				if(st.charAt(l)==st.charAt(r)){
					s1.append(st.charAt(l));
					s2.append(st.charAt(r));
					l++;
					r--;
				}else break;
			}
			
			st = st.substring(l,r+1);
			n = st.length();
			if(n==0){
				System.out.println(gh);
				continue;
			}
			StringBuilder cv = new StringBuilder(st);
			String sd = cv.reverse().toString();
			int max1 = findLongestPalindromicString(st);
			int max2 = findLongestPalindromicString(sd);
			if(max1>max2 && max1>0)
				s1.append(st.substring(0,max1));
			else if(max2>0){
				String vv = sd.substring(0,max2);
				cv = new StringBuilder(vv);
				vv = cv.reverse().toString();
				s1.append(vv);
			}
			s1.append(s2.reverse().toString());
			System.out.println(s1.toString());
			
		}
	}
	
	public static int findLongestPalindromicString(String text)  
    { 
        int N = text.length(); 
        if (N == 0) 
            return 0; 
        N = 2 * N + 1; // Position count 
        int[] L = new int[N + 1]; // LPS Length Array 
        L[0] = 0; 
        L[1] = 1; 
        int C = 1; // centerPosition 
        int R = 2; // centerRightPosition 
        int i = 0; // currentRightPosition 
        int iMirror; // currentLeftPosition 
        int maxLPSLength = 0; 
        int maxLPSCenterPosition = 0; 
        int start = -1; 
        int end = -1; 
        int diff = -1; 
  
        // Uncomment it to print LPS Length array 
        // printf("%d %d ", L[0], L[1]); 
        for (i = 2; i < N; i++)  
        { 
  
            // get currentLeftPosition iMirror  
            // for currentRightPosition i 
            iMirror = 2 * C - i; 
            L[i] = 0; 
            diff = R - i; 
  
            // If currentRightPosition i is within  
            // centerRightPosition R 
            if (diff > 0) 
                L[i] = Math.min(L[iMirror], diff); 
  
            // Attempt to expand palindrome centered at  
            // currentRightPosition i. Here for odd positions,  
            // we compare characters and if match then  
            // increment LPS Length by ONE. If even position,  
            // we just increment LPS by ONE without  
            // any character comparison 
            while (((i + L[i]) + 1 < N && (i - L[i]) > 0) &&  
                               (((i + L[i] + 1) % 2 == 0) ||  
                         (text.charAt((i + L[i] + 1) / 2) ==  
                          text.charAt((i - L[i] - 1) / 2)))) 
            { 
                L[i]++; 
            } 
  
            if (L[i] > maxLPSLength) // Track maxLPSLength 
            { 
                maxLPSLength = L[i]; 
                maxLPSCenterPosition = i; 
            } 
  
            // If palindrome centered at currentRightPosition i 
            // expand beyond centerRightPosition R, 
            // adjust centerPosition C based on expanded palindrome. 
            if (i + L[i] > R)  
            { 
                C = i; 
                R = i + L[i]; 
            } 
  
            // Uncomment it to print LPS Length array 
            // printf("%d ", L[i]); 
        } 
  
        start = (maxLPSCenterPosition - maxLPSLength) / 2; 
        end = start + maxLPSLength - 1;
		int max = 1;
		for(int x=1;x<=N;x++){
			if(x-L[x]==0){
				max = Math.max(max, L[x]);
			}
		}
		return max;
	}
	
	public static class pair{
		int val;
		int index;
	}
	
	public static class comp implements Comparator<pair>{
		public int compare(pair o1, pair o2){
			return o2.val-o1.val;
		}
	}
	
	
	
	static int parent[];
	public static int find(int a){
		if(parent[a]==a)
			return a;
		int rep = find(parent[a]);
		parent[a]=rep;
		return rep;
	}
	public static void join(int a, int b){
		int f1 = find(a);
		int f2 = find(b);
		if(f1==f2)
			return;
		
		if(f1<f2)
			parent[f2] = f1;
		else parent[f1] = f2;
	}
	
	
	public static class FastReader {
		BufferedReader br;
		StringTokenizer st;
	
 
		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
 
		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception r) {
					r.printStackTrace();
				}
			}
			return st.nextToken();
		}
 
		int nextInt() {
			return Integer.parseInt(next());//converts string to integer
		}
 
		double nextDouble() {
			return Double.parseDouble(next());
		}
 
		long nextLong() {
			return Long.parseLong(next());
		}
 
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (Exception r) {
				r.printStackTrace();
			}
			return str;
		}
	}
}
