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.*;
import java.util.*;

public class D {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int t = sc.nextInt();
		PrintWriter pw = new PrintWriter(System.out);
		while(t-- > 0) {
			char[] s = sc.next().toCharArray();
			int n = s.length;
			if(n == 1) {
				pw.println(s); continue;
			}
			char[] ss = new char[2*n-1];
			for(int i = 0; i < n; i++) {
				ss[2*i] = s[i];
			}
			int[] arr = manacher(ss);
			int[] vals = new int[n];
			for(int i = 0; i < 2*n-1; i++) {
//				if(i % 2 == 0) {
					int L = i - arr[i] + 1 == 0 ? 0 : (i - arr[i] + 1) / 2 + 1;
					int R = i + arr[i] - 1 == 2*n-2 ? n-1 : (i + arr[i] - 1) / 2;
					if(i < n) {
						vals[L] = Math.max(vals[L], L == 0 ? arr[i] : arr[i]-1);
					}
					else {
						vals[R] = Math.max(vals[R], R == n-1 ? arr[i] : arr[i]-1);
					}
//				}
//				else {
//					int L = (i - arr[i]/2) / 2 + 1;
//					int R = (i + arr[i]/2) / 2;
//				}
			}
			int maxv = 0, maxind = 0;
			for(int k = 0; k < n/2; k++) {
				int curr = 2*k + Math.max(vals[k], vals[n-k-1]);
				if(curr > maxv) {
					maxind = k;
					maxv = curr;
				}
				if(s[k] != s[n-k-1]) break;
			}
			StringBuilder sb = new StringBuilder();
			for(int i = 0; i < maxind; i++) {
				sb.append(s[i]);				
			}
			if(vals[maxind] > vals[n-maxind-1]) {
				for(int i = 0; i < vals[maxind]; i++) {
					sb.append(s[maxind+i]);
				}
			}
			else {
				for(int i = 0; i < vals[n-maxind-1]; i++) {
					sb.append(s[n-maxind-vals[n-maxind-1]+i]);
				}
			}
			for(int i = n-maxind; i < n; i++) {
				sb.append(s[i]);
			}
			pw.println(sb.toString());
		}
		
		pw.flush();

	}
	
	static int[] manacher(char[] s) { //needs the 2*n - 1 trick
		int n = s.length;
		int[] res = new int[n];
		int C = 0, R = -1, rad; 
		for (int i = 0; i < n; ++i) {
		    if (i <= R) {
		        rad = Math.min(res[2*C-i], R-i); 
		    } else {
		        rad = 0; 
		    }
		    // Try to extend
		    while (i+rad < n && i-rad >= 0 && s[i-rad] == s[i+rad]) {
		        rad++; 
		    }
		    res[i] = rad;
		    if (i + rad - 1 > R) {
		        C = i; 
		        R = i + rad - 1; 
		    }
		}
		return res;
	}
	static class FastScanner {
		public BufferedReader reader;
		public StringTokenizer tokenizer;
		public FastScanner() {
			reader = new BufferedReader(new InputStreamReader(System.in), 32768);
			tokenizer = null;
		}
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		public String nextLine() {
			try {
				return reader.readLine();
			} catch(IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

}