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.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;

public class global7d1 {
	public static void main(String[] args) {
		POW=new long[1000001][MOD.length];
		
		for(int i=0;i<1000001;i++) {
			for(int j=0;j<3;j++) {
				if(i==0) POW[i][j]=1;
				else POW[i][j]=(POW[i-1][j]*BASE)%MOD[j];
			}
		}
		Scanner scan=new Scanner(System.in);
		int t=scan.nextInt();
		
		for(int tt=0;tt<t;tt++) {
			int[] a=toarr(scan.next());
			int n=a.length;
			
			//find largest prefix such that the suffix matches it backwards
			
			StringBuilder start=new StringBuilder();
			int l=0, r=n-1;
			while(l<r&&a[l]==a[r]) {
				start.append(c(a[l]));
				l++;
				r--;
			}
			if(l>r) {
				System.out.print(start);
				System.out.println(start.reverse());
				continue;
			}
			
			//find the largest prefix of the substring from l to r, inclusive, that is a palindrome
			
			int fres=0, bres=0;
			hash forward=new hash(), backward=new hash();
			for(int i=l;i<=r;i++) {
				forward.addLast(a[i]);
				backward.addFirst(a[i]);
				if(forward.equals(backward)) fres=i-l+1;
			}
			
			//reverse it and do the same
			forward=new hash();
			backward=new hash();
			for(int i=r;i>=l;i--) {
				forward.addLast(a[i]);
				backward.addFirst(a[i]);
				if(forward.equals(backward)) bres=r-i+1;
			}
			
			StringBuilder middle=new StringBuilder();
			if(fres>bres) {
				for(int x=0;x<fres;x++) middle.append(c(a[l+x]));
			}
			else {
				for(int x=0;x<bres;x++) middle.append(c(a[r-x]));
			}
			System.out.print(start);
			System.out.print(middle);
			System.out.println(start.reverse());
		}
	}
	public static int[] toarr(String s) {
		char[] c=s.toCharArray();
		int[] a=new int[c.length];
		for(int i=0;i<c.length;i++) a[i]=(int)(c[i]-'a'+1);
		return a;
	}
	public static char c(int x) {
		return (char)('a'+x-1);
	}
	static class hash {
		ArrayDeque<Integer> word;
		long[] val;
		int len;

		hash() {
			word=new ArrayDeque<>();
			val=new long[MOD.length];
			len=0;
		}
		void addLast(int x) {
			for(int i=0;i<MOD.length;i++) {
				val[i]=mult(val[i],BASE,MOD[i]);
				val[i]=add(val[i],x,MOD[i]);
			}
			word.addLast(x);
			len++;
		}
		void pollFirst() {
			if(len==0) return;
			int poll=word.pollFirst();
			for(int i=0;i<MOD.length;i++) {
				val[i]=sub(val[i],poll*POW[len-1][i],MOD[i]);
			}
			len--;
		}
		void addFirst(int x) {
			len++;
			for(int i=0;i<MOD.length;i++) {
				val[i]=add(val[i],x*POW[len-1][i],MOD[i]);
			}
			word.addFirst(x);
			//			System.out.println(val[0]);
		}
		void pollLast() {
			if(len==0) return;
			int poll=word.pollLast();
			//			System.out.println("polling "+poll);
			for(int i=0;i<MOD.length;i++) {
				val[i]=sub(val[i],poll,MOD[i]);
				val[i]=mult(val[i],MODINV[i],MOD[i]);
			}
			//			System.out.println(val[0]);
			len--;
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + len;
			result = prime * result + Arrays.hashCode(val);
			return result;
		}
		@Override
		public boolean equals(java.lang.Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			hash other = (hash) obj;
			if (len != other.len)
				return false;
			if (!Arrays.equals(val, other.val))
				return false;
			return true;
		}
	}

	static final int[] MOD={(int)1e9+7,(int)1e9+9,(int)1e9+21};
	static final int[] MODINV={370370373,296296299,407407416};
	static final int BASE=27;
	static long[][] POW;
	
	public static long mult(long a, long b, long mod) {
		return ((a%mod)*(b%mod))%mod;
	}
	public static long add(long a, long b, long mod) {
		return ((a%mod)+(b%mod))%mod;
	}
	public static long sub(long a, long b, long mod) {
		long res=(a%mod)-(b%mod);
		while(res<0) res+=mod;
		return res;
	}
}