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".
// package global7;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * codeforces
 *
 * @author 郭靖/fangxue.zhang
 * @date 2021-08-08
 * TODO COMMENT
 */
public class D2 {


    // 生成新的辅助String, eg: abc123成为#a#b#c#1#2#3#2#1#
    public static char[] manacherStringString(String str) {
        char[] c = str.toCharArray();
        char[] res = new char[c.length * 2 + 1];
        int index = 0;
        for (int i = 0; i != res.length; i++) {
            // 两个一样效果, 填充符号"#"
            res[i] = (i % 2) == 0 ? '#' : c[index++];
            // res[i] = (i & 1) == 0 ? '#' : c[index++];
        }
        return res;
    }

    // 返回最长回文串长度
    public static int[] maxLcpsLength(String str) {
        if (str == null || str.length() == 0) {
            return new int[0];
        }
        char[] charArr = manacherStringString(str);
        // 辅助回文长度数组, 表示最多能扩充多少
        int[] pArr = new int[charArr.length];
        // 中心点
        int C = -1;
        // 最右边界
        int R = -1;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i != charArr.length; i++) {
            // i在右边界内, i`到C的长度和到i到R的距离, 哪个小, 哪个就是起码成为回文的区域
            // 否则为1, 自身
            pArr[i] = R > i ? Math.min(pArr[2 * C - i], R - i) : 1;
            // 检查边界
            while (i + pArr[i] < charArr.length && i - pArr[i] > -1) {
                if (charArr[i + pArr[i]] == charArr[i - pArr[i]]) {
                    // 左右字母相等, 扩1, 直到不能扩为止
                    pArr[i]++;
                } else {
                    // 不能扩
                    break;
                }
            }
            // 如果大于R, 那更新回文右边界和中心点
            if ((i + pArr[i]) > R) {
                R = i + pArr[i];
                C = i;
            }
            // 如果需要, 则更新max
            max = Math.max(max, pArr[i]);
        }
        // 返回最大回文长度
        return pArr;
    }




    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while(T > 0){
            T--;
            String s = in.next();

            int N = s.length();
            if(N == 1){
                System.out.println(s);
                continue;
            }
            int left = 0;
            int right = N - 1;
            while(left <= right && s.charAt(left) == s.charAt(right)){
                left++;
                right--;
            }

            if(left >= right){
                System.out.println(s);
                continue;
            }

            String tmp = s.substring(left, right + 1);
            char[] palinStr = manacherStringString(tmp);
            int[] palin = maxLcpsLength(tmp);
            int max = -1;
            int pos = -1;
            for(int i = 0; i < palin.length; i++){
                int len = palin[i] - 1;
                if(i + len == palin.length - 1 || i - len == 0){
                    if(max < len){
                        max = len;
                        pos = i;
                    }
                }
            }

            Deque<Character> q = new LinkedList<Character>();
            for(int i = left - 1; i >= 0; i--){
                q.addFirst(s.charAt(i));
            }
            if(pos != -1){
                for(int j = pos - max; j <= pos + max; j++){
                    if(palinStr[j] == '#'){
                        continue;
                    }
                    q.addLast(palinStr[j]);

                }

            }
            for(int i = right + 1; i < N; i++){
                q.addLast(s.charAt(i));
            }
            StringBuilder sb = new StringBuilder();
            for(char c : q){
                sb.append(c);
            }
            System.out.println(sb.toString());




        }
    }

}
