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.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.SplittableRandom;
import java.io.BufferedReader;
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;
        FastScanner in = new FastScanner(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, FastScanner in, PrintWriter out) {
            int numTests = in.nextInt();
            for (int test = 0; test < numTests; test++) {
                char[] s = in.next().toCharArray();
                out.println(solve(s));
            }

            if (false) {
//        if (true) {
                SplittableRandom random = new SplittableRandom(0);
                for (int test = 0; test < 1000; test++) {
                    int n = 30;
                    char[] s = new char[n];
                    for (int i = 0; i < s.length; i++) {
                        s[i] = (char) (random.nextInt(2) + 'a');
                    }
                    String u = solve(s);
                    String v = dummy(s);
                    if (u.length() != v.length()) {
                        System.out.println(new String(s));
                        System.out.println(u);
                        System.out.println(v);
                        throw new AssertionError();
                    }
                }
            }
        }

        private String dummy(char[] s) {
            String best = "";
            String str = new String(s);
            for (int pref = 0; pref <= str.length(); pref++) {
                for (int suff = 0; suff + pref <= str.length(); suff++) {
                    String cur = str.substring(0, pref) + str.substring(str.length() - suff);
                    if (isPalindrome(cur) && best.length() < cur.length()) {
                        best = cur;
                    }
                }
            }
            return best;
        }

        private boolean isPalindrome(String s) {
            return s.equals(new StringBuilder(s).reverse().toString());
        }

        private String solve(char[] s) {
            int n = s.length;
            int len = 0;
            {
                int i = 0;
                int j = n - 1;
                while (i < j && s[i] == s[j]) {
                    ++len;
                    ++i;
                    --j;
                }
                if (i >= j) {
                    return new String(s);
                }
            }

            int[] rOdd = TaskD2.MaximumPalindrome.calcForOdd(s);
            int[] rEven = TaskD2.MaximumPalindrome.calcForEven(s);

            int bestLen = -1;
            int bestI = -1;
            int bestJ = -1;
            boolean bestOdd = false;

            // odd
            // r[i] = maximum j such that [i - j + 1, i + j - 1] is a palindrome.
            for (int i = 0; i < n; i++) {
                int j = rOdd[i];
                int L = i - j + 1;
                int R = i + j - 1;

                int t = Math.min(L, n - 1 - R);
                if (t <= len) {
                    int cur = 2 * t + (2 * j - 1);
                    if (bestLen < cur) {
                        bestLen = cur;
                        bestI = i;
                        bestJ = j;
                        bestOdd = true;
                    }
                }
            }

            // even
            // r[i] = maximum j such that [i - j + 1, i + j] is a palindrome, or 0 if there is none.
            for (int i = 0; i < n; i++) {
                int j = rEven[i];
                if (j == 0) {
                    continue;
                }

                int L = i - j + 1;
                int R = i + j;
                int t = Math.min(L, n - 1 - R);
                if (t <= len) {
                    int cur = 2 * t + 2 * j;
                    if (bestLen < cur) {
                        bestLen = cur;
                        bestI = i;
                        bestJ = j;
                        bestOdd = false;
                    }
                }
            }

            StringBuilder sb = new StringBuilder();
//        System.out.println(bestI + " " + bestJ + " " + bestOdd + " " + bestLen);
            int i = bestI;
            int j = bestJ;
            int L = i - j + 1;
            int R = i + j - 1;
            if (!bestOdd) {
                ++R;
            }
            int t = Math.min(L, n - 1 - R);
            for (int pos = 0; pos < t; pos++) {
                sb.append(s[pos]);
            }
            for (int pos = L; pos <= R; pos++) {
                sb.append(s[pos]);
            }
            for (int pos = n - t; pos < n; pos++) {
                sb.append(s[pos]);
            }

            return sb.toString();
        }

        static class MaximumPalindrome {
            static int[] calcForOdd(char[] s) {
                int n = s.length;
                int[] r = new int[n];
                int rightmost = -1;
                int iForRightmost = -1;
                for (int i = 0; i < s.length; i++) {
                    int cur = 1;
                    if (rightmost >= i) {
                        int j = 2 * iForRightmost - i;
                        cur = Math.min(r[j], rightmost - i + 1);
                    }
                    while (i - cur + 1 >= 0 && i + cur <= n && s[i - cur + 1] == s[i + cur - 1]) {
                        ++cur;
                    }
                    --cur;
                    r[i] = cur;
                    if (rightmost < i + cur - 1) {
                        rightmost = i + cur - 1;
                        iForRightmost = i;
                    }
                }
                return r;
            }

            static int[] calcForEven(char[] s) {
                int n = s.length;
                int[] r = new int[n];
                int rightmost = -1;
                int iForRightmost = -1;
                for (int i = 0; i < s.length; i++) {
                    int cur = 1;
                    if (rightmost > i) {
                        int j = 2 * iForRightmost - i;
                        cur = Math.min(r[j], rightmost - i);
                    }
                    while (cur > 0 && i - cur + 1 >= 0 && i + cur < n && s[i - cur + 1] == s[i + cur]) {
                        ++cur;
                    }
                    if (cur > 0) {
                        --cur;
                    }
                    r[i] = cur;
                    if (rightmost < i + cur) {
                        rightmost = i + cur;
                        iForRightmost = i;
                    }
                }
                return r;
            }

        }

    }

    static class FastScanner {
        private BufferedReader in;
        private StringTokenizer st;

        public FastScanner(InputStream stream) {
            in = new BufferedReader(new InputStreamReader(stream));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

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

    }
}