Read problems statements in Mandarin Chinese and Russian as well. 

Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D.
Find out maximum possible sum of all good disjoint pairs that can be made from these numbers.
Sum of X pairs is the sum of all 2*X numbers in the pairs.

------ Input ------ 

First line contains T, the number of test cases to follow.
First line of each test case contains 2 space separated integers: N and D.
Second line of each test case contains N space separated integers.

------ Output ------ 

For each test case, output the answer in a separate line.

------ Constraints ------ 

$1 ≤ T, N, D, Array Elements ≤ 10^{5}$
$1 ≤ Sum of N over all test cases ≤ 5*10^{5}$

------ Note: ------ 
 
Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct.

----- Sample Input 1 ------ 
3
3 3
3 5 8
4 3
5 8 10 12
5 3
3 2 8 17 15
----- Sample Output 1 ------ 
8
22
37
----- explanation 1 ------ 
Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2.  

Test Case 3: You can take pairs(3,2) and (15,17) as the answer.

Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Greedy algorithms
Skill types: Dynamic programming

Greedy algorithms

Starter code:

Solutions:
def max_sum_disjoint_pairs(sort_arr, diff):
	n = len(sort_arr)
	i = 0
	res = 0
	while i < n - 1:
		if sort_arr[i] - sort_arr[i + 1] < diff:
			res += sort_arr[i] + sort_arr[i + 1]
			i += 2
		else:
			i += 1
	return res
T = int(input())
for i in range(T):
	diff = int(input().split()[1])
	sort_arr = list(map(int, input().split()))
	sort_arr.sort()
	print(max_sum_disjoint_pairs(sort_arr[::-1], diff))


for _ in range(int(input())):
	(N, D) = map(int, input().split())
	array = list(map(int, input().split()))
	array.sort()
	i = N - 1
	sm = 0
	while i > 0:
		if array[i] - array[i - 1] < D:
			sm += array[i] + array[i - 1]
			i -= 2
		else:
			i -= 1
	print(sm)


test = int(input())
for _ in range(test):
	(n, d) = map(int, input().split())
	a = list(map(int, input().split()))
	s = 0
	b = sorted(a)
	i = n - 1
	while i >= 1:
		if b[i] - b[i - 1] < d:
			s += b[i] + b[i - 1]
			i -= 2
		else:
			i -= 1
	print(s)


for t in range(int(input())):
	(n, d) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	a.reverse()
	i = 1
	ans = 0
	while 1:
		if i > n - 1:
			break
		elif a[i - 1] - a[i] < d:
			ans = ans + a[i - 1] + a[i]
			i = i + 2
		else:
			i = i + 1
	print(ans)


for _ in range(int(input())):
	(n, d) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.sort()
	ans = 0
	i = n - 1
	while i >= 1:
		if arr[i] - arr[i - 1] < d:
			ans += arr[i] + arr[i - 1]
			i -= 2
		else:
			i -= 1
	print(ans)


for t in range(int(input())):
	(n, d) = map(int, input().split())
	a = list(map(int, input().split()))
	a = sorted(a, reverse=True)
	ans = 0
	i = 0
	while i < n - 1:
		if a[i] - a[i + 1] < d:
			ans += a[i] + a[i + 1]
			i += 2
		else:
			i += 1
	print(ans)


t = int(input())
for T in range(t):
	(n, d) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	a.sort(reverse=True)
	(ind, ind1) = (0, 1)
	ans = []
	while ind < n - 1:
		if a[ind] - a[ind + 1] < d:
			ans.append(a[ind])
			ans.append(a[ind + 1])
			ind += 2
			continue
		ind += 1
	print(sum(ans))


for _ in range(int(input())):
	(n, d) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	i = n - 1
	s = 0
	while i >= 1:
		if l[i] - l[i - 1] < d:
			s += l[i] + l[i - 1]
			i -= 2
		else:
			i -= 1
	print(s)


from sys import stdin, stdout

def sin():
	return stdin.readline().rstrip()

def listInput():
	return list(map(int, sin().split()))

def printBS(li):
	if not li:
		return
	for i in range(len(li) - 1):
		stdout.write('%d ' % li[i])
	stdout.write('%d\n' % li[-1])
t = int(sin())
from bisect import bisect_left
for _ in range(t):
	(n, d) = listInput()
	li = listInput()
	li.sort()
	ans = 0
	i = n - 1
	while i > 0:
		if li[i] - li[i - 1] < d:
			ans += li[i] + li[i - 1]
			i -= 2
		else:
			i -= 1
	print(ans)


