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

public class D {
    private static final int IN_BUFFER_SIZE = 1 << 16;
    private static final int OUT_BUFFER_SIZE = 1 << 16;
    private byte[] input = new byte[IN_BUFFER_SIZE];
    private int ix = IN_BUFFER_SIZE;
    private int bytesRead = ix;
    private byte[] output = new byte[OUT_BUFFER_SIZE];
    private int ox = 0;

    private void readMore() {
        try {
            bytesRead = System.in.read(input, 0, IN_BUFFER_SIZE);
            if (bytesRead <= 0)
                throw new RuntimeException();
            ix = 0;
        } catch (IOException e) {
            throw new RuntimeException();
        }
    }

    private void flushOut() {
        System.out.write(output, 0, ox);
        ox = 0;
    }

    private void append(String s) {
        char[] ca = s.toCharArray();
        for (int i = 0; i < ca.length; i++) {
            append(ca[i]);
        }
    }

    private void append(char c) {
        if (ox == OUT_BUFFER_SIZE)
            flushOut();
        output[ox++] = (byte) c;
    }

    private int nextInt() {
        skipSpaces();
        int ret = 0;
        if (ix == bytesRead) {
            readMore();
        }
        int sign = 1;
        if (input[ix] == '-') {
            sign = -1;
            ix++;
        }
        while (true) {
            if (ix == bytesRead) {
                try {
                    readMore();
                } catch (RuntimeException e) {
                    return ret;
                }
            }
            if (input[ix] < '0') {
                break;
            }
            ret *= 10;
            ret += input[ix++] - '0';
        }
        return sign * ret;
    }

    private long nextLong() {
        skipSpaces();
        long ret = 0;
        if (ix == bytesRead) {
            readMore();
        }
        int sign = 1;
        if (input[ix] == '-') {
            sign = -1;
            ix++;
        }
        while (true) {
            if (ix == bytesRead) {
                try {
                    readMore();
                } catch (RuntimeException e) {
                    return ret;
                }
            }
            if (input[ix] < '0')
                break;
            ret *= 10;
            ret += input[ix++] - '0';
        }
        return sign * ret;
    }

    private String nextLine() {
        StringBuilder sb = new StringBuilder();
        while (true) {
            if (ix == bytesRead) {
                readMore();
            }
            char c = (char) input[ix++];
            if (c == '\n')
                break;
            sb.append(c);
        }
        return sb.toString();
    }

    private String nextString() {
        try {
            skipSpaces();
        } catch (RuntimeException e) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        while (true) {
            if (ix == bytesRead) {
                try {
                    readMore();
                } catch (RuntimeException e) {
                    return sb.toString();
                }
            }
            if (input[ix] <= ' ')
                break;
            sb.append((char) input[ix++]);
        }
        return sb.toString();
    }

    private String nextStringIncludeSpace32() {
        try {
            skipSpaces();
        } catch (RuntimeException e) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        while (true) {
            if (ix == bytesRead) {
                try {
                    readMore();
                } catch (RuntimeException e) {
                    return sb.toString();
                }
            }
            if (input[ix] < ' ')
                break;
            sb.append((char) input[ix++]);
        }
        return sb.toString();
    }

    private void nextCharArray(char[] ca, int len) {
        skipSpaces();
        for (int i = 0; i < len; i++) {
            if (ix == bytesRead) {
                readMore();
            }
            ca[i] = (char) input[ix++];
        }
    }

    private int charArrayLength;

    private void nextCharArray(char[] ca) {
        skipSpaces();
        if (ix == bytesRead) {
            readMore();
        }
        charArrayLength = 0;
        ca[charArrayLength++] = (char) input[ix++];
        while (true) {
            if (ix == bytesRead) {
                try {
                    readMore();
                } catch (RuntimeException e) {
                    return;
                }
            }
            char c = (char) input[ix++];
            if (c < '0')
                break;
            ca[charArrayLength++] = c;
        }
    }

    private char nextCharSkipRest() {
        skipSpaces();
        if (ix == bytesRead) {
            readMore();
        }
        char ret = (char) input[ix++];
        while (true) {
            if (ix == bytesRead) {
                readMore();
            }
            if (input[ix++] <= ' ')
                break;
        }
        return ret;
    }

    int cntSize;

