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 implements Runnable{
    FastScanner sc;
    PrintWriter pw;
    final class FastScanner {
        BufferedReader br;
        StringTokenizer st;
 
        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        public long nlo() {
            return Long.parseLong(next());
        }
 
        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }
 
        public int ni() {
            return Integer.parseInt(next());
        }
 
        public String nli() {
            String line = "";
            if (st.hasMoreTokens()) line = st.nextToken();
            else try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (st.hasMoreTokens()) line += " " + st.nextToken();
            return line;
        }
 
        public double nd() {
            return Double.parseDouble(next());
        }
    }
    public static void main(String[] args)
    {
        new Thread(null,new Main(),"codeforces",1<<25).start();
    }
    public void run()
    {
        sc=new FastScanner();
        pw=new PrintWriter(System.out);
       solve();
        pw.flush();
        pw.close();
    }
    public long gcd(long a,long b)
    {
        return b==0L?a:gcd(b,a%b);
    }
    public long ppow(long a,long b,long mod)
    {
        if(b==0L)
        return 1L;
        long tmp=1;
        while(b>1L)
        {
            if((b&1L)==1)
            tmp*=a;
            a*=a;
            a%=mod;
            tmp%=mod;
            b>>=1;
        }
        return (tmp*a)%mod;
    }
    //////////////////////////////////
    /////////////  LOGIC  ///////////
    ////////////////////////////////
    public long[] pow,prr,srr,inv;
    public long mod,p;
    public int n;
    public void solve()
    {
        int t=sc.ni();
        p=59;
        mod=1000000007;
        pow=new long[1000000];
        inv=new long[1000000];
        pow[0]=1;
        inv[0]=1;
        for(int i=1;i<1000000;i++)
        {
            pow[i]=p*pow[i-1];
            while(pow[i]>=mod)
            pow[i]-=mod;
            inv[i]=ppow(pow[i],mod-2,mod);
        }
        while(t-->0)
        {
        char[] arr=sc.next().toCharArray();
        n=arr.length;
        prr=new long[n];
        srr=new long[n];
        prr[0]=pow[0]*(arr[0]-'a'+1);
        srr[n-1]=pow[0]*(arr[n-1]-'a'+1);
        for(int i=1;i<n;i++)
        {
            prr[i]=(arr[i]-'a'+1)*pow[i];
            while(prr[i]>=mod)
            prr[i]-=mod;
            prr[i]+=prr[i-1];
            if(prr[i]>=mod)
            prr[i]-=mod;
        }
        for(int i=n-2;i>=0;i--)
        {
            srr[i]=(arr[i]-'a'+1)*pow[n-1-i];
            while(srr[i]>=mod)
            srr[i]-=mod;
            srr[i]+=srr[i+1];
            if(srr[i]>=mod)
            srr[i]-=mod;
        }
        StringBuilder str,str1,str4,str3;
        int tmp=0;
        for(int i=0;i<n;i++)
            if(isp(arr,0,i))
                tmp=i;
        str=new StringBuilder(String.copyValueOf(arr,0,tmp+1));
        tmp=n-1;
        for(int i=n-2;i>=0;i--)
            if(isp(arr,i,n-1))
            tmp=i;
        str1=new StringBuilder(String.copyValueOf(arr,tmp,n-tmp));
        int i=0,j=n-1;
        while((i<=j)&&(arr[i]==arr[j]))
        {
            i++;j--;
        }
        if(i<j)
        {
            int a=i;
            int b=j;
            tmp=a;
            for(int k=a+1;k<=b;k++)
            if(isp(arr,a,k))
            tmp=k;
            str3=new StringBuilder(String.copyValueOf(arr,0,i));
            str4=new StringBuilder(String.copyValueOf(arr,0,i));
            str3.append(String.copyValueOf(arr,i,tmp-i+1));
            tmp=b;
            for(int k=b-1;k>=a;k--)
            if(isp(arr,k,b))
            tmp=k;
            str4.append(String.copyValueOf(arr,tmp,b-tmp+1));
            str3.append(new StringBuilder(String.copyValueOf(arr,0,i)).reverse());
            str4.append(new StringBuilder(String.copyValueOf(arr,0,i)).reverse());
        }
        else
        {str3=new StringBuilder(String.copyValueOf(arr,0,n));
        str4=new StringBuilder(String.copyValueOf(arr,0,n));}
        int l1=str.length();int l2=str1.length();int l3=str3.length();int l4=str4.length();
        if(l1>=l2&&l1>=l3&&l1>=l4)
        pw.println(str);
        else if(l2>=l1&&l2>=l3&&l2>=l4)
        pw.println(str1);
        else if(l3>=l1&&l3>=l2&&l3>=l4)
        pw.println(str3);
        else
        pw.println(str4);
        }
    }
    public boolean isp(char[] arr,int i,int j)
    {
        int n=arr.length;
        int a=i;
        int b=j;
        long x,y;
        if(a==0)
        x=prr[b];
        else
        {
            x=(prr[b]-prr[a-1]+mod)*inv[a];
            x%=mod;
        }
        if(b==n-1)
        y=srr[a];
        else
        {
            y=(srr[a]-srr[b+1]+mod)*inv[n-1-b];
            y%=mod;
        }
        if(x==y)
        return true;
        else 
        return false;
    }
}