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.util.*;
import java.io.*;
public class Q1 
{
	private static String go(String s)
	{
        int n = s.length();
        String kmp = s + "#" + (new StringBuilder(s).reverse().toString());
 
        int m = kmp.length();
        int[] lps = new int[m];
        for(int i=1; i<m; i++) {
            int pi = lps[i-1];
            while(pi > 0 && kmp.charAt(i) != kmp.charAt(pi)) {
                pi = lps[pi - 1];
            }
            lps[i] = pi + (kmp.charAt(i)==kmp.charAt(pi)?1:0);
        }
        int l = lps[m-1];
        return s.substring(0, l);
    }
	
	public static void main(String args[]) throws Exception 
	{
		Scan scan = new Scan();
		Print print = new Print();
		int T = scan.scanInt();
		while (T-- > 0) 
		{
			String string=scan.scanString();
			int N=string.length();
			int max=0;
			int i=0;
			loop:
			for(;i<N/2;i++)
			{
				if(string.charAt(i)==string.charAt(N-i-1))
				{
					//System.out.println(string.charAt(i));
					max++;
				}
				else
				{
					break loop;
				}
			}
			if(max==N/2)
			{
				print.println(string);
			}
			else
			{
				String temp=string.substring(max,N-max);
				String pall=go(temp);
				String temp_rev=new StringBuilder(temp).reverse().toString();
				String pall1=go(temp_rev);
				if(pall.length()>=pall1.length())
				{
					print.println(string.substring(0,max)+pall+string.substring(N-max));
				}
				else
				{
					print.println(string.substring(0,max)+pall1+string.substring(N-max));
				}
				/*i=0;
				int j=temp.length()-1;
				int pall=0;
				while(i<j)
				{
					//System.out.println(temp.charAt(i));
					if(temp.charAt(i)==temp.charAt(j))
					{
						pall+=2;
						i++;
					}
					else
					{
						i=0;
						pall=0;
					}
					j--;
				}
				pall+=((i==j)?1:0);
				
				int pall1=0;
				i=0;
				j=temp.length()-1;
				while(i<j)
				{
					if(temp_rev.charAt(i)==temp_rev.charAt(j))
					{
						pall1+=2;
						i++;
					}
					else
					{
						i=0;
						pall1=0;
					}
					j--;
				}
				pall1+=((i==j)?1:0);
				//System.out.println(pall1);
				if(pall>=pall1)
				{
					//System.out.println("HERE"+max);
					print.println(string.substring(0,max+pall)+string.substring(N-max));
				}
				else
				{
					
					print.println(string.substring(0,max)+string.substring(N-max-pall1));
				}*/
			}
		}
		print.close();
	}

	static class Print {
		private final BufferedWriter bw;

		public Print() {
			this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
		}

		public void print(Object object) throws IOException {
			bw.append("" + object);
		}

		public void println(Object object) throws IOException {
			print(object);
			bw.append("\n");
		}

		public void close() throws IOException {
			bw.close();
		}
	}

	static class Scan {
		private byte[] buf = new byte[1024 * 1024];
		private int index;
		private InputStream in;
		private int total;

		public Scan() {
			in = System.in;
		}

		public int scan() throws IOException {
			if (total < 0)
				throw new InputMismatchException();
			if (index >= total) {
				index = 0;
				total = in.read(buf);
				if (total <= 0)
					return -1;
			}
			return buf[index++];
		}

		public int scanInt() throws IOException {
			int integer = 0;
			int n = scan();
			while (isWhiteSpace(n))
				n = scan();
			int neg = 1;
			if (n == '-') {
				neg = -1;
				n = scan();
			}
			while (!isWhiteSpace(n)) {
				if (n >= '0' && n <= '9') {
					integer *= 10;
					integer += n - '0';
					n = scan();
				} else
					throw new InputMismatchException();
			}
			return neg * integer;
		}

		public double scanDouble() throws IOException {
			double doub = 0;
			int n = scan();
			while (isWhiteSpace(n))
				n = scan();
			int neg = 1;
			if (n == '-') {
				neg = -1;
				n = scan();
			}
			while (!isWhiteSpace(n) && n != '.') {
				if (n >= '0' && n <= '9') {
					doub *= 10;
					doub += n - '0';
					n = scan();
				} else
					throw new InputMismatchException();
			}
			if (n == '.') {
				n = scan();
				double temp = 1;
				while (!isWhiteSpace(n)) {
					if (n >= '0' && n <= '9') {
						temp /= 10;
						doub += (n - '0') * temp;
						n = scan();
					} else
						throw new InputMismatchException();
				}
			}
			return doub * neg;
		}

		public long scanLong() throws IOException {
			long ret = 0;
			long c = scan();
			while (c <= ' ') {
				c = scan();
			}

			boolean neg = (c == '-');
			if (neg) {
				c = scan();
			}

			do {
				ret = ret * 10 + c - '0';
			} while ((c = scan()) >= '0' && c <= '9');

			if (neg) {
				return -ret;
			}
			return ret;
		}

		public String scanString() throws IOException {
			StringBuilder sb = new StringBuilder();
			int n = scan();
			while (isWhiteSpace(n))
				n = scan();
			while (!isWhiteSpace(n) || n == ' ') {
				sb.append((char) n);
				n = scan();
			}
			return sb.toString();
		}

		private boolean isWhiteSpace(int n) {
			if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
				return true;
			return false;
		}
	}
}
