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.*;
import java.util.*;


public class D {
    InputReader sc;
    PrintWriter pw;


    class Manacher {
        public int[]  p;  // p[i] = length of longest palindromic substring of t, centered at i
        private String s;  // original string
        private char[] t;  // transformed string


        // Manacher code from: https://algs4.cs.princeton.edu/53substring/Manacher.java.html
        /******************************************************************************
         *  Compilation:  javac Manacher.java
         *  Execution:    java Manacher text
         *  Dependencies: StdOut.java
         *
         *  Computes the longest palindromic substring in linear time
         *  using Manacher's algorithm.
         *
         *  Credits: The code is lifted from the following excellent reference
         *  http://www.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html
         *
         ******************************************************************************/

        public Manacher(String s) {
            this.s = s;
            preprocess();
            p = new int[t.length];

            int center = 0, right = 0;
            for (int i = 1; i < t.length-1; i++) {
                int mirror = 2*center - i;

                if (right > i)
                    p[i] = Math.min(right - i, p[mirror]);

                // attempt to expand palindrome centered at i
                while (t[i + (1 + p[i])] == t[i - (1 + p[i])])
                    p[i]++;

                // if palindrome centered at i expands past right,
                // adjust center based on expanded palindrome.
                if (i + p[i] > right) {
                    center = i;
                    right = i + p[i];
                }
            }

        }

        // Transform s into t.
        // For example, if s = "abba", then t = "$#a#b#b#a#@"
        // the # are interleaved to avoid even/odd-length palindromes uniformly
        // $ and @ are prepended and appended to each end to avoid bounds checking
        private void preprocess() {
            t = new char[s.length()*2 + 3];
            t[0] = '$';
            t[s.length()*2 + 2] = '@';
            for (int i = 0; i < s.length(); i++) {
                t[2*i + 1] = '#';
                t[2*i + 2] = s.charAt(i);
            }
            t[s.length()*2 + 1] = '#';
        }
    }

    /******************************************************************************/

    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 float nextFloat() {
            return Float.parseFloat(next());
        }

        public double nextDouble() {
            return Float.parseFloat(next());
        }

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

    class Palindrome {
        int center;
        int size;

        Palindrome(int center, int size) {
            this.center = center;
            this.size = size;
        }

        int left() {
            return center - size;
        }

        int right() {
            return center + size;
        }

        int startIdx(int pos) {
            // 2 * n + 3
            // 2, 4, 6, 8, ..., 2 * n
            // 0, 1, .....,     n - 1

            if (pos % 2 == 0) {
                int idx = pos / 2 - 1;
                int startPos = pos - 1 - 2 * (pos - center);
                int startIdx = (startPos + 1) / 2;
                return startIdx;
            } else {
                return pos;
            }
        }

        boolean covers(int pos) {
            return left() < pos && pos < right();
        }

        public String toString() {
            return String.format("center=%d, size=%d: [%d - %d]", center, size, left(), right());
        }
    }

    int[] maxLengths(int[] m) {
        int N = m.length;
        int[] res = new int[N];

        LinkedList<Palindrome> q = new LinkedList<>();
        for (int i = 0; i < N; i++) {
            Palindrome p = new Palindrome(i, m[i]);

            q.add(p);

            while (!q.isEmpty() && !q.getFirst().covers(i)) {
                q.removeFirst();
            }

            if (i % 2 == 0) {
                while (!q.isEmpty()) {
                    Palindrome first = q.getFirst();
                    int n = (N - 3) / 2;
                    int idx = i / 2 - 1;
                    if (n - idx - 1 > first.startIdx(i)) {
                        q.removeFirst();
                    } else {
                        break;
                    }
                }
            }

            if (q.isEmpty()) {
                res[i] = 0;
            } else {
                res[i] = 1 + 2 * (i - q.getFirst().center);
            }
        }

        return res;
    }

    void reverse(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n / 2; i++) {
            int aux = arr[i];
            arr[i] = arr[n - 1 - i];
            arr[n - 1 - i] = aux;
        }
    }

    String solve(String s) {
        Manacher m = new Manacher(s);

        int[] nBw = maxLengths(m.p);
        reverse(m.p);
        int[] nFw = maxLengths(m.p);
        reverse(nFw);

        int n = s.length();
        int N = n * 2 + 3;
        int bestL = Math.max((nFw[2] + 1) / 2, (nBw[N - 3] + 1) / 2);
        int bestNCommon = 0;

        for (int nCommon = 1; nCommon < n; nCommon++) {
            if (s.charAt(nCommon - 1) == s.charAt(n - nCommon)) {
                int crtL = nCommon * 2 + Math.max((nFw[nCommon * 2 + 2] + 1) / 2,
                        (nBw[N - nCommon * 2 - 3] + 1) / 2);
                if (crtL > n) {
                    break;
                }
                if (crtL > bestL) {
                    bestL = crtL;
                    bestNCommon = nCommon;
                }
            } else {
                break;
            }
        }

        String best = s.substring(0, bestNCommon);

        int n1 = (nFw[bestNCommon * 2 + 2] + 1) / 2;
        int n2 = (nBw[N - bestNCommon * 2 - 3] + 1) / 2;
        if (n1 > n2) {
            best += s.substring(bestNCommon, bestNCommon + n1);
        } else {
            best += s.substring(n - bestNCommon - n2, n - bestNCommon);
        }

        best += s.substring(n - bestNCommon);

        return best;
    }

    void run() throws FileNotFoundException {
        // sc = new Scanner(new File("D.txt"));
        sc = new InputReader(System.in);
        pw = new PrintWriter(System.out);

        int T = sc.nextInt();
        for (int t = 0; t < T; t++) {
            String s = sc.next();

            String res = solve(s);
            pw.println(res);
        }

        pw.close();
    }

    public static void main(String[] args) throws FileNotFoundException {
        D sol = new D();
        sol.run();
    }
}