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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author bhavy seth
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD2 solver = new TaskD2();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD2 {
        public void solve(int testNumber, InputReader sc, PrintWriter out) {
            int t = sc.nextInt();
            while (t-- > 0) {
                String s = sc.nextLine();
                int n = s.length();
                StringBuilder s1 = new StringBuilder();
                int k = -1;
                for (int i = 0; i < n; i++) {
                    if (s.charAt(i) == s.charAt(n - i - 1)) {
                        s1.append(s.charAt(i));
                        k = i;
                    } else
                        break;
                }
                if (k < n / 2) {
                    StringBuilder s2 = new StringBuilder();
                    StringBuilder s3 = new StringBuilder();
                    if (k != -1) {
                        s2.append(s.substring(k + 1, n - k - 1));
                    } else {
                        s2.append(s);
                    }
                    s3.append(s2);
                    //  out.println(s2);
                    StringBuilder temp = new StringBuilder();
                    temp.append(s2);
                    s2.append("*");
                    s2.append(temp.reverse());
                    ///now using prefix suffix count using KMP agorithm

                    int pi[] = new int[s2.length()];
                    int j = 0;
                    for (int i = 1; i < s2.length(); i++) {
                        j = pi[i - 1];
                        while (j > 0 && s2.charAt(i) != s2.charAt(j))
                            j = pi[j - 1];
                        if (s2.charAt(i) == s2.charAt(j))
                            j++;
                        pi[i] = j;
                    }
                    int len2 = pi[s2.length() - 1];
                    s3.reverse();
                    StringBuilder tem = new StringBuilder();
                    s3.append("*");
                    tem.append(s3);
                    s3.append(tem.reverse());
                    int pi2[] = new int[s3.length()];
                    int J = 0;
                    for (int i = 1; i < s3.length(); i++) {
                        J = pi2[i - 1];
                        while (J > 0 && s3.charAt(i) != s3.charAt(J))
                            J = pi2[J - 1];
                        if (s3.charAt(i) == s3.charAt(J))
                            J++;
                        pi2[i] = J;
                    }
                    //  for(int l=0;l<s2.length();l++){
                    //    out.print(pi[l]+" ");
                    //}
                    //out.println();
                    StringBuilder res = new StringBuilder();
                    if (k != -1) {
                        res.append(s.substring(0, k + 1));
                        //out.println(res);
                    }
                    int len3 = pi2[s3.length() - 1];
                    if (len2 >= len3) {
                        res.append(s2.substring(0, len2));
                        // out.println(res);
                    } else {
                        res.append(s3.substring(0, len3));
                        // out.println(res);
                    }
                    if (k != -1)
                        res.append(s.substring(n - k - 1, n));
                    out.println(res);
                } else {
                    out.println(s);
                }

            }
        }

    }

    static class InputReader {
        BufferedReader br;
        StringTokenizer st;

        public InputReader(InputStream inputStream) {
            br = new BufferedReader(new
                    InputStreamReader(inputStream));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

        public String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

    }
}