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.*;
import java.io.*;
 
public class Main {
    static int[][] HashsArray;
    static int HashsArrayInd=0;
	static int[] prepow;
	static int prelen=0;
	public static void PH(String s,boolean Uppercase,boolean reverse) {
		prelen=s.length();
		HashsArray[HashsArrayInd]=new int[prelen+1];
		prepow=new int[prelen];
		
		mod=1000000033;
		
		int p;
		prepow[0]=1;
		if(!reverse) {
		
		if(Uppercase) {
		p=53;
		for(int i=1;i<prelen;i++) {
			prepow[i]=(int) ((1l*prepow[i-1]*p)%mod);
			
		}
		for(int i=0;i<prelen;i++) {
			if(s.charAt(i)>='a'&&s.charAt(i)<='z')
				HashsArray[HashsArrayInd][i+1]=(int) ((1l*HashsArray[HashsArrayInd][i]+((1l*s.charAt(i)-'a'+1)*prepow[i])%mod)%mod);
			else 
				HashsArray[HashsArrayInd][i+1]=(int) ((1l*HashsArray[HashsArrayInd][i]+((1l*s.charAt(i)-'A'+27)*prepow[i])%mod)%mod);
		}
		}
		else {
		p=31;
		for(int i=1;i<prelen;i++) {
			prepow[i]=(int) ((1l*prepow[i-1]*p)%mod);
			
		}
		for(int i=0;i<prelen;i++) {
			HashsArray[HashsArrayInd][i+1]=(int) ((1l*HashsArray[HashsArrayInd][i]+((1l*s.charAt(i)-'a'+1)*prepow[i])%mod)%mod);
		}
		}
		}
		else {
			if(Uppercase) {
				p=53;
				for(int i=1;i<prelen;i++) {
					prepow[i]=(int) ((1l*prepow[i-1]*p)%mod);
					
				}
				for(int i=0;i<prelen;i++) {
					if(s.charAt(i)>='a'&&s.charAt(i)<='z')
						HashsArray[HashsArrayInd][i+1]=(int) ((1l*HashsArray[HashsArrayInd][i]+((1l*s.charAt(i)-'a'+1)*prepow[prelen-1-i])%mod)%mod);
					else 
						HashsArray[HashsArrayInd][i+1]=(int) ((1l*HashsArray[HashsArrayInd][i]+((1l*s.charAt(i)-'A'+27)*prepow[prelen-1-i])%mod)%mod);
				}
				}
				else {
				p=31;
				for(int i=1;i<prelen;i++) {
					prepow[i]=(int) ((1l*prepow[i-1]*p)%mod);
					
				}
				for(int i=0;i<prelen;i++) {
					HashsArray[HashsArrayInd][i+1]=(int) ((1l*HashsArray[HashsArrayInd][i]+((1l*s.charAt(i)-'a'+1)*prepow[prelen-1-i])%mod)%mod);
				}
				}
		}

		HashsArrayInd++;
		
	}
public static int PHV(int l,int r,int n,boolean reverse) {
	if(l>r) {
		return 0;
	}
	int val=(int) ((1l*HashsArray[n-1][r]+mod-HashsArray[n-1][l-1])%mod);
	if(!reverse) {
	    
	    val=(int) ((1l*val*prepow[prelen-l])%mod);
	}else {
		val=(int) ((1l*val*prepow[r-1])%mod);
	}
		return val;
	}
	public static void main(String[] args) throws Exception {
		int t=sc.nextInt();
		for(int i=0;i<t;i++) {
			String s=sc.next();
		           
		   
		
		if(isPal(s)) {
			pw.println(s);
		}
		else {
			HashsArrayInd=0;
			HashsArray=new int[2][];
			int j=0;
			 Queue<Character> q=new LinkedList<Character>();
			    Stack<Character> Q=new Stack<Character>();
		while(s.charAt(j)==s.charAt(s.length()-1-j)) {
			q.add(s.charAt(j));
			Q.add(s.charAt(j));
			j++;
		}
		String S=s.substring(j,s.length()-j);
		int n=S.length();
		PH(S, false, false);
		PH(S, false, true);
		int len=0;
		boolean end=false;
		for(int l=1;l<=n/2;l++) {
			if(PHV(1, l, 1, false)==PHV(l+1, 2*l, 2, true)) {
				len=2*l;
				end=false;
			}else if(PHV(n-2*l+1, n-l, 1, false)==PHV(n-l+1, n, 2, true)) {
				len=2*l;
				end=true;
			}
			else if(PHV(1, l-1, 1, false)==PHV(l+1, 2*l-1, 2, true)) {
				len=2*l-1;
				end=false;
			}
			
			else if(PHV(n-2*l+2, n-l, 1, false)==PHV(n-l+2, n, 2, true)) {
				len=2*l-1;
				end=true;
			}
		}
		while(!q.isEmpty()) {
			pw.print(q.poll());
		}
		if(end) {
			pw.print(S.substring(n-len,n));
		}else {
			pw.print(S.substring(0,len));
		}
		while(!Q.isEmpty()) {
			pw.print(Q.pop());
		}
		pw.println();
		}
		}
		
		
		pw.close();
	}
 
 
	static class Scanner {
		StringTokenizer st;
		BufferedReader br;
 
		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}
 
