Given an array of **N** distinct elements and a number **K**. The task is to arrange array elements according to the absolute difference with K, i. e., element having minimum difference comes first and so on.

**Note :** If two or more elements are at equal distance arrange them in same sequence as in the given array.

**Input:**

First line of input contains a single integer T which denotes the number of test cases. Then T test cases follows. First line of test case contains two space separated integers N and K. Second line of each test case contains N space separated integers.

**Output:**

For each test case print the given array in the order as described above.

**Constraints:**

1 <= T <= 100

1 <= N <= 10^{5}

1 <= K <= 10^{5}

**Example:**

**Input:**

3

5 7

10 5 3 9 2

5 6

1 2 3 4 5

4 5

2 6 8 3

**Output:**

5 9 10 3 2

5 4 3 2 1

6 3 2 8

**Explanation:
Testcase 1:** Sorting the numbers accoding to the absolute difference with 7, we have array elements as 5, 9, 10, 3, 2.

Author: harsh.agarwal0

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

SashaV | 206 |

VivSisMMM | 204 |

GustavoMéndez | 191 |

d_Coder00 | 189 |

GHJKL | 174 |

Pulkit__Sharma__ | 726 |

Anirban166 | 677 |

Found_me | 582 |

abducodes | 566 |

Sathvik Avasarala | 518 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4559 |

Login to report an issue on this page.