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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        D2PrefixSuffixPalindromeHardVersion solver = new D2PrefixSuffixPalindromeHardVersion();
        solver.solve(1, in, out);
        out.close();
    }

    static class D2PrefixSuffixPalindromeHardVersion {
        long mod = 1000000009;
        int MAXN = 100005;
        PrintWriter out;
        InputReader in;
        long[] pow = new long[MAXN];
        long[] inv_pow = new long[MAXN];

        void pre() {
            int i = 0;
            pow[0] = 1;
            inv_pow[0] = 1;
            for (i = 1; i < MAXN; i++) {
                pow[i] = (pow[i - 1] * 31) % mod;
                inv_pow[i] = power(pow[i], mod - 2);
            }
        }

        String manacher(char[] s) {
            //pn(Arrays.toString(s));
            int n = s.length;
            int[] d1 = new int[n];
            int[] d2 = new int[n];
            for (int i = 0, l = 0, r = -1; i < n; i++) {
                int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1);
                while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) {
                    k++;
                }
                d1[i] = k--;
                if (i + k > r) {
                    l = i - k;
                    r = i + k;
                }
            }
            for (int i = 0, l = 0, r = -1; i < n; i++) {
                int k = (i > r) ? 0 : Math.min(d2[l + r - i + 1], r - i + 1);
                while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) {
                    k++;
                }
                d2[i] = k--;
                if (i + k > r) {
                    l = i - k - 1;
                    r = i + k;
                }
            }
            int i = 0;
            int type = -1, ind = -1, mx = 0;
            for (i = 0; i < n; i++) {
                if (i + d1[i] == n) {
                    int left = i - d1[i] + 1;
                    int right = n - 1;
                    int len = right - left + 1;
                    if (len > mx) {
                        mx = len;
                        type = 1;
                        ind = i;
                    }
                }
            }
            for (i = 0; i < n; i++) {
                if (i + d2[i] == n) {
                    int left = i - d2[i];
                    int right = n - 1;
                    int len = right - left + 1;
                    if (len > mx) {
                        mx = len;
                        type = 2;
                        ind = i;
                    }
                }
            }

            StringBuilder str = new StringBuilder();
            i = ind;
            //pn(ind +" "+mx +" "+type);
            //pn(Arrays.toString(d1));
            //pn(Arrays.toString(d2));
            if (type == 1) {
                int left = i - d1[i] + 1;
                int right = n - 1;
                for (i = left; i <= right; i++)
                    str.append(s[i]);
            } else {
                int left = i - d2[i];
                int right = n - 1;
                for (i = left; i <= right; i++)
                    str.append(s[i]);
            }
            return str.toString();
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            int t = ni();
            pre();
            StringBuffer ans = new StringBuffer();
            while (t-- > 0) {
                String st = n();
                int n = st.length();
                char[] arr = st.toCharArray();
                int lo = 0, hi = n - 1;
                while (lo <= hi) {
                    if (arr[lo] == arr[hi]) {
                        lo++;
                        hi--;
                    } else
                        break;
                }

                if (lo > hi) {
                    ans.append(st).append("\n");
                    continue;
                }
                StringBuilder str = new StringBuilder();
                int i = 0;
                for (i = lo; i <= hi; i++)
                    str.append(arr[i]);
                String s1 = manacher(str.toString().toCharArray());
                String s2 = manacher(str.reverse().toString().toCharArray());
                //pn(s1 +" "+s2);
                String opt = "";
                if (s1.length() > s2.length())
                    opt = s1;
                else
                    opt = s2;
                for (i = 0; i < lo; i++)
                    ans.append(arr[i]);
                ans.append(opt);
                for (i = hi + 1; i < n; i++)
                    ans.append(arr[i]);
                ans.append("\n");
            }
            pn(ans);
        }

        String n() {
            return in.next();
        }

        int ni() {
            return in.nextInt();
        }

        void pn(Object o) {
            out.println(o);
        }

        long power(long x, long y) {
            if (y == 0) {
                return 1;
            }
            long p = power(x, y / 2) % mod;
            p = (p * p) % mod;
            return (y % 2 == 0) ? p : (x * p) % mod;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

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

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}