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 javafx.util.Pair;
import java.io.*;
import java.util.*;
import static java.lang.Math.*;

public class Main implements Runnable
{
    boolean multiple = true;
    long MOD = 998244353;

    @SuppressWarnings("Duplicates")
    void solve() throws Exception
    {
        String s = sc.nextToken();
        int n = s.length();
        int ends = 0;
        for (; ends < n / 2;)
        {
            if (s.charAt(ends) != s.charAt(n - 1 - ends))
                break;
            ends++;
        }

        if (ends == n / 2)
        {
            System.out.println(s);
            return;
        }

        String ans1 = s.substring(0, ends);
        String ans2 = s.substring(n - 1 - ends + 1);
        s = s.substring(ends, n - 1 - ends + 1);
        n = s.length();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++)
            arr[i] = s.charAt(i);
        forward1 = new long[n];
        forward2 = new long[n];
        backward1 = new long[n];
        backward2 = new long[n];
        pow1 = new long[n];
        pow2 = new long[n];
        inv1 = new long[n];
        inv2 = new long[n];
        pow1[0] = 1; pow2[0] = 1;
        for (int i = 1; i < n; i++) pow1[i] = (pow1[i - 1] * p1) % MOD;
        for (int i = 1; i < n; i++) pow2[i] = (pow2[i - 1] * p2) % MOD;
        for (int i = 0; i < n; i++) inv1[i] = inv(pow1[i], MOD);
        for (int i = 0; i < n; i++) inv2[i] = inv(pow2[i], MOD);
        computeForward(arr);
        computeBackward(arr);

        int right = 0;
        for (int start = 0; start < n; start++)
            if (palindrome(arr, start, n - 1))
                right = max(right, n - start);

        int left = 0;
        for (int end = 0; end < n; end++)
            if (palindrome(arr, 0, end))
                left = max(left, end + 1);

        if (left > right)
            System.out.println(ans1 + s.substring(0, left) + ans2);
        else
            System.out.println(ans1 + s.substring(s.length() - right) + ans2);
    }

    long[] forward1, backward1;
    long[] forward2, backward2;

    long p1 = 249561083, p2 = 499122163;
    long[] inv1, inv2;
    long[] pow1, pow2;

    void computeForward(int[] arr)
    {
        forward1[0] = arr[0];
        for (int i = 1; i < arr.length; i++)
            forward1[i] = (forward1[i - 1] + ((arr[i] * pow1[i]) % MOD)) % MOD;
        forward2[0] = arr[0];
        for (int i = 1; i < arr.length; i++)
            forward2[i] = (forward2[i - 1] + ((arr[i] * pow2[i]) % MOD)) % MOD;
    }

    void computeBackward(int[] temp)
    {
        int n = temp.length;
        int[] arr = new int[n];
        for (int i = 0; i < n; i++)
            arr[i] = temp[n - i - 1];
        backward1[0] = arr[0];
        for (int i = 1; i < arr.length; i++)
            backward1[i] = (backward1[i - 1] + ((arr[i] * pow1[i]) % MOD)) % MOD;
        backward2[0] = arr[0];
        for (int i = 1; i < arr.length; i++)
            backward2[i] = (backward2[i - 1] + ((arr[i] * pow2[i]) % MOD)) % MOD;
    }

    boolean palindrome(int[] arr, int i, int j)
    {
        if (i == j) return true;
        if (i == j - 1) return arr[i] == arr[i + 1];
        int chars = (j - i + 1) / 2;
        int n = arr.length;
        int start1 = i, end1 = i + chars - 1;
        int start2 = n - 1 - j, end2 = n - 1 - (j - chars + 1);

        long hash1 = ((forward1[end1] - (start1 == 0 ? 0 : forward1[start1 - 1])) + 800 * MOD) % MOD;
        hash1 = (hash1 * inv1[start1]) % MOD;
        long hash2 = ((backward1[end2] - (start2 == 0 ? 0 : backward1[start2 - 1])) + 800 * MOD) % MOD;
        hash2 = (hash2 * inv1[start2]) % MOD;

        if (hash1 != hash2) return false;

        hash1 = ((forward2[end1] - (start1 == 0 ? 0 : forward2[start1 - 1])) + 800 * MOD) % MOD;
        hash1 = (hash1 * inv2[start1]) % MOD;
        hash2 = ((backward2[end2] - (start2 == 0 ? 0 : backward2[start2 - 1])) + 800 * MOD) % MOD;
        hash2 = (hash2 * inv2[start2]) % MOD;

        return hash1 == hash2;
    }

    class Node implements Comparable<Node>
    {
        int k, pos, idx;
        Node(int K, int P, int I)
        {
            k = K;
            pos = P;
            idx = I;
        }

        @Override
        public int compareTo(Node o)
        {
            return Integer.compare(k, o.k);
        }
    }


    long powmod(long x, long y){
        if(y==0) return 1L;
        long t=powmod(x,y/2);
        if (y%2==0) return (t*t)%MOD;
        return (((x*t)%MOD)*t)%MOD;
    }
    long inv(long a, long b){return 1<a ? b - inv(b%a,a)*b/a : 1;}
    TreeMap<Integer, TreeSet<Integer>> buildGraph(int n, int m) throws Exception{TreeMap<Integer, TreeSet<Integer>> graph = new TreeMap<>();for (int i = 1; i <= n; i++) graph.put(i, new TreeSet<>());for (int i = 0; i < m; i++) { int u = sc.nextInt();int v = sc.nextInt();graph.get(u).add(v);graph.get(v).add(u); }return graph; }
    TreeMap<Integer, TreeSet<Integer>> buildDirGraph(int n, int m) throws Exception { TreeMap<Integer, TreeSet<Integer>> graph = new TreeMap<>();for (int i = 1; i <= n; i++) graph.put(i, new TreeSet<>());for (int i = 0; i < m; i++) graph.get(sc.nextInt()).add(sc.nextInt());return graph; }
    TreeMap<Integer, TreeSet<Integer>> revGraph(TreeMap<Integer, TreeSet<Integer>> graph) { TreeMap<Integer, TreeSet<Integer>> rev = new TreeMap<>();for (Integer u : graph.keySet()) rev.put(u, new TreeSet<>());for (Integer u : graph.keySet()) for (Integer v : graph.get(u)) rev.get(v).add(u);return rev; }
    TreeMap<Long, Integer> counts(long[] arr) { TreeMap<Long, Integer> counts = new TreeMap<>(); for (long a : arr) { if (!counts.containsKey(a)) counts.put(a, 0); counts.put(a, counts.get(a) + 1);} return counts; }
    String reverse(String s, int i, int j) /*[i, j] closed*/ { return (i > j) ? reverse(s, j, i) : s.substring(0, i) + (new StringBuilder(s.substring(i, j + 1))).reverse() + ((j + 1 <= s.length() - 1) ? s.substring(j + 1) : "");}

    @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in));out = new PrintWriter(System.out);sc = new FastScanner(in);if (multiple) { int q = sc.nextInt();for (int i = 0; i < q; i++) solve(); } else solve(); } catch (Throwable uncaught) { Main.uncaught = uncaught; } finally { out.close(); }} public static void main(String[] args) throws Throwable{ Thread thread = new Thread(null, new Main(), "", (1 << 26));thread.start();thread.join();if (Main.uncaught != null) {throw Main.uncaught;} } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) {this.in = in;}public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); }return st.nextToken(); }public int nextInt() throws Exception { return Integer.parseInt(nextToken()); }public long nextLong() throws Exception { return Long.parseLong(nextToken()); }public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
}