T = int(input())
for _ in range(T):
	(n, d) = map(int, input().split())
	L = list(map(int, input().split()))
	L.sort(reverse=True)
	ans = 0
	i = 0
	while i < n - 1:
		if L[i] - L[i + 1] < d:
			ans += L[i] + L[i + 1]
			i += 2
		else:
			i += 1
	print(ans)


T = int(input())
for i in range(T):
	sum1 = 0
	(N, D) = map(int, input().split())
	s = list(map(int, input().split()))
	s.sort()
	j = N - 1
	while j > 0:
		if s[j] - s[j - 1] < D:
			sum1 += s[j] + s[j - 1]
			j -= 2
		else:
			j -= 1
	print(sum1)


t = int(input().strip())
for i in range(t):
	sum = 0
	(n, d) = map(int, input().strip().split())
	a = map(int, input().strip().split())
	a = sorted(a, reverse=True)
	present = [1] * n
	for i in range(len(a)):
		if present[i]:
			for j in range(i + 1, len(a)):
				if present[j] and a[i] - a[j] >= d:
					break
				if present[j] and a[i] - a[j] < d:
					sum += a[i] + a[j]
					present[j] = 0
					present[i] = 0
					break
	print(sum)


t = int(input())
for x in range(t):
	(n, d) = map(int, input().split())
	l = list(map(int, input().split()))
	p1 = 0
	p2 = 1
	sums = 0
	p = l[:]
	l.sort()
	i = n - 1
	flag = 0
	i = n - 1
	j = i - 1
	iprev = -20
	while j >= 0 and len(l) > 1:
		if abs(l[i] - l[j]) < d:
			sums += l[i] + l[j]
			l.pop(-1)
			l.pop(-1)
			i = len(l) - 1
			j = i - 1
		else:
			l.pop(-1)
			i = len(l) - 1
			j = i - 1
	print(sums)


def integer_list():
	return list(map(int, input().split()))

def string_list():
	return list(map(str, input().split()))

def hetro_list():
	return list(input().split())
import math
import sys
from collections import Counter

def main():
	lst.sort()
	s = 0
	while lst:
		ele = lst.pop()
		if lst:
			if ele - lst[-1] < d:
				s += ele + lst.pop()
	print(s)
t = int(input())
for _ in range(t):
	(n, d) = integer_list()
	lst = integer_list()
	main()


def sumpair():
	for _ in range(int(input())):
		(n, d) = map(int, input().split())
		a = list(map(int, input().split()))
		a.sort(reverse=True)
		i = 0
		ksum = 0
		while i < len(a) - 1:
			if a[i] - a[i + 1] < d:
				ksum += a[i] + a[i + 1]
				i += 2
			else:
				i += 1
		print(ksum)
sumpair()


for x in range(int(input())):
	(n, d) = map(int, input().split())
	li = list(map(int, input().split()))
	li = sorted(li)[::-1]
	j = 0
	ans = 0
	while j < n - 1:
		if li[j] - li[j + 1] < d:
			ans += li[j] + li[j + 1]
			j += 2
		else:
			j += 1
	print(ans)


t = int(input())
for _ in range(t):
	(n, p) = map(int, input().split())
	flag = 0
	sum = 0
	l = list(map(int, input().split()))
	l.sort()
	for i in range(n - 1, 0, -1):
		if flag == 1:
			flag = 0
			continue
		elif abs(l[i] - l[i - 1]) < p:
			sum = sum + l[i] + l[i - 1]
			flag = 1
	print(sum)


t = int(input())
for _ in range(t):
	(n, d) = map(int, input().split())
	numbers = [int(number) for number in input().split()]
	numbers.sort()
	result = 0
	i = n - 1
	while i > 0:
		if numbers[i] - numbers[i - 1] < d:
			result += numbers[i] + numbers[i - 1]
			i -= 2
		else:
			i -= 1
	print(result)


def sumpair(n, d, arr):
	arr.sort()
	i = n - 1
	s = 0
	while i > 0:
		if arr[i] - arr[i - 1] < d:
			s += arr[i - 1] + arr[i]
			i -= 2
		else:
			i -= 1
	return s
t = int(input())
for _ in range(t):
	(n, d) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(sumpair(n, d, arr))


import math
T = int(input())
for t in range(T):
	(N, D) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	A.sort()
	A.reverse()
	count = 0
	i = 0
	while i < len(A) - 1:
		if A[i] - A[i + 1] < D:
			count = count + A[i] + A[i + 1]
			i += 2
		else:
			i += 1
	print(count)


