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".
/*
 * akshaygupta26
 */
import java.io.*;
import java.util.*;
public class A
{ 
	static int prime[]= {1000000000+7,1000000000+9,1000000000+21,1000000000+33,1000000000+87};
	static int base[]= {31,53,37,39,33,35};
	static Random rand=new Random();
    public static void main(String[] args) 
	{ 
		FastReader sc=new FastReader(); 
		StringBuffer ans=new StringBuffer();
		int test=sc.nextInt();
		while(test-->0)
		{
			char str[]=sc.next().toCharArray();
			int n=str.length;
			int lp=0;int rp=n-1;
			while( lp<=rp && str[lp] == str[rp]) {
				++lp;--rp;
			}
			if(lp>rp) {
				ans.append(new String(str)+"\n");continue;
			}
			
			String s=new String(str).substring(lp,rp+1);
			
			
			
			
			String x = solve(s);
			String y = solve(reverse(s));
			
			if(x.length()>=y.length()) {
				if(lp == 0) {
					ans.append((x)+"\n");
				}
				else {
					String pre =new String(str).substring(0,lp);
					ans.append(pre+x+reverse(pre));
					ans.append("\n");				
				}
			}
			else {
				if(rp == n-1) {
					ans.append((y)+"\n");
				}
				else {
					String pre =new String(str).substring(0,lp);
					ans.append(pre+y+reverse(pre));
					ans.append("\n");				
				}
			}
					
		}
		
	    System.out.print(ans);
	} 
    
    
    static String solve(String x) {
    	
    	int xx=rand.nextInt(5);int y=rand.nextInt(6);
    	
    	int p =1000000000+33;
    	int b =39;
    	
    	int p2 =1000000000+21;
    	int b2 =31;
    	
    	int n=x.length();
    	
    	Hashing h1=new Hashing((int)n,p,b);
    	Hashing h1r=new Hashing((int)n,p,b);
    	
    	Hashing h2 =new Hashing(n,p2,b2);
    	Hashing h2r =new Hashing(n,p2,b2);
    	
		h1.precalc();
		h1.build(x);
		
		h1r.precalc();
		h1r.build(reverse(x));
		
		for(int i=n-1;i>=0;i--) {
			int hash1 =h1.getHash(0, i);
			int hash1r=h1r.getHash(n-1-i, n-1);
			int hash2 =h2.getHash(0, i);
			int hash2r=h2r.getHash(n-1-i, n-1);
			
			if( hash1 ==  hash1r && hash2 == hash2r) {
				return x.substring(0,i+1);
			}
			
		}
		return "";
    }
    
    static class Hashing{
		int N;
		int base;
		int mod;
		int pw[];
		int inv[];
		int H[];
		
		Hashing(){
		}
		
		Hashing(int N,int mod,int base){
			this.N=N;
			this.mod=mod;
			this.base=base;
			pw=new int[N];
			inv=new int[N];
			H=new int[N];
		}
		
		int add(int a, int b, int mod){
		    int res = (a + b) % mod;
		    if(res < 0)
			res += mod;
		    return res;
    		}
    	 
		int mult(int a, int b, int mod){
		    int res = (int)((((long)a) * b) % mod);
		    if(res < 0)
		        res += mod;
		    return res;
		}
		
		int power(int a, int b, int mod){
		    int res = 1;
		    while(b>0){
		        if((b % 2) == 1)
		            res = mult(res, a, mod);
		        a = mult(a, a, mod);
		        b /= 2;
		    }
		    return res;
		}
		
		void precalc() {
		    pw[0] = 1;
		    for(int i = 1; i < N; i++)
		        pw[i] = mult(pw[i - 1], base, mod);
		    
		    int pw_inv = power(base , mod - 2 , mod);
		    inv[0] = 1;
		    for(int i = 1; i < N; i++)
		        inv[i] = mult(inv[i - 1], pw_inv, mod);
		}
		
		void build(String s){
		    int n = s.length();
		    for(int i = 0; i < n ; ++i){
		        H[i] = add((i == 0) ? 0 : H[i - 1], mult(pw[i], s.charAt(i) - 'a' + 1, mod), mod);
		    }
		}
		 
		int getHash(int x , int y){
		    int res = add(H[y], (x == 0) ? 0 : -H[x - 1], mod);
		    res = mult(res , (x == 0) ? 1 : inv[x], mod);
		    return res;
		}
	}
    
    static String reverse(String x) {
    	StringBuffer res=new StringBuffer();
    	int n=x.length();
    	for(int i=n-1;i>=0;i--)res.append(x.charAt(i));
    	return res.toString();
    }
    
    static long _gcd(long a,long b) {
    	if(b == 0) return a;
    	return _gcd(b,a%b);
    }
    
    static final Random random=new Random();
    
	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	
	static void ruffleSort(long[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n);
			long temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	
	static class FastReader 
	{ 
		BufferedReader br; 
		StringTokenizer st; 

		public FastReader() 
		{ 
			br = new BufferedReader(new
					InputStreamReader(System.in)); 
		} 

		String next() 
		{ 
			while (st == null || !st.hasMoreElements()) 
			{ 
				try
				{ 
					st = new StringTokenizer(br.readLine()); 
				} 
				catch (IOException e) 
				{ 
					e.printStackTrace(); 
				} 
			} 
			return st.nextToken(); 
		} 

		int nextInt() 
		{ 
			return Integer.parseInt(next()); 
		} 

		long nextLong() 
		{ 
			return Long.parseLong(next()); 
		} 

		double nextDouble() 
		{ 
			return Double.parseDouble(next()); 
		} 

		String nextLine() 
		{ 
			String str = ""; 
			try
			{ 
				str = br.readLine(); 
			} 
			catch (IOException e) 
			{ 
				e.printStackTrace(); 
			} 
			return str; 
		} 
	} 


} 
