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.util.*;
import java.util.stream.*;
import java.io.*;
import java.math.*;

public class Main { 
    
    static boolean FROM_FILE = false;
    
    static class FastReader { 
        BufferedReader br; 
        StringTokenizer st; 
        public FastReader() { 
            if (FROM_FILE) {
                try {
                    br = new BufferedReader(new FileReader("input.txt")); 
                } catch (IOException error) {
                }   
            } else {
                br = new BufferedReader(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; 
        } 
    }
    static int max(int... nums) {
        int res = Integer.MIN_VALUE;
        for (int num: nums) res = Math.max(res, num);
        return res;
    }
    static int min(int... nums) {
        int res = Integer.MAX_VALUE;
        for (int num: nums) res = Math.min(res, num);
        return res;
    }
    static long max(long... nums) {
        long res = Long.MIN_VALUE;
        for (long num: nums) res = Math.max(res, num);
        return res;
    }
    static long min(long... nums) {
        long res = Long.MAX_VALUE;
        for (long num: nums) res = Math.min(res, num);
        return res;
    }
    
    static FastReader fr = new FastReader();
    static PrintWriter out;
    
    static class ManachersAlgorithm {
    
        public static String findLongestPalindrome(String s) {
            if (s==null || s.length()==0) return "";
            char[] s2 = addBoundaries(s.toCharArray());
            int[] p = getPArray(s2);
            int len = 0, c = 0;
            for (int i = 1; i < s2.length; i += 1) {
                if (len < p[i]) {
                    len = p[i]; c = i;
                }
            }
            char[] ss = Arrays.copyOfRange(s2, c-len, c+len+1);
            return String.valueOf(removeBoundaries(ss));
        }
        
        public static String findLongestPrefixOrSuffixPalindrome(String s) {
            if (s == null || s.length() == 0) return "";
            char[] s2 = addBoundaries(s.toCharArray());
            int[] p = getPArray(s2);
            int len = 0, c = 0;
            for (int i = 1; i < s2.length; i += 1) {
                if (p[i] == i || p[i] == s2.length - 1 - i) {
                    if (len < p[i]) {
                        len = p[i]; c = i;
                    }
                }
            }
            char[] ss = Arrays.copyOfRange(s2, c-len, c+len+1);
            return String.valueOf(removeBoundaries(ss));
        }
        
        private static int[] getPArray(char[] s2) {
            int[] p = new int[s2.length];
            int c = 0, r = 0; // Here the first element in s2 has been processed.
            int m = 0, n = 0; // The walking indices to compare if two elements are the same.
            for (int i = 1; i < s2.length; i += 1) {
                if (i > r) {
                    p[i] = 0; m = i - 1; n = i + 1;
                } else {
                    int i2 = c * 2 - i;
                    if (p[i2] < r - i) {
                        p[i] = p[i2];
                        m = -1; // This signals bypassing the while loop below. 
                    } else {
                        p[i] = r-i;
                        n = r + 1; m = i * 2 - n;
                    }
                }
                while (m>=0 && n<s2.length && s2[m]==s2[n]) {
                    p[i]++; m--; n++;
                }
                if ((i+p[i])>r) {
                    c = i; r = i+p[i];
                }
            }
            return p;
        } 
     
        private static char[] addBoundaries(char[] cs) {
            if (cs==null || cs.length==0)
                return "||".toCharArray();
    
            char[] cs2 = new char[cs.length*2+1];
            for (int i = 0; i<(cs2.length-1); i = i+2) {
                cs2[i] = '|';
                cs2[i+1] = cs[i/2];
            }
            cs2[cs2.length-1] = '|';
            return cs2;
        }
    
        private static char[] removeBoundaries(char[] cs) {
            if (cs==null || cs.length<3)
                return "".toCharArray();
    
            char[] cs2 = new char[(cs.length-1)/2];
            for (int i = 0; i<cs2.length; i++) {
                cs2[i] = cs[i*2+1];
            }
            return cs2;
    }    
    }
    
    public static void main(String[] args) { 
        if (FROM_FILE) {
            try {
                out = new PrintWriter(new FileWriter("output.txt"));
            } catch (IOException error) {
                
            }
        } else {
            out = new PrintWriter(new OutputStreamWriter(System.out));
        }
        new Main().run();
        out.flush();
        out.close();
    } 
    
    void run() {
        int t = fr.nextInt();
        while (t-- > 0) {
            String str = fr.nextLine();
            String s = ManachersAlgorithm.findLongestPrefixOrSuffixPalindrome(str);
            int i = 0, n = str.length(), j = n - 1;
            while (i < j && str.charAt(i) == str.charAt(j)) { i += 1; j -= 1; }
            if (i != 0) {
                String s2 = str.substring(0, i)
                    + ManachersAlgorithm.findLongestPrefixOrSuffixPalindrome(str.substring(i, j + 1))
                    + str.substring(j + 1, n);
                s = s.length() >= s2.length() ? s : s2;
            }
            out.println(s);
        }
    }
}