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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class C1326D2 {
    public static void main(String[] args) {
        var scanner = new BufferedScanner();
        var writer = new PrintWriter(new BufferedOutputStream(System.out));

        var t = scanner.nextInt();
//        var t = 1;
        for (int tc = 0; tc < t; tc++) {
            var s = scanner.next();
//            var sb = new StringBuilder();
//            for (; sb.length() < 1e6; ) {
//                sb.append("ab");
//            }
//            var s = sb.toString();
//            System.out.println(s.length());
            var cs = s.chars().map(i -> i - 'a').toArray();
            var rcs = reverse(cs);
            var maxPrefixSuffix = 0;
            {
                var left = 0;
                var right = s.length() - 1;
                while (left < right && cs[left] == cs[right]) {
                    left++;
                    right--;
                }
                maxPrefixSuffix = left;
            }
            var tt = System.nanoTime();
            int csLen = cs.length;
            var hashLeft = new int[csLen + 1][];
            var hashRight = new int[csLen + 1][];
            for (var len = 1; len <= csLen; len <<= 1) {
                hashLeft[len] = hash(cs, len);
                {
                    var hs = hash(rcs, len);
                    hashRight[len] = new int[csLen];
                    for (int i = 0, j = csLen - len; i + len - 1 < csLen; i++, j--) {
                        hashRight[len][i] = hs[j];
                    }
                }
            }
//            System.out.println((System.nanoTime() - tt) / 1e9);
            tt = System.nanoTime();
            var ansLen = 0;
            var ansData = new int[0][];
            var t1 = 0;
            for (int center = 0; center < csLen; center++) {
//                var ttt = System.nanoTime();
                var ans1 = find(center, center, maxPrefixSuffix, cs, hashLeft, hashRight);
                var ans2 = find(center, center + 1, maxPrefixSuffix, cs, hashLeft, hashRight);
//                t1 += System.nanoTime() - ttt;
                var ans1Len = 0;
                for (int[] r : ans1) {
                    ans1Len += r[1] - r[0] + 1;
                }
//                var ans1Len = Arrays.stream(ans1).map(p -> p[1] - p[0] + 1).mapToInt(i -> i).sum();
                if (ans1Len > ansLen) {
                    ansLen = ans1Len;
                    ansData = ans1;
                }
                var ans2Len = 0;
                for (int[] r : ans2) {
                    ans2Len += r[1] - r[0] + 1;
                }
//                var ans2Len = Arrays.stream(ans2).map(p -> p[1] - p[0] + 1).mapToInt(i -> i).sum();
                if (ans2Len > ansLen) {
                    ansLen = ans2Len;
                    ansData = ans2;
                }
            }
//            System.out.println("t1: " + (t1 / 1e9));
//            System.out.println((System.nanoTime() - tt) / 1e9);
            var ansT = "";
            for (int i = 0; i < 3; i++) {
                ansT += s.substring(ansData[i][0], ansData[i][1] + 1);
            }
            writer.println(ansT);
        }

        scanner.close();
        writer.flush();
        writer.close();
    }

    private static int[] reverse(int[] cs) {
        var rcs = new int[cs.length];
        for (int i = 0, j = cs.length - 1; i < cs.length; i++, j--) {
            rcs[j] = cs[i];
        }
        return rcs;
    }

    private static int[][] find(int c1, int c2, int maxPrefixSuffix, int[] cs, int[][] hashLeft, int[][] hashRight) {
        int csLen = cs.length;
        if (c1 < 0 || c2 >= csLen) {
            return new int[0][];
        }
        var r = Math.min(c1 + 1, csLen - c2);
        var len = 1;
        while ((len << 1) <= r) {
            len <<= 1;
        }
        var i = c1 - len + 1;
        var j = c2 + len - 1;
        while (len > 0) {
            var half = len >> 1;
            if (hashLeft[len][i] == hashRight[len][j - len + 1]) {
                while (i - half < 0 || j + half >= csLen) {
                    half >>= 1;
                }
                i -= half;
                j += half;
            } else {
                i += half;
                j -= half;
            }
            len = half;
        }
        if (cs[i] != cs[j]) {
            i++;
            j--;
        }
        if (cs[i] != cs[j] || i > maxPrefixSuffix && j + maxPrefixSuffix < csLen - 1) {
            return new int[0][];
        } else {
            if (c1 < csLen / 2) {
                return new int[][]{{0, i - 1}, {i, j}, {csLen - i, csLen - 1}};
            } else {
                return new int[][]{{0, csLen - j - 2}, {i, j}, {j + 1, csLen - 1}};
            }
        }
    }

    static long modulo = (long) 1e9 + 9;
    static long r = 26;

    private static int[] hash(int[] cs, int len) {
        int csLen = cs.length;
        var hs = new int[csLen];
        var h = 0L;
        var p = 1L;
        for (int i = 0; i < len - 1; i++) {
            h = (h * r + cs[i]) % modulo;
            p = p * r % modulo;
        }
        h = (h * r + cs[len - 1]) % modulo;
        hs[0] = (int) h;
        var forMinus = r * modulo;
        for (int i = 1; i + len - 1 < csLen; i++) {
            h = ((h + forMinus - p * cs[i - 1]) * r + cs[i + len - 1]) % modulo;
            hs[i] = (int) h;
        }
        return hs;
    }

    public static class BufferedScanner {
        BufferedReader br;
        StringTokenizer st;

        public BufferedScanner(Reader reader) {
            br = new BufferedReader(reader);
        }

        public BufferedScanner() {
            this(new InputStreamReader(System.in));
        }

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

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

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

        double nextDouble() {
            return Double.parseDouble(next());
        }

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

        void close() {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    static long gcd(long a, long b) {
        if (a < b) {
            return gcd(b, a);
        }
        while (b > 0) {
            long tmp = b;
            b = a % b;
            a = tmp;
        }
        return a;
    }

    static long inverse(long a, long m) {
        long[] ans = extgcd(a, m);
        return ans[0] == 1 ? (ans[1] + m) % m : -1;
    }

    private static long[] extgcd(long a, long m) {
        if (m == 0) {
            return new long[]{a, 1, 0};
        } else {
            long[] ans = extgcd(m, a % m);
            long tmp = ans[1];
            ans[1] = ans[2];
            ans[2] = tmp;
            ans[2] -= ans[1] * (a / m);
            return ans;
        }
    }

    private static List<Integer> primes(double upperBound) {
        var limit = (int) Math.sqrt(upperBound);
        var isComposite = new boolean[limit + 1];
        var primes = new ArrayList<Integer>();
        for (int i = 2; i <= limit; i++) {
            if (isComposite[i]) {
                continue;
            }
            primes.add(i);
            int j = i + i;
            while (j <= limit) {
                isComposite[j] = true;
                j += i;
            }
        }
        return primes;
    }


}
