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:
//created by Whiplash99
import java.io.*;
import java.util.*;
public class D2
{
    private static void calcPi(char str[], int pi[], int N)
    {
        for (int i = 1, j=0; i < N; i++) {
            while (j > 0 && str[i] != str[j]) j = pi[j-1];
            if (str[i] == str[j]) j++;
            pi[i] = j;
        }
    }
    private static void reverse(char str[], char rev[], int N)
    {
        for(int i=0;i<N;i++)
            rev[i]=str[N-i-1];
    }
    public static void main(String[] args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        int i,N;

        int T=Integer.parseInt(br.readLine().trim());
        StringBuilder sb=new StringBuilder();

        while(T-->0)
        {
            char str[]=br.readLine().trim().toCharArray();
            N=str.length;

            char rev[]=new char[N];
            reverse(str,rev,N);

            int l1,l2,l3,l4,l5;
            l1=l2=l3=l4=l5=0;

            int pi[]=new int[3*N];
            char tmp[]=(new String(str)+"$"+new String(rev)).toCharArray();
            calcPi(tmp,pi,2*N+1);
            l1=pi[2*N];

            tmp=(new String(rev)+"$"+new String(str)).toCharArray();
            calcPi(tmp,pi,2*N+1);
            l2=pi[2*N];

            for(i=0;i<N/2;i++)
            {
                if(str[i]==str[N-i-1])
                    l3=i+1;
                else
                    break;
            }

            int len=N-l3-1-l3+1;
            if(len>0&&l3>0)
            {
                char str2[]=new char[len];
                char rev2[]=new char[len];

                for(i=l3;i<=N-l3-1;i++)
                    str2[i-l3]=str[i];

                reverse(str2,rev2,len);

                tmp=(new String(str2)+"$"+new String(rev2)).toCharArray();
                calcPi(tmp,pi,2*len+1);
                l4=pi[2*len];

                tmp=(new String(rev2)+"$"+new String(str2)).toCharArray();
                calcPi(tmp,pi,2*len+1);
                l5=pi[2*len];
            }

            int max=Math.max(l1,l2);
            max=Math.max(max,2*l3+l4);
            max=Math.max(max,2*l3+l5);

            if(max==l1)
            {
                for(i=0;i<l1;i++)
                    sb.append(str[i]);
            }
            else if(max==l2)
            {
                for(i=N-l2;i<N;i++)
                    sb.append(str[i]);
            }
            else if(max==2*l3+l4)
            {
                for(i=0;i<l3+l4;i++)
                    sb.append(str[i]);
                for(i=N-l3;i<N;i++)
                    sb.append(str[i]);
            }
            else
            {
                for(i=0;i<l3;i++)
                    sb.append(str[i]);
                for(i=N-l3-l5;i<N;i++)
                    sb.append(str[i]);
            }
            sb.append("\n");
        }
        System.out.println(sb);
    }
}