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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

import java.util.*;

public class Main {

// vector< int > manacher(const string &s) {
//   vector< int > radius(s.size());
//   int i = 0, j = 0;
//   while(i < s.size()) {
//     while(i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j]) {
//       ++j;
//     }
//     radius[i] = j;
//     int k = 1;
//     while(i - k >= 0 && i + k < s.size() && k + radius[i - k] < j) {
//       radius[i + k] = radius[i - k];
//       ++k;
//     }
//     i += k;
//     j -= k;
//   }
//   return radius;
// }
    public static int[] manacher(String S) {
        int[] radius = new int[S.length()];
        int i = 0;
        int j = 0;
        while (i<S.length()) {
            while (i-j>=0 && i+j<S.length() && S.charAt(i-j)==S.charAt(i+j)) {
                j++;
            }
            radius[i] = j;
            int k = 1;
            while (i-k>=0 && i+k<S.length() && k+radius[i-k]<j) {
                radius[i+k] = radius[i-k];
                k++;
            }
            i+=k;
            j-=k;
        }
        return radius;
    }
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        int t = in.nextInt();
        for (int i=0;i<t;i++) {
            String S = in.next();
            char[] c = S.toCharArray();
            int n = c.length;
            int index = 0;
            while (index<n/2) {
                if (c[index]==c[n-1-index]) {
                    index++;
                } else {
                    break;
                }
            }
            StringBuilder ans = new StringBuilder();
            for (int j=0;j<index;j++) {
                ans.append(c[j]);
            }
            StringBuilder sb = new StringBuilder();
            StringBuilder sbsb = new StringBuilder();
            for (int j=index;j<n-index;j++) {
                sbsb.append(c[j]);
                sb.append('$');
                sb.append(c[j]);
                if (j==n-index-1) sb.append('$');
            }
            String S_tmp = sb.toString();
            int[] arr_new = manacher(S_tmp);
            int max_index_l = -1;
            int max_val_l = -1;
            for (int j=1;j<arr_new.length;j++) {
                if (arr_new[j]==j+1 && max_val_l<arr_new[j]) {
                    max_val_l = arr_new[j];
                    max_index_l = j;
                }
            }
            int max_index_r = -1;
            int max_val_r = -1;
            for (int j=arr_new.length-2;j>=1;j--) {
                if (arr_new[j]==arr_new.length-j && max_val_r<arr_new[j]) {
                    max_val_r = arr_new[j];
                    max_index_r = j;
                }
            }
            if (max_val_l<max_val_r) {
                // out.println("sbsb: "+sbsb);
                sbsb = sbsb.reverse();
                // out.println("sbsb: "+sbsb);
                max_val_l = max_val_r;
                max_index_l = arr_new.length-1-max_index_r;
            }
            // System.out.println(Arrays.toString(arr_new));
            // System.out.println(sb);
            // System.out.println(sbsb);
            // System.out.println("max_val_l, max_index: "+max_val_l+" "+max_index_l);
            char[] ddd = sbsb.toString().toCharArray();
            int ccc = 0;
            while (ccc<(max_val_l+1)/2-1) {
                ans.append(ddd[ccc]);
                ccc++;
            }
            if (max_val_l%2==0) {
                ans.append(ddd[ccc]);
                ccc++;
            }
            int eee = 0;
            while (eee<(max_val_l+1)/2-1) {
                ans.append(ddd[ccc]);
                ccc++;
                eee++;
            }
            for (int j=n-index;j<n;j++) {
                ans.append(c[j]);
            }

            // if (max_val_l>=max_val_r) {
            //     for (int j=index;j<index+max_val_l)
            // }

            out.println(ans);
            // out.println(S_tmp);
            // out.println(Arrays.toString(manacher(S_tmp)));
            // sb.append("#");
            // for (int j=n-1;j>=0;j--) {
            //     sb.append(c[j]);
            // }
            // String str = sb.toString();
            // int[] z_array = z_algo(str);
            // out.println(str);
            // out.println(Arrays.toString(z_array));
        }

        out.close();
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}