    private char lastCharSkipRest() {
        skipSpaces();
        if (ix == bytesRead) {
            readMore();
        }
        char ret = (char) input[ix++];
        cntSize = 1;
        while (true) {
            if (ix == bytesRead) {
                readMore();
            }
            char c = (char) input[ix++];
            if (c <= ' ')
                break;
            ret = c;
            cntSize++;
        }
        return ret;
    }

    private void skipSpaces() {
        while (true) {
            if (ix == bytesRead) {
                readMore();
            }
            if (input[ix] > ' ')
                break;
            ix++;
        }
    }

    private char[] nn = new char[32];

    private void printLong(long n) {
        if (n == 0) {
            append('0');
        } else {
            if (n < 0) {
                append('-');
                n = -n;
            }
            int kk = 0;
            while (n > 0) {
                nn[kk++] = (char) (n % 10 + '0');
                n /= 10;
            }
            for (int i = kk - 1; i >= 0; i--) {
                append(nn[i]);
            }
        }
    }

    private void printInt(int n) {
        if (n == 0) {
            append('0');
        } else {
            if (n < 0) {
                append('-');
                n = -n;
            }
            int kk = 0;
            while (n > 0) {
                nn[kk++] = (char) (n % 10 + '0');
                n /= 10;
            }
            for (int i = kk - 1; i >= 0; i--) {
                append(nn[i]);
            }
        }
    }

    private void printDouble(double x, int p) {
        if (p == 0) {
            printLong(Math.round(x));
        } else {
            long pow = 1;
            for (int i = 0; i < p; i++) {
                pow *= 10;
            }
            x *= pow;
            long t = Math.round(x);
            printLong(t / pow);
            append('.');
            long rem = t % pow;
            if (rem == 0) {
                while (p-- > 0)
                    append('0');
            } else {
                long tt = rem;
                while (tt > 0) {
                    p--;
                    tt /= 10;
                }
                while (p-- > 0)
                    append('0');
                printLong(rem);
            }
        }
    }

    private void work() {
        int nc = nextInt();
        while (nc-- > 0) {
            char[] s = nextString().toCharArray();
            int l = 0;
            int r = s.length - 1;
            while (l < r && s[l] == s[r]) {
                l++;
                r--;
            }

            if (l >= r) {
                for (int i = 0; i < s.length; i++) append(s[i]);
                append('\n');
                continue;
            }

            char[] mid = getEdgePalindrome(new String(s, l, r - l + 1));

            for (int i = 0; i < l; i++) {
                append(s[i]);
            }

            for (int i = 0; i < mid.length; i++) {
                append(mid[i]);
            }

            for (int i = r + 1; i < s.length; i++) {
                append(s[i]);
            }

            append('\n');
        }
        if (ox > 0) flushOut();
        System.out.close();
    }

    private char[] getEdgePalindrome(String s) {

        char[] t = addDelims(s.toCharArray());
        int[] p = new int[t.length];
        int c = 0, r = 0;
        int m = 0, n = 0;
        for (int i = 1; i < t.length; i++) {
            if (i > r) {
                p[i] = 0;
                m = i - 1;
                n = i + 1;
            } else {
                int i2 = c * 2 - i;
                if (p[i2] < (r - i - 1)) {
                    p[i] = p[i2];
                    m = -1;
                } else {
                    p[i] = r - i;
                    n = r + 1;
                    m = i * 2 - n;
                }
            }
            while (m >= 0 && n < t.length && t[m] == t[n]) {
                p[i]++;
                m--;
                n++;
            }
            if ((i + p[i]) > r) {
                c = i;
                r = i + p[i];
            }
        }
        int len = 0;
        c = 0;
        for (int i = 1; i < t.length; i++) {
            if (len < p[i]) {
                // if it is a prefix or a suffix update
                if (i == p[i] || t.length - 1 - i == p[i]) {
                    len = p[i];
                    c = i;
                }
            }
        }

        char[] ss = Arrays.copyOfRange(t, c - len, c + len + 1);
        return removeDelims(ss);
    }

    private char[] addDelims(char[] s) {
        char[] t = new char[2 * s.length + 1];
        char delim = '@';
        for (int i = 0; i < s.length; i++) {
            t[2 * i] = delim;
            t[2 * i + 1] = s[i];
        }
        t[t.length - 1] = delim;
        return t;
    }

    private char[] removeDelims(char[] t) {
        char[] s = new char[t.length / 2];
        for (int i = 0; i < s.length; i++) {
            s[i] = t[2 * i + 1];
        }
        return s;
    }

    public static void main(String[] args) {
        new D().work();
    }
}