for t in range(int(input())):
	(n, d) = map(int, input().split())
	li = list(map(int, input().split()))
	li.sort(reverse=True)
	sm = 0
	x = 0
	while x < len(li) - 1:
		if li[x] - li[x + 1] < d:
			sm += li[x] + li[x + 1]
			x += 1
		x += 1
	print(sm)


for t in range(int(input())):
	temp = list(map(int, input().split()))
	n = temp[0]
	d = temp[1]
	a = list(map(int, input().split()))
	a.sort(reverse=True)
	ans = 0
	if n == 1:
		print(0)
		continue
	p1 = 0
	p2 = 1
	ans = 0
	while p1 < n and p2 < n:
		if a[p1] - a[p2] < d:
			ans += a[p1] + a[p2]
			p1 = p2 + 1
			p2 = p1 + 1
			continue
		else:
			p1 = p2 + 1
			if p1 > p2:
				tt = p1
				p1 = p2
				p2 = tt
	print(ans)


t = int(input())
for i in range(t):
	C = list(map(int, input().split()))
	A = list(map(int, input().split()))
	A.sort()
	a = C[0] - 1
	sum = 0
	while a > 0:
		if A[a] - A[a - 1] < C[1]:
			sum += A[a] + A[a - 1]
			a -= 1
		a -= 1
	print(sum)


from bisect import bisect_left
from heapq import heapify, heappush, heappop
from collections import deque
import sys
from math import gcd, sqrt, log10, log2, floor, ceil, sqrt
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

def il():
	return [int(a) for a in input().strip().split()]

def ip():
	return input().strip()

def ii():
	return int(input())

def ot(in_p, *args, e=' '):
	if type(in_p) == list:
		print(*in_p, end=e)
	if type(in_p) == str or type(in_p) == int:
		print(in_p, end=e)
		for l in args:
			print(l, end=e)
	print()

