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.*;
import java.util.*;
public class Contest1 {
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////                                                                                                               /////////
////////                                                                                                               /////////
////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMM          MMMM         OOOOOO             SSSSSSS      EEEEEEEEEEEEE  /////////
////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMMMM      MMMMMM      OOO      OOO        SSSS   SSS     EEEEEEEEEEEEE  /////////
////////   HHHH        HHHH  EEEEE           MMMM MMM  MMM MMMM    OOO          OOO    SSSS       SSS   EEEEE          /////////
////////   HHHH        HHHH  EEEEE           MMMM  MMMMMM  MMMM   OOO            OOO   SSSS             EEEEE          /////////
////////   HHHH        HHHH  EEEEE           MMMM          MMMM  OOO              OOO   SSSSSSS         EEEEE          /////////
////////   HHHHHHHHHHHHHHHH  EEEEEEEEEEE     MMMM          MMMM  OOO              OOO      SSSSSS       EEEEEEEEEEE    /////////
////////   HHHHHHHHHHHHHHHH  EEEEEEEEEEE     MMMM          MMMM  OOO              OOO         SSSSSSS   EEEEEEEEEEE    /////////
////////   HHHH        HHHH  EEEEE           MMMM          MMMM   OOO            OOO              SSSS  EEEEE          /////////
////////   HHHH        HHHH  EEEEE           MMMM          MMMM    OOO          OOO     SSS       SSSS  EEEEE          /////////
////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMM          MMMM      OOO      OOO        SSS    SSSS    EEEEEEEEEEEEE  /////////
////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMM          MMMM         OOOOOO             SSSSSSS      EEEEEEEEEEEEE  /////////
////////                                                                                                               /////////
////////                                                                                                               /////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    static boolean check(String s) {

        for (int i = 0; i < s.length() / 2; i++) {
            if (s.charAt(i) != s.charAt(s.length() - i - 1))
                return false;
        }
        return true;
    }

    static int[][] manacher(char[] s) {
        int n = s.length, d1[] = new int[n], d2[] = new int[n];

        //Calculation of odd-length palindromes
        for (int i = 0, l = 0, r = -1; i < n; ++i) {
            int k = i > r ? 1 : Math.min(r - i + 1, d1[l + r - i]);
            while (i + k < n && i - k >= 0 && s[i + k] == s[i - k])
                ++k;
            d1[i] = k--;
            if (i + k > r) {
                l = i - k;
                r = i + k;
            }
        }

        //Calculation of even-length palindromes
        //you may insert # between characters of s to get rid of the following part
        for (int i = 0, l = 0, r = -1; i < n; ++i) {
            int k = i > r ? 0 : Math.min(r - i + 1, d2[l + r - i + 1]);
            while (i + k < n && i - k - 1 >= 0 && s[i + k] == s[i - k - 1])
                ++k;
            d2[i] = k--;
            if (i + k > r) {
                l = i - k - 1;
                r = i + k;
            }
        }

        return new int[][]{d1, d2};
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        int t = sc.nextInt();
//        pw.println(Arrays.deepToString(manacher(new char[]{'a','a','a','a','a'})));
        while (t-- > 0) {
            String all = sc.nextLine();
            char[] s = all.toCharArray();

            if (check(all)) {
                pw.println(all);
                continue;
            }
            int end = 0;
            ArrayList<Character> need = new ArrayList<>();
            for (int i = 0; i < s.length / 2; i++) {
                if (s[i] == s[s.length - i - 1]) {
                    need.add(s[i]);
                } else break;
                end++;
            }
            int [] [] sol= manacher(all.substring(end,s.length-need.size()).toCharArray());
//            pw.println(Arrays.deepToString(sol));
            int max11=0;
            int max12=0;
            int max21=0;
            int max22=0;
            for (int i =0;i<sol[0].length;i++){
                if ((sol[0][i]==i+1)){
                    max11=i+1;
                }
            }
            for (int i =sol[0].length-1;i>=0;i--){
                if ((sol[0][i]==sol[0].length-i)){
                    max21=sol[0].length-i;
                }
            }
            for (int i =0;i<sol[0].length;i++){
                if ((sol[1][i]==i)){
                    max12=i;
                }
            }
            for (int i =sol[0].length-1;i>=0;i--){
                if ((sol[1][i]==sol[0].length-i)){
                    max22=sol[0].length-i;
                }
            }
            String s1="";
            String s2="";
            if (2*max12>2*(max11-1)+1){
                s1=all.substring(end,end+2*max12);
            }
            else {
                s1=all.substring(end,end+2*(max11-1)+1);
            }
            if (2*max22>2*(max21-1)+1){
                s2=all.substring(s.length-need.size()-2*max22,s.length-need.size());
            }
            else {
                s2=all.substring(s.length-need.size()-(2*(max21-1)+1),s.length-need.size());
            }
            String ans="";
            if (s1.length()>s2.length()){
                ans=s1;
            }
            else {
                ans=s2;
            }
            LinkedList<String>ll = new LinkedList<>();
            ll.addLast(ans);
            for (int i =need.size()-1;i>=0;i--){
                ll.addFirst(""+need.get(i));
                ll.addLast(""+need.get(i));
            }
            while (!ll.isEmpty()){
                pw.print(ll.pollFirst());
            }
            pw.println();
        }
        pw.flush();
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

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

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

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }
    }

}