#P1844F2. Min Cost Permutation (Hard Version)

    ID: 8527 Type: RemoteJudge 3000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>binary searchconstructive algorithmsdata structuresgreedymathsortings

Min Cost Permutation (Hard Version)

No submission language available for this problem.

Description

The only difference between this problem and the easy version is the constraints on tt and nn.

You are given an array of nn positive integers a1,,ana_1,\dots,a_n, and a (possibly negative) integer cc.

Across all permutations b1,,bnb_1,\dots,b_n of the array a1,,ana_1,\dots,a_n, consider the minimum possible value of i=1n1bi+1bic.\sum_{i=1}^{n-1} |b_{i+1}-b_i-c|. Find the lexicographically smallest permutation bb of the array aa that achieves this minimum.

A sequence xx is lexicographically smaller than a sequence yy if and only if one of the following holds:

  • xx is a prefix of yy, but xyx \ne y;
  • in the first position where xx and yy differ, the sequence xx has a smaller element than the corresponding element in yy.

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and cc (1n21051 \le n \le 2 \cdot 10^5, 109c109-10^9 \le c \le 10^9).

The second line of each test case contains nn integers a1,,ana_1,\dots,a_n (1ai1091 \le a_i \le 10^9).

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

For each test case, output nn integers b1,,bnb_1,\dots,b_n, the lexicographically smallest permutation of aa that achieves the minimum i=1n1bi+1bic\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c|.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and cc (1n21051 \le n \le 2 \cdot 10^5, 109c109-10^9 \le c \le 10^9).

The second line of each test case contains nn integers a1,,ana_1,\dots,a_n (1ai1091 \le a_i \le 10^9).

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, output nn integers b1,,bnb_1,\dots,b_n, the lexicographically smallest permutation of aa that achieves the minimum i=1n1bi+1bic\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c|.

Sample Input 1

3
6 -7
3 1 4 1 5 9
3 2
1 3 5
1 2718
2818

Sample Output 1

9 3 1 4 5 1
1 3 5
2818

Note

In the first test case, it can be proven that the minimum possible value of i=1n1bi+1bic\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c| is 2727, and the permutation b=[9,3,1,4,5,1]b = [9,3,1,4,5,1] is the lexicographically smallest permutation of aa that achieves this minimum: 39(7)+13(7)+41(7)+54(7)+15(7)=1+5+10+8+3=27|3-9-(-7)|+|1-3-(-7)|+|4-1-(-7)|+|5-4-(-7)|+|1-5-(-7)| = 1+5+10+8+3 = 27.

In the second test case, the minimum possible value of i=1n1bi+1bic\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c| is 00, and b=[1,3,5]b = [1,3,5] is the lexicographically smallest permutation of aa that achieves this.

In the third test case, there is only one permutation bb.