def a_pwr_n(a, n, mod):
	if n == 1:
		return a % mod
	if n % 2 == 0:
		val = a_pwr_n(a, n // 2, mod)
		return val % mod * val % mod
	else:
		return a % mod * a_pwr_n(a, n - 1, mod) % mod

def rg(*ag):
	if len(ag) == 3:
		return range(ag[0], ag[1], ag[2])
	elif len(ag) == 2:
		return range(ag[0], ag[1])
	elif len(ag) == 1:
		return range(ag[0])
	else:
		raise 'invalid input'

def ispal(i, j, st):
	while i <= j:
		if st[i] == st[j]:
			i = i + 1
			j = j - 1
		else:
			return False
	return True
mod = 1000000000 + 7
dx = [-1, -2, 1, 2, 2, -2, -1, 1]
dy = [2, 1, 2, 1, -1, -1, -2, -2]
for _ in range(ii()):
	(n, d) = il()
	arr = il()
	ans = 0
	arr.sort()
	i = n - 1
	while i >= 1:
		if arr[i] - arr[i - 1] < d:
			ans += arr[i] + arr[i - 1]
			i -= 2
		else:
			i -= 1
	print(ans)


TC = int(input())
for _ in range(TC):
	(N, D) = list(map(int, input().strip().split()))
	arr = list(map(int, input().strip().split()))
	arr.sort(reverse=True)
	total = 0
	i = 1
	while i < N:
		if arr[i - 1] - arr[i] < D:
			total += arr[i] + arr[i - 1]
			i += 1
		i += 1
	print(total)


t = int(input())
for I in range(t):
	(n, d) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	a.sort()
	sum1 = 0
	i = n - 1
	while i > 0:
		if a[i] - a[i - 1] < d:
			sum1 += a[i] + a[i - 1]
			i -= 2
		else:
			i -= 1
	print(sum1)


t = int(input())
while t != 0:
	(n, d) = map(int, input().split())
	a = list(map(int, input().split()))
	s = 0
	b = sorted(a)
	i = n - 1
	while i >= 1:
		if b[i] - b[i - 1] < d:
			s += b[i] + b[i - 1]
			i -= 2
		else:
			i -= 1
	print(s)
	t -= 1


t = int(input())
for _ in range(t):
	(n, d) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.sort(reverse=False)
	ans = 0
	index = n - 1
	while index > 0:
		if arr[index] - arr[index - 1] < d:
			ans += arr[index] + arr[index - 1]
			index -= 2
		else:
			index -= 1
	print(ans)


for _ in range(int(input())):
	(n, d) = map(int, input().split())
	nli = list(map(int, input().split()))
	nli.sort(reverse=True)
	lst = list()
	ind = 0
	while ind < n - 1:
		if nli[ind] - nli[ind + 1] < d:
			lst.append(nli[ind + 1])
			lst.append(nli[ind])
			ind += 2
		else:
			ind += 1
	print(sum(lst))


t = int(input())
while t:
	t -= 1
	(n, d) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	a.sort()
	a.reverse()
	i = 1
	ans = 0
	while i < n:
		if a[i - 1] - a[i] < d:
			ans += a[i] + a[i - 1]
			i += 2
		else:
			i += 1
	print(ans)


def io(func=str, n=1):

	def nfp():
		ff = str
		try:
			it = iter(func)

			def nf(val):
				nonlocal ff
				ff = next(it, ff)
				return ff(val)
			return type(func)((nf(va) for va in input().split()))
		except TypeError:
			return func(input())
	if type(n) is list:
		return [_(func, x) for x in n]
	else:
		t = lambda : nfp()
		if n == 1:
			return t()
		elif n == 0:
			return [t() for i in range(int(input()))]
		else:
			return [t() for i in range(n)]

def main():
	T = io(int)
	for i in range(T):
		(N, D) = io((int, int))
		data = sorted(io([int]))[::-1]
		prec = None
		s = 0
		for elt in data:
			if prec is not None:
				if prec - elt < D:
					s += elt + prec
					prec = None
				else:
					prec = elt
			else:
				prec = elt
		yield s
print('\n'.join([str(res) for res in main()]))


from typing import List

def soln(n: int, d: int, arr: List[int]) -> int:
	arr.sort(reverse=True)
	res = 0
	i = 0
	while i < n - 1:
		if arr[i] - arr[i + 1] < d:
			res += arr[i] + arr[i + 1]
			i += 1
		i += 1
	return res
for _ in range(int(input())):
	(n, d) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(soln(n, d, arr))


for _ in range(int(input())):
	(n, d) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	k = a.pop()
	ans = 0
	while a:
		k1 = a.pop()
		if k - k1 < d:
			ans += k + k1
			if a:
				k = a.pop()
		else:
			k = k1
	print(ans)


def solve():
	ans = 0
	i = input().split(' ')
	n = int(i[0])
	d = int(i[1])
	a = list(map(int, input().split(' ')))
	a.sort()
	while n > 1:
		if a[-1] - a[-2] < d:
			ans += a[-1] + a[-2]
			del a[-1]
			del a[-1]
			n -= 2
		else:
			del a[-1]
			n -= 1
	print(ans)
	return

def main():
	t = int(input())
	while t > 0:
		solve()
		t -= 1
	return
main()


t = int(input())
while t > 0:
	(n, d) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort(reverse=True)
	s = 0
	i = 0
	while i < n:
		if abs(a[i] - a[i - 1]) < d:
			s += a[i] + a[i - 1]
			i += 1
		i += 1
	print(s)
	t -= 1


for _ in range(int(input())):
	(N, D) = map(int, input().split())
	array = sorted(map(int, input().split()), reverse=True)
	i = 0
	ans = 0
	while i + 1 < N:
		if array[i] - array[i + 1] < D:
			ans += array[i] + array[i + 1]
			i += 1
		i += 1
	print(ans)


t = int(input())
for i in range(t):
	l = input().split()
	n = int(l[0])
	d = int(l[1])
	l = list(map(int, input().split()))
	l = sorted(l)
	sum = 0
	j = len(l) - 1
	while j >= 1:
		if l[j] - l[j - 1] < d:
			sum += l[j] + l[j - 1]
			j -= 2
		else:
			j -= 1
	print(sum)


for _ in range(int(input())):
	(N, d) = map(int, input().split())
	arr = list(map(int, input().split()))
	brr = []
	i = 0
	sum = 0
	arr.sort(reverse=True)
	while i < len(arr) - 1:
		if abs(arr[i] - arr[i + 1]) < d:
			sum += arr[i] + arr[i + 1]
			i += 2
		else:
			i += 1
	print(sum)


for _ in range(int(input())):
	(N, D) = list(map(int, input().split()))
	L = list(map(int, input().split()))
	L.sort(reverse=True)
	sum_ = 0
	flag = 0
	for i in range(N - 1):
		if flag == 1:
			flag = 0
		elif L[i] - L[i + 1] < D:
			sum_ += L[i] + L[i + 1]
			flag = 1
	print(sum_)


tc = int(input())
for i in range(tc):
	(x, y) = map(int, input().split())
	l = list(map(int, input().split()))[:x]
	l.sort()
	s = 0
	j = x - 1
	while j >= 0:
		if abs(l[j] - l[j - 1]) < y:
			s += l[j] + l[j - 1]
			j = j - 2
		else:
			j = j - 1
	print(s)


for _ in range(int(input())):
	(n, d) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort(reverse=True)
	i = 0
	s = 0
	while i < n - 1:
		if a[i] - a[i + 1] < d:
			s += a[i] + a[i + 1]
			i = i + 2
		else:
			i += 1
	print(s)


t = int(input())
for _ in range(t):
	(n, d) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.sort(reverse=True)
	taken = 0
	Sm = 0
	for i in range(n - 1):
		if taken == 0:
			if arr[i] - arr[i + 1] < d:
				Sm = Sm + arr[i] + arr[i + 1]
				taken = 1
		elif taken == 1:
			taken = 0
	print(Sm)


t = int(input())
for t0 in range(t):
	(n, d) = [int(i) for i in input().strip().split(' ')]
	li = [int(i) for i in input().strip().split(' ')]
	li.sort(reverse=True)
	sm = 0
	i = 0
	while i < n - 1:
		if li[i] - li[i + 1] < d:
			sm += li[i] + li[i + 1]
			i += 1
		i += 1
	print(sm)


t = int(input())
for i in range(t):
	(n, d) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	(s1, s2) = (0, 0)
	j = len(l) - 1
	while j > 0:
		if l[j] - l[j - 1] < d:
			s1 += l[j] + l[j - 1]
			j -= 2
		else:
			j = j - 1
	j = 0
	while j < len(l) - 1:
		if l[j + 1] - l[j] < d:
			s2 += l[j + 1] + l[j]
			j += 2
		else:
			j += 1
	print(max(s1, s2))


for T in range(int(input())):
	(N, D) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr = sorted(arr)
	mark = [0] * N
	ans = 0
	for i in range(N - 1, 0, -1):
		if mark[i] == 0:
			if arr[i] - arr[i - 1] < D:
				ans += arr[i] + arr[i - 1]
				(mark[i], mark[i - 1]) = (1, 1)
	print(ans)


def sort(low, high, L):
	if low < high:
		mid = low + int((high - low) / 2)
		a = low
		b = mid + 1
		sort(low, mid, L)
		sort(mid + 1, high, L)
		temp = []
		while a <= mid or b <= high:
			if a <= mid and b <= high:
				if L[a] > L[b]:
					temp.append(L[a])
					a += 1
				else:
					temp.append(L[b])
					b += 1
			elif a <= mid and b > high:
				temp.append(L[a])
				a += 1
			elif a > mid and b <= high:
				temp.append(L[b])
				b += 1
		for i in range(len(temp)):
			L[low + i] = temp[i]

def solution(L, D):
	k = {}
	sum = 0
	sort(0, len(L) - 1, L)
	i = 0
	while i < len(L) - 1:
		if L[i] - L[i + 1] < D:
			sum += L[i] + L[i + 1]
			i += 1
		i += 1
	return sum
T = int(input())
for t in range(T):
	(N, D) = map(int, input().split())
	L = list(map(int, input().split()))
	print(solution(L, D))


T = int(input())
for i in range(T):
	(n, d) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	l = l[::-1]
	(s, i) = (0, 1)
	while i < n:
		if l[i - 1] - l[i] < d:
			s += l[i] + l[i - 1]
			i += 1
		i += 1
	print(s)


for _ in range(int(input())):
	(n, d) = map(int, input().split())
	arr = [int(x) for x in input().split()]
	arr.sort()
	i = len(arr) - 1
	res = 0
	while i >= 1:
		if arr[i] - arr[i - 1] < d:
			res += arr[i] + arr[i - 1]
			i -= 2
		else:
			i -= 1
	print(res)


def integer_list():
	return list(map(int, input().split()))

def string_list():
	return list(map(str, input().split()))

def hetro_list():
	return list(input().split())
t = int(input())
for _ in range(t):
	(n, d) = integer_list()
	lst = integer_list()
	lst.sort()
	s = 0
	while lst:
		ele = lst.pop()
		if lst:
			if ele - lst[-1] < d:
				s += ele + lst.pop()
	print(s)


from random import randint

def test():
	t = randint(1, 10)
	print(t)
	for _ in range(t):
		n = randint(1, 20)
		d = randint(1, 20)
		print('%d %d' % (n, d))
		for _ in range(n):
			print(randint(1, 100), end=' ')
		print()
	exit(0)
	pass

def solve():
	for _ in range(int(input())):
		(n, d) = map(int, input().split())
		a = list(map(int, input().split()))
		a.sort()
		ans = 0
		i = len(a) - 2
		j = len(a) - 1
		while i > -1:
			if abs(a[i] - a[j]) < d:
				ans += a[i] + a[j]
				i -= 2
				j -= 2
			else:
				i -= 1
				j -= 1
		print(ans)
	pass
solve()


n = int(input())
list2 = []
for i in range(n):
	(n, d) = map(int, input().split())
	b = list(map(int, input().split()))
	ans = 0
	b.sort()
	j = len(b) - 1
	while j > 0:
		if b[j] - b[j - 1] < d:
			ans += b[j] + b[j - 1]
			j -= 2
		else:
			j -= 1
	print(ans)


test = int(input())
while test != 0:
	test = test - 1
	(n, d) = map(int, input().split())
	x = list(input().split())
	i = 0
	while i < n:
		x[i] = int(x[i])
		i = i + 1
	x.sort()
	ans = 0
	while n > 0:
		a = x[n - 1]
		b = x[n - 2]
		b = abs(a - b)
		if b < d:
			ans = ans + x[n - 1] + x[n - 2]
			n = n - 2
		else:
			n = n - 1
	print(ans)


t = int(input())
while t:
	ans = 0
	(n, d) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr = sorted(arr)
	l = len(arr)
	i = l - 1
	while i > 0:
		if arr[i] - arr[i - 1] < d:
			ans += arr[i] + arr[i - 1]
			i = i - 2
		else:
			i = i - 1
	print(ans)
	t -= 1


t = int(input())
for _ in range(t):
	(n, d) = map(int, input().split())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	l.sort(reverse=True)
	ans = 0
	i = 0
	j = 1
	while j < n:
		if l[i] - l[j] < d:
			ans += l[i] + l[j]
			j += 2
			i += 2
		else:
			i += 1
			j += 1
	print(ans)


tests = int(input())
for t in range(tests):
	(n, d) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	a.sort(reverse=True)
	i = 0
	while i < len(a) - 1:
		if a[i] - a[i + 1] < d:
			i += 2
		else:
			a.pop(i)
	if i == len(a) - 1:
		a.pop()
	print(sum(a))


t = int(input())
for j in range(0, t):
	(n, d) = input().strip().split()
	n = int(n)
	d = int(d)
	a = list(map(int, input().strip().split()))
	a.sort(reverse=True)
	add = 0
	i = 0
	while i < n - 1:
		if a[i] - a[i + 1] < d:
			add = add + a[i] + a[i + 1]
			i = i + 2
		else:
			i = i + 1
	print(add)


a = int(input())
for i in range(0, a):
	(b, c) = map(int, input().split())
	d = list(map(int, input().split()))
	d.sort(reverse=True)
	u = 0
	z = []
	for t in range(0, b):
		z.append(0)
	for t in range(0, b):
		if z[t] == 0:
			z[t] = 1
			for y in range(t + 1, b):
				r = abs(d[t] - d[y])
				if r < c and z[y] == 0:
					u = u + (d[t] + d[y])
					z[y] = 1
					break
				elif r >= c:
					break
	print(u)


T = int(input())
for j in range(0, T):
	(N, D) = input().split()
	N = int(N)
	D = int(D)
	array = input().split()
	array = [int(x) for x in array]
	array.sort()
	sum = 0
	for k in range(len(array) - 1, 0, -1):
		if array[k] - array[k - 1] < D:
			sum += array[k] + array[k - 1]
			array[k - 1] += 2 * D
	print(sum)


for j in range(int(input())):
	(n, d) = map(int, input().split())
	x = list(map(int, input().split()))
	x.sort()
	x.reverse()
	i = 1
	c = 0
	while i < n:
		if x[i - 1] - x[i] < d:
			c += x[i - 1] + x[i]
			i += 2
		else:
			i += 1
	print(c)


for i in range(int(input())):
	(x, y) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	k = 0
	j = len(a) - 1
	while j > 0:
		if a[j] - a[j - 1] < y:
			k = k + a[j] + a[j - 1]
			j = j - 2
		else:
			j = j - 1
	print(k)