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


public class  Main{
	static char[]in;
	static int[][] manacher(char[] s)
	{
		int n = s.length, d1[] = new int[n], d2[] = new int[n];

		//Calculation of odd-length palindromes
		for(int i = 0, l = 0, r = -1; i < n; ++i)
		{
			int k = i > r ? 1 : Math.min(r - i + 1, d1[l + r - i]);
			while(i + k < n && i - k >= 0 && s[i + k] == s[i - k])
				++k;
			d1[i] = k--;
			if(i + k > r) { l = i - k; r = i + k; }
		}
		
		//Calculation of even-length palindromes
		//you may insert # between characters of s to get rid of the following part
		for(int i = 0, l = 0, r = -1; i < n; ++i)
		{
			int k = i > r ? 0 : Math.min(r - i + 1, d2[l + r - i + 1]);
			while(i + k < n && i - k - 1 >= 0 && s[i + k] == s[i - k - 1])
				++k;
			d2[i] = k--;
			if(i + k > r) { l = i - k - 1; r = i + k; }
		}
		
		return new int[][] { d1, d2 };
	}
	
	static StringBuilder best(char[] c) {
		int[][]d=manacher(c);
		
		
		int max=0,idx=-1;boolean odd=true;
		
		for(int i=0;i<d[0].length;i++) {
			if(i-d[0][i]+1==0 || i+d[0][i]-1==c.length-1) {
				int len=(d[0][i])*2-1;
				if(len>max) {
					max=len;
					idx=i;
					odd=true;
				}
				
			}
		}
		
		for(int i=0;i<d[1].length;i++) {
			if(i-d[1][i]==0 || i+d[1][i]-1==c.length-1) {
				int len=d[1][i]*2;
				if(len>max) {
					max=len;
					idx=i;
					odd=false;
				}
			}
		}
		StringBuilder ans=new StringBuilder();
		if(odd) {
			for(int i=idx-d[0][idx]+1;i<=idx+d[0][idx]-1;i++) {
				ans.append(c[i]);
			}
		}
		else {
			for(int i=idx-d[1][idx];i<=idx+d[1][idx]-1;i++) {
				ans.append(c[i]);
			}
		}
		
		
		return ans;
	}
	
	
	public static void main(String[] args) throws Exception{
		MScanner sc=new MScanner(System.in);
		PrintWriter pw=new PrintWriter(System.out);
		int tc=sc.nextInt();
		while(tc-->0) {
			in=sc.nextLine().toCharArray();
			int[]ans=new int[2];
			ans[0]=-1;
			ans[1]=in.length;
			int max=1;
			int n=in.length;
			
			int s=0,e=n-1;
			StringBuilder mid=new StringBuilder();
			while(s<=e) {
				if(in[s]!=in[e]) {
					
					StringBuilder sb=new StringBuilder();
					
					for(int i=s,j=e;i<=e;i++,j--) {
						sb.append(in[i]);
					}
					
					
					mid=best(sb.toString().toCharArray());

					
					break;
				}
				ans[0]=s;ans[1]=e;
				int len=n-e-1+s;
				max=len;
				if(s==e) {
					ans[0]--;
				}
				s++;e--;
			}
			
			
			for(int i=0;i<=ans[0];i++) {
				pw.print(in[i]);
			}
			pw.print(mid);
			for(int i=ans[1];i<n;i++) {
				pw.print(in[i]);
			}
			pw.println();
		}
		pw.flush();
	}
	static class MScanner {
		StringTokenizer st;
		BufferedReader br;
		public MScanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}
 
		public MScanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}
 
		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int[] intArr(int n) throws IOException {
	        int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
	        return in;
		}
		public long[] longArr(int n) throws IOException {
	        long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
	        return in;
		}
		public int[] intSortedArr(int n) throws IOException {
	        int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
	        shuffle(in);
	        Arrays.sort(in);
	        return in;
		}
		public long[] longSortedArr(int n) throws IOException {
	        long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
	        shuffle(in);
	        Arrays.sort(in);
	        return in;
		}
		static void shuffle(int[]in) {
			for(int i=0;i<in.length;i++) {
				int idx=(int)(Math.random()*in.length);
				int tmp=in[i];
				in[i]=in[idx];
				in[idx]=tmp;
			}
		}
		static void shuffle(long[]in) {
			for(int i=0;i<in.length;i++) {
				int idx=(int)(Math.random()*in.length);
				long tmp=in[i];
				in[i]=in[idx];
				in[idx]=tmp;
			}
		}
		public Integer[] IntegerArr(int n) throws IOException {
	        Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt();
	        return in;
		}
		public Long[] LongArr(int n) throws IOException {
	        Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong();
	        return in;
		}
		public String nextLine() throws IOException {
			return br.readLine();
		}
 
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
 
		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}
 
		public char nextChar() throws IOException {
			return next().charAt(0);
		}
 
		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}
 
		public boolean ready() throws IOException {
			return br.ready();
		}
 
		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
	static void addX(int[]in,int x) {
		for(int i=0;i<in.length;i++)in[i]+=x;
	}
	static void addX(long[]in,int x) {
		for(int i=0;i<in.length;i++)in[i]+=x;
	}
}