		public Scanner(FileReader r) {
			br = new BufferedReader(r);
		}
 
		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
 
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
 
		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}
 
		public String nextLine() throws IOException {
			return br.readLine();
		}
 
		public double nextDouble() throws IOException {
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if (x.charAt(0) == '-') {
				neg = true;
				start++;
			}
			for (int i = start; i < x.length(); i++)
				if (x.charAt(i) == '.') {
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				} else {
					sb.append(x.charAt(i));
					if (dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg ? -1 : 1);
		}
 
		public boolean ready() throws IOException {
			return br.ready();
		}
 
	}
 
	static class pair implements Comparable<pair> {
		int x;
		int y;
 
		public pair(int x, int y) {
			this.x = x;
			this.y = y;
		}
 
		public String toString() {
			return x + " " + y;
		}
 
		public int compareTo(pair other) {
			if (this.x == other.x) {
				return this.y - other.y;
			} else {
				return this.x - other.x;
			}
		}
	}
 
	static class tuble implements Comparable<tuble> {
		int x;
		int y;
		int z;
 
		public tuble(int x, int y, int z) {
			this.x = x;
			this.y = y;
			this.z = z;
		}
 
		public String toString() {
			return x + " " + y + " " + z;
		}
 
		public int compareTo(tuble other) {
			if (this.x == other.x) {
				return this.y - other.y;
			} else {
				return this.x - other.x;
			}
		}
	}
 
	public static long GCD(long a, long b) {
		if (b == 0)
			return a;
		if (a == 0)
			return b;
		return (a > b) ? GCD(a % b, b) : GCD(a, b % a);
	}
 
	public static long LCM(long a, long b) {
		return a * b / GCD(a, b);
	}
 
	static long Pow(long a, int e, int mod)	// O(log e)
    {
        a %= mod;
        long res = 1;
        while(e > 0)
        {
            if((e & 1) == 1)
                res = (res * a) % mod;
            a = (a * a) % mod;
            e >>= 1;
        }
        return res;
    }
    
    static long nc(int n,int r){
        if (n<r)return 0;
        long v = fac[n];
        v*=Pow(fac[r],mod-2,mod);
        v%=mod;
        v*=Pow(fac[n-r],mod-2,mod);
        v%=mod;
        return v;
    }
 
	public static boolean isprime(long a) {
		if (a == 0 || a == 1) {
			return false;
		}
		if (a == 2) {
			return true;
		}
		for (int i = 2; i < Math.sqrt(a) + 1; i++) {
			if (a % i == 0) {
				return false;
			}
		}
		return true;
	}
	public static boolean isPal(String s) {
		boolean t=true;
		for(int i=0;i<s.length();i++) {
			if(s.charAt(i)!=s.charAt(s.length()-1-i)) {
				t=false;
				break;
			}
		}
		return t;
	}

	static long fac[];
	static int mod=998244353;
	static Scanner sc = new Scanner(System.in);
	static PrintWriter pw = new PrintWriter(System.out);
}