A total of $n$ depots are located on a number line. Depot $i$ lies at the point $x_i$ for $1 \le i \le n$.

You are a salesman with $n$ bags of goods, attempting to deliver one bag to each of the $n$ depots. You and the $n$ bags are initially at the origin $0$. You can carry up to $k$ bags at a time. You must collect the required number of goods from the origin, deliver them to the respective depots, and then return to the origin to collect your next batch of goods.

Calculate the minimum distance you need to cover to deliver all the bags of goods to the depots. You do not have to return to the origin after you have delivered all the bags.


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10\,500$). Description of the test cases follows.

The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$).

The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($-10^9 \le x_i \le 10^9$). It is possible that some depots share the same position.

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


-----Output-----

For each test case, output a single integer denoting the minimum distance you need to cover to deliver all the bags of goods to the depots.


-----Examples-----

Input
4
5 1
1 2 3 4 5
9 3
-5 -10 -15 6 5 8 3 7 4
5 3
2 2 3 3 3
4 2
1000000000 1000000000 1000000000 1000000000
Output
25
41
7
3000000000


-----Note-----

In the first test case, you can carry only one bag at a time. Thus, the following is a solution sequence that gives a minimum travel distance: $0 \to 2 \to 0 \to 4 \to 0 \to 3 \to 0 \to 1 \to 0 \to 5$, where each $0$ means you go the origin and grab one bag, and each positive integer means you deliver the bag to a depot at this coordinate, giving a total distance of $25$ units. It must be noted that there are other sequences that give the same distance.

In the second test case, you can follow the following sequence, among multiple such sequences, to travel minimum distance: $0 \to 6 \to 8 \to 7 \to 0 \to 5 \to 4 \to 3 \to 0 \to (-5) \to (-10) \to (-15)$, with distance $41$. It can be shown that $41$ is the optimal distance for this test case.

Source: codeforces | Difficulty: EASY
Tags: Greedy algorithms


Solutions:
import sys
input = sys.stdin.readline

def positive_dist(x, n, k, last):
	cur = n - 1
	total = 0
	while cur >= 0 and x[cur] > 0:
		total += 2 * x[cur]
		cur -= k
	if last and x[-1] > 0:
		total -= x[-1]
	return total

def negative_dist(x, n, k, last):
	cur = 0
	total = 0
	while cur < n and x[cur] < 0:
		total -= 2 * x[cur]
		cur += k
	if last and x[0] < 0:
		total += x[0]
	return total
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	x = [int(i) for i in input().split()]
	x.sort()
	if x[0] >= 0:
		print(positive_dist(x, n, k, True))
	elif x[-1] <= 0:
		print(negative_dist(x, n, k, True))
	elif abs(x[0]) > abs(x[-1]):
		print(positive_dist(x, n, k, False) + negative_dist(x, n, k, True))
	else:
		print(positive_dist(x, n, k, True) + negative_dist(x, n, k, False))


from bisect import bisect_left, bisect_right

def solve(n, k, depots):
	distance = 0
	depots.sort()
	loc1 = bisect_left(depots, 0)
	loc2 = bisect_right(depots, 0)
	j = n - 1
	while j >= loc2:
		distance += 2 * depots[j]
		j -= k
	j = 0
	while j < loc1:
		distance -= 2 * depots[j]
		j += k
	if loc2 < n and loc1 > 0:
		distance -= max(-depots[0], depots[-1])
	elif loc2 < n:
		distance -= depots[-1]
	elif loc1 > 0:
		distance += depots[0]
	return distance
ans = []
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	ans.append(solve(n, k, list(map(int, input().split()))))
print(*ans, sep='\n')


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	(b, c) = ([], [])
	for x in a:
		if x > 0:
			b.append(x)
		elif x < 0:
			c.append(-x)
	(nb, nc) = (len(b), len(c))
	res = 0
	b.sort()
	c.sort()
	if nb == nc == 0:
		print(0)
		continue
	if nc == 0:
		(nb, nc) = (nc, nb)
		(b, c) = (c, b)
	if (nb > 0 and nc > 0) and max(b) > max(c):
		(nb, nc) = (nc, nb)
		(b, c) = (c, b)
	if nb:
		for x in range(nb - 1, -1, -k):
			res += b[x] * 2
	for x in range(nc - 1, -1, -k):
		res += c[x] * 2
	print(res - c[-1])


from sys import stdin, stdout
import math
for _ in range(int(stdin.readline())):
	(n, k) = map(int, stdin.readline().split())
	a = list(map(int, stdin.readline().split()))
	a1 = []
	a2 = []
	for i in a:
		if i < 0:
			a1.append(i * -1)
		elif i > 0:
			a2.append(i)
	a1.sort()
	a2.sort()
	ans = 0
	n1 = len(a1)
	n2 = len(a2)
	for i in range(n2 - 1 - k, -1, -k):
		ans += 2 * a2[i]
	for i in range(n1 - 1 - k, -1, -k):
		ans += 2 * a1[i]
	if n1 > 0 and n2 > 0:
		if a1[-1] > a2[-1]:
			ans += a1[-1]
			ans += 2 * a2[-1]
		else:
			ans += a2[-1]
			ans += 2 * a1[-1]
	elif n1 > 0:
		ans += a1[-1]
	elif n2 > 0:
		ans += a2[-1]
	print(ans)


import heapq
import sys
import math
from collections import deque
from bisect import bisect_left, bisect_right
I = sys.stdin.readline
II = lambda : int(I())
IN = lambda x: map(int, x.split())
FN = lambda x: map(float, x.split())
L = lambda x: list(IN(x))
M = 1000000007
P = print
T = True
F = False
for _ in range(II()):
	(n, k) = IN(I())
	l = L(I())
	l.sort()
	f = 0
	n2 = n - 1
	n1 = n
	for i in range(n):
		if l[i] >= 0:
			n2 = min(n2, i - 1)
		if l[i] > 0:
			n1 = i
			break
	i = n
	ans = 0
	while i > n1:
		ans += 2 * l[max(i - 1, n1)]
		i = i - k
	if n1 < n:
		ans -= l[-1]
	i = 0
	while i <= n2:
		ans -= 2 * l[i]
		i = i + k
	if n2 > -1:
		ans += l[0]
	if n1 < n and n2 > -1:
		ans += min(l[-1], abs(l[0]))
	print(ans)


from sys import stdin, stdout

def main():
	for _ in range(int(stdin.readline().strip())):
		(n, k) = map(int, stdin.readline().split())
		x = sorted(list(map(int, stdin.readline().split())))
		(neg, pos) = ([], [])
		for i in x:
			if i < 0:
				neg.append(i)
			elif i > 0:
				pos.append(i)
		neg.sort()
		pos.sort(reverse=True)
		r = []
		for i in range(0, len(neg), k):
			r.append(abs(neg[i]))
		for i in range(0, len(pos), k):
			r.append(pos[i])
		if r:
			print(sum(r) * 2 - max(r))
		else:
			print(0)
main()


import sys
import bisect
inp = sys.stdin.readline

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)
for _ in range(int(inp())):
	(n, k) = map(int, inp().split())
	a = [int(i) for i in inp().split()]
	a.append(0)
	a.sort()
	i = 0
	n += 1
	while a[i] < 0:
		i += 1
	ans = 0
	j = n - 1
	while j >= i:
		ans += 2 * a[j]
		j = j - k
	j = 0
	while j < i:
		ans += 2 * abs(a[j])
		j = j + k
	print(ans - max(abs(a[0]), a[-1]))


def main():
	for _ in range(int(input())):
		print(minimize_distance())

def minimize_distance():
	(n, k) = map(int, input().split())
	values = map(int, input().split())
	positive = []
	negative = []
	for val in values:
		if val > 0:
			positive.append(val)
		else:
			negative.append(-val)
	positive.sort()
	negative.sort()
	if not negative:
		shorter = negative
		longer = positive
	elif not positive:
		shorter = positive
		longer = negative
	elif positive[-1] > negative[-1]:
		shorter = negative
		longer = positive
	else:
		shorter = positive
		longer = negative
	total = longer[-1]
	si = len(shorter) - 1
	li = len(longer) - k - 1
	while si >= 0 or li >= 0:
		if si < 0:
			total += longer[li] * 2
			li -= k
		elif li < 0:
			total += shorter[si] * 2
			si -= k
		elif longer[li] > shorter[si]:
			total += longer[li] * 2
			li -= k
		else:
			total += shorter[si] * 2
			si -= k
	return total
main()


t = int(input())
while t:
	t -= 1
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	g = a[0]
	k = a[1]
	b.sort()
	ans = 0
	i = 0
	while i < g and b[i] < 0:
		ans = ans + abs(b[i]) * 2
		i += k
	i = g - 1
	while i >= 0 and b[i] > 0:
		ans = ans + b[i] * 2
		i -= k
	ans = ans - max(abs(b[0]), abs(b[g - 1]))
	print(ans)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	aa = list(map(int, input().split()))
	c = 0
	aa.sort()
	t = True
	i = n - 1
	while t == True and i >= 0:
		if aa[i] > 0:
			c += aa[i] * 2
			i -= k
		else:
			t = False
	t = True
	i = 0
	while t == True and i < n:
		if aa[i] < 0:
			c -= aa[i] * 2
			i += k
		else:
			t = False
	print(c - max(-aa[0], aa[n - 1]))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.append(0)
	arr.sort()
	oi = arr.index(0)
	ans = 0
	for i in range(0, oi, k):
		ans += 2 * abs(arr[i])
	for i in range(n, oi, -k):
		ans += 2 * arr[i]
	t = max(abs(arr[0]), abs(arr[-1]))
	ans -= t
	print(ans)


I = lambda : map(int, input().split())
for _ in [0] * int(input()):
	(n, k) = I()
	a = sorted(list(I()))
	i = s = 0
	while i < n and a[i] < 0:
		i = i + 1
	if i == 0:
		s = a[n - 1]
		for j in range(n - k - 1, -1, -k):
			s = s + 2 * a[j]
	elif i == n:
		s = a[0]
		for j in range(k, n, k):
			s = s + 2 * a[j]
		s = -s
	else:
		b = a[:i]
		blen = i
		a = a[i:]
		alen = n - i
		if a[-1] < abs(b[0]):
			s = b[0]
			for j in range(k, blen, k):
				s = s + 2 * b[j]
			s = -s
			for j in range(alen - 1, -1, -k):
				s = s + 2 * a[j]
		else:
			s = a[alen - 1]
			for j in range(alen - k - 1, -1, -k):
				s = s + 2 * a[j]
			for j in range(0, blen, k):
				s = s - 2 * b[j]
	print(s)


def minimize_distance(n, k, l):
	l.sort()
	d = -max(abs(l[0]), abs(l[n - 1]))
	i = n - 1
	while i >= 0 and l[i] > 0:
		d += 2 * l[i]
		i -= k
	i = 0
	while i < n and l[i] < 0:
		d += 2 * abs(l[i])
		i += k
	return d
t = int(input())
for i in range(t):
	[n, k] = list(map(int, input().split()))
	l = list(map(int, input().split()))
	print(minimize_distance(n, k, l))


import math as m
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	res = 0
	a.sort()
	pos = (x for x in range(len(a) - 1, -1, -k) if a[x] > 0)
	nev = (x for x in range(0, len(a), k) if a[x] < 0)
	for i in pos:
		res += a[i]
	for i in nev:
		res -= a[i]
	print(2 * res - max(abs(a[0]), a[n - 1]))


def int_input():
	a = int(input())
	return a

def int_list_input():
	a = list(map(int, input().split()))
	return a

def solve():
	(n, k) = int_list_input()
	arr = int_list_input()
	ans = 0
	arr.sort()
	pos = [0]
	neg = [0]
	i = n - 1
	while i >= 0:
		if arr[i] < 0:
			break
		pos.append(arr[i])
		i = i - k
	i = 0
	while i < n:
		if arr[i] > 0:
			break
		neg.append(abs(arr[i]))
		i = i + k
	ans = 2 * sum(pos) + 2 * sum(neg)
	ans -= max(max(pos), max(neg))
	print(ans)
for _ in range(int(input())):
	solve()


ne = int(input())
for a in range(ne):
	nk = [int(item) for item in input().split()]
	n = nk[0]
	k = nk[1]
	nnl = [int(item) for item in input().split()]
	nnl = sorted(nnl, key=lambda item: item)
	dist = 0
	neg = 0
	while neg < n and nnl[neg] < 0:
		dist -= nnl[neg]
		neg += k
	pos = n - 1
	while pos > -1 and nnl[pos] > 0:
		dist += nnl[pos]
		pos -= k
	bl = 0
	if nnl[0] < 0:
		bl -= nnl[0]
	if nnl[n - 1] > 0:
		bl = max(bl, nnl[n - 1])
	dist *= 2
	dist -= bl
	print(dist)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	m = list(map(int, input().split()))
	m.sort()
	ind = 0
	s = 0
	while ind >= 0 and ind < n and (m[ind] <= 0):
		s += abs(m[ind]) * 2
		ind += k
	ind = n - 1
	while ind >= 0 and ind < n and (m[ind] >= 0):
		s += abs(m[ind]) * 2
		ind -= k
	print(s - max(abs(m[0]), abs(m[-1])))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	li = list(map(int, input().split()))
	ans = 0
	pos = []
	neg = []
	for i in range(n):
		if li[i] > 0:
			pos.append(li[i])
		else:
			neg.append(-li[i])
	pos.sort(reverse=True)
	neg.sort(reverse=True)
	ans = 0
	for i in range(k, len(pos)):
		if i % k == 0:
			ans = ans + 2 * pos[i]
	for i in range(k, len(neg)):
		if i % k == 0:
			ans = ans + 2 * neg[i]
	if len(pos) == 0 or len(neg) == 0:
		if len(pos) != 0:
			ans = ans + pos[0]
		elif len(neg) != 0:
			ans = ans + neg[0]
	else:
		ans = ans + min(pos[0], neg[0]) + pos[0] + neg[0]
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	d = []
	i = 0
	while i + k - 1 < n:
		if a[i + k - 1] > 0:
			break
		d.append(abs(a[i]))
		i += k
	j = n - 1
	while j - k + 1 >= 0:
		if a[j - k + 1] < 0:
			break
		d.append(a[j])
		j -= k
	if i < n and a[i] < 0:
		d.append(abs(a[i]))
	if j >= 0 and a[j] > 0:
		d.append(a[j])
	print(2 * sum(d) - max(d))


import math
import sys

def solve(k, negative_part, positive_part):
	(po, ne) = (len(positive_part), len(negative_part))
	(answer, a, b) = (0, 0, 0)
	if po != 0:
		if po < k:
			a = positive_part[po - 1]
			answer += a
		else:
			a = positive_part[po - 1]
			for i in range(po):
				if i == po - 1:
					answer += positive_part[i]
				elif (po - i - 1) % k == 0:
					answer += 2 * positive_part[i]
	if ne != 0:
		if ne < k:
			b = negative_part[ne - 1]
			answer += b
		else:
			b = negative_part[ne - 1]
			for i in range(ne):
				if i == ne - 1:
					answer += negative_part[i]
				elif (ne - i - 1) % k == 0:
					answer += 2 * negative_part[i]
	answer += min(a, b)
	return answer

def main():
	test_case = int(sys.stdin.readline())
	for tc in range(test_case):
		(n, k) = map(int, sys.stdin.readline().split())
		arr = [int(a) for a in sys.stdin.readline().split()]
		positive_part = []
		negative_part = []
		for i in range(n):
			if arr[i] < 0:
				negative_part.append(-1 * arr[i])
			else:
				positive_part.append(arr[i])
		negative_part.sort()
		positive_part.sort()
		print(solve(k, negative_part, positive_part))
main()


T = int(input())
for tt in range(T):
	(n, k) = map(int, input().split())
	x = list(map(int, input().split()))
	x.sort()
	res = 0
	res -= max(abs(x[0]), abs(x[-1]))
	if x[0] < 0:
		i = 0
		while i < len(x) and x[i] < 0:
			res += 2 * abs(x[i])
			i += k
	if x[-1] > 0:
		i = len(x) - 1
		while i >= 0 and x[i] > 0:
			res += 2 * abs(x[i])
			i -= k
	print(res)


def solve(n, k, x):
	(pos, neg) = ([], [])
	for num in x:
		if num < 0:
			neg.append(-num)
		elif num > 0:
			pos.append(num)
	if pos:
		pos.sort()
	if neg:
		neg.sort()
	(n1, n2) = (len(pos), len(neg))
	idx1 = n1 - 1
	idx2 = n2 - 1
	v1 = -float('inf') if not pos else pos[-1]
	v2 = -float('inf') if not neg else neg[-1]
	v = max(v1, v2)
	ans = -v
	if v == -float('inf'):
		print(0)
		return
	while idx1 >= 0 and idx2 >= 0:
		(p1, p2) = (pos[idx1], neg[idx2])
		if p1 <= p2:
			ans += p1 * 2
			idx1 -= k
		else:
			ans += p2 * 2
			idx2 -= k
	while idx1 >= 0:
		p1 = pos[idx1]
		ans += p1 * 2
		idx1 -= k
	while idx2 >= 0:
		p2 = neg[idx2]
		ans += p2 * 2
		idx2 -= k
	print(ans)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	x = list(map(int, input().split()))
	solve(n, k, x)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	x = [int(i) for i in input().split()]
	x.sort()
	d = []
	i = 0
	while i < n and x[i] < 0:
		d.append(-x[i])
		i += k
	i = n - 1
	while i >= 0 and x[i] >= 0:
		d.append(x[i])
		i -= k
	d.sort(reverse=True)
	ans = 0
	ans += d[0]
	for i in range(1, len(d)):
		ans += d[i] * 2
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	x = sorted(map(int, input().split()))
	x_filter = []
	for i in range(0, n, k):
		if x[i] > 0:
			break
		x_filter.append(abs(x[i]))
	for i in range(n - 1, -1, -k):
		if x[i] < 0:
			break
		x_filter.append(abs(x[i]))
	print(sum(x_filter) * 2 - max(x_filter))


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	a = sorted(list(map(int, input().split())))
	ans = -max(abs(a[0]), abs(a[-1]))
	i = n - 1
	while i >= 0 and a[i] >= 0:
		ans += 2 * a[i]
		i -= k
	i = 0
	while i < n and a[i] < 0:
		ans += 2 * abs(a[i])
		i += k
	print(ans)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	a = []
	b = []
	for j in map(int, input().split()):
		if j > 0:
			a.append(j)
		elif j < 0:
			b.append(-j)
	a.sort()
	b.sort()
	l = 0
	mx = 0
	if len(a + b) > 0:
		mx = max(a + b)
	if len(a) > 0:
		l += a[(len(a) % k - 1) % k] * 2
		a = a[(len(a) % k - 1) % k + 1:]
		if len(a) > 0:
			for j in range(len(a) // k):
				l += 2 * a[j * k + k - 1]
	if len(b) > 0:
		l += b[(len(b) % k - 1) % k] * 2
		b = b[(len(b) % k - 1) % k + 1:]
		if len(b) > 0:
			for j in range(len(b) // k):
				l += 2 * b[j * k + k - 1]
	print(l - mx)


t = int(input())
for r in range(t):
	(n, k) = map(int, input().split())
	r = 0
	A = list(map(int, input().split()))
	A = sorted(A)
	if abs(A[0]) > abs(A[n - 1]):
		r -= abs(A[0])
	else:
		r -= abs(A[n - 1])
	af = 0
	ak = 0
	ind = -1
	for i in range(n):
		if A[i] < 0:
			ind = i
	b = 0
	while b <= ind:
		r += 2 * abs(A[b])
		b += k
	b = n - 1
	while b > ind:
		r += 2 * A[b]
		b -= k
	print(r)


t = int(input())
for i in range(t):
	(n, k) = [int(c) for c in input().split(' ')]
	x = [int(c) for c in input().split(' ')]
	pos = [y for y in x if y > 0]
	neg = [abs(y) for y in x if y < 0]
	pos.sort()
	neg.sort()
	dist = 0
	for l in [pos, neg]:
		strt = len(l) % k
		if strt == 0:
			strt = k
		strt -= 1
		kk = strt
		while kk < len(l) - 1:
			dist += 2 * l[kk]
			kk += k
	if len(pos) == 0:
		mpos = 0
	else:
		mpos = pos[-1]
	if len(neg) == 0:
		mneg = 0
	else:
		mneg = neg[-1]
	dist += 2 * min(mpos, mneg)
	dist += max(mpos, mneg)
	print(dist)


t = int(input())
for u in range(t):
	(n, k) = map(int, input().split())
	x = list(map(int, input().split()))
	x.sort()
	ans = 0
	if abs(x[0]) > abs(x[n - 1]):
		ans -= abs(x[0])
	else:
		ans -= abs(x[n - 1])
	pos = -1
	for i in range(n):
		if x[i] < 0:
			pos += 1
	j = 0
	while j <= pos:
		ans += abs(x[j]) * 2
		j += k
	j = n - 1
	while j > pos:
		ans += x[j] * 2
		j -= k
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	st_p = []
	st_m = []
	for i in input().split():
		i = int(i)
		if i > 0:
			st_p.append(i)
		else:
			st_m.append(i)
	st_p.sort()
	st_m.sort()
	awn = -2 * sum(st_m[::k])
	awn += 2 * sum(st_p[::-k])
	if st_p and st_m:
		awn -= max(-st_m[::k][0], st_p[::-k][0])
	elif st_p:
		awn -= st_p[::-k][0]
	elif st_m:
		awn += st_m[::k][0]
	else:
		awn = 0
	print(awn)


def minimiseDistance(n, k, array):
	left = []
	right = []
	for x in range(n):
		if array[x] < 0:
			left.append(abs(array[x]))
		elif array[x] > 0:
			right.append(array[x])
	if not left and (not right):
		return 0
	right.sort()
	qR = len(right)
	dist = 0
	rem = qR % k
	if rem == 0:
		i = rem
	else:
		dist += right[rem - 1] * 2
		i = rem
	while i < qR:
		i += k
		dist += right[i - 1] * 2
	left.sort()
	qL = len(left)
	rem = qL % k
	if rem == 0:
		i = rem
	else:
		dist += left[rem - 1] * 2
		i = rem
	while i < qL:
		i += k
		dist += left[i - 1] * 2
	if not left:
		dist -= right[-1]
	elif not right:
		dist -= left[-1]
	else:
		dist -= max(left[-1], right[-1])
	return dist
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	print(minimiseDistance(n, k, l))


import sys
from os import path
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = map(int, input().split())
	(neg, pos) = ([], [])
	for i in a:
		if i < 0:
			neg.append(-i)
		else:
			pos.append(i)
	neg.sort()
	pos.sort()
	ans = int(0)
	for i in range(len(pos) - 1, -1, -k):
		ans += pos[i] * 2
	for i in range(len(neg) - 1, -1, -k):
		ans += neg[i] * 2
	pos.insert(0, 0)
	neg.insert(0, 0)
	ans -= max(pos[-1], neg[-1])
	print(ans)


t = int(input())
while t:
	(n, k) = map(int, input().split())
	x = list(map(int, input().split()))
	a = []
	b = []
	for i in x:
		if i > 0:
			a.append(i)
		elif i < 0:
			b.append(-i)
	a.sort(reverse=1)
	b.sort(reverse=1)
	dsta = 0
	for i in range(0, len(a), k):
		dsta += 2 * a[i]
	dstb = 0
	for i in range(0, len(b), k):
		dstb += 2 * b[i]
	if len(a):
		mxa = a[0]
	else:
		mxa = 0
	if len(b):
		mxb = b[0]
	else:
		mxb = 0
	print(dsta + dstb - max(mxa, mxb))
	t -= 1


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a = [x for x in a if x != 0]
	a.sort()
	ng = []
	ps = []
	for x in a:
		if x < 0:
			ng += (abs(x),)
		else:
			ps += (x,)
	ng.sort()
	ans = 0
	pls = len(ps)
	if pls > 0:
		ans += ps[-1]
	i = pls - k - 1
	while i >= 0:
		ans += 2 * ps[i]
		i -= k
	nls = len(ng)
	if nls > 0:
		ans += ng[-1]
	i = nls - k - 1
	while i >= 0:
		ans += 2 * ng[i]
		i -= k
	if pls > 0 and nls > 0:
		ans += min(ps[-1], ng[-1])
	print(ans)


def func(N, K, arr):
	(ps, ne) = (sorted([i for i in arr if i > 0]), sorted([i for i in arr if i < 0]))
	(mn1, mn2) = (0, 0)
	(it1, it2) = (len(ps) - K - 1, 0)
	while it1 >= 0:
		mn1 += ps[it1] * 2
		it1 -= K
	while it2 < len(ne):
		mn1 += abs(ne[it2]) * 2
		it2 += K
	(pt1, pt2) = (len(ps) - 1, K)
	while pt1 >= 0:
		mn2 += ps[pt1] * 2
		pt1 -= K
	while pt2 < len(ne):
		mn2 += abs(ne[pt2]) * 2
		pt2 += K
	if ps:
		mn1 += ps[-1]
	if ne:
		mn2 += abs(ne[0])
	return min(mn1, mn2)
for t in range(int(input())):
	(N, K) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(func(N, K, arr))


def walk(n, k, L):
	if n == 0:
		return 0
	if n <= k:
		return L[-1]
	dist = 0
	q = int(n / k)
	r = n - q * k
	if r != 0:
		for i in range(q):
			dist += 2 * L[i * k + r - 1]
		dist += L[-1]
		return dist
	if r == 0:
		for i in range(1, q):
			dist += 2 * L[i * k - 1]
		dist += L[-1]
		return dist
cases = int(input())
for wow in range(cases):
	line1 = input().split()
	n = int(line1[0])
	k = int(line1[1])
	long = input().split()
	L1 = []
	n1 = 0
	L2 = []
	n2 = 0
	for elem in long:
		elem = int(elem)
		if elem > 0:
			L1.append(elem)
			n1 += 1
		elif elem < 0:
			L2.append(-elem)
			n2 += 1
	L1.sort()
	L2.sort()
	if n1 == 0:
		print(walk(n2, k, L2))
	elif n2 == 0:
		print(walk(n1, k, L1))
	elif L1[-1] > L2[-1]:
		print(walk(n1, k, L1) + walk(n2, k, L2) + L2[-1])
	else:
		print(walk(n1, k, L1) + walk(n2, k, L2) + L1[-1])


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	bags = sorted(list(map(int, input().split())))
	negatives = [abs(i) for i in bags if i < 0]
	positives = [i for i in bags if i > 0][::-1]
	distance_passed = 0
	if negatives and positives and (negatives[0] >= positives[0]):
		index = k
		while index < len(negatives):
			distance_passed += 2 * negatives[index]
			index += k
		index = 0
		while index < len(positives):
			distance_passed += 2 * positives[index]
			index += k
		distance_passed += negatives[0]
	elif negatives and positives and (negatives[0] < positives[0]):
		index = 0
		while index < len(negatives):
			distance_passed += 2 * negatives[index]
			index += k
		index = k
		while index < len(positives):
			distance_passed += 2 * positives[index]
			index += k
		distance_passed += positives[0]
	elif negatives:
		index = k
		while index < len(negatives):
			distance_passed += 2 * negatives[index]
			index += k
		index = 0
		while index < len(positives):
			distance_passed += 2 * positives[index]
			index += k
		distance_passed += negatives[0]
	elif positives:
		index = 0
		while index < len(negatives):
			distance_passed += 2 * negatives[index]
			index += k
		index = k
		while index < len(positives):
			distance_passed += 2 * positives[index]
			index += k
		distance_passed += positives[0]
	print(distance_passed)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	tk = k
	pa = []
	na = []
	for i in l:
		if i >= 0:
			pa.append(i)
		else:
			na.append(i)
	pa.sort()
	na.sort()
	na.reverse()
	p = len(pa)
	q = len(na)
	arr = []
	if p > 0:
		temp = p - 1
		arr.append(pa[temp])
		temp -= k
		while temp >= 0:
			arr.append(pa[temp])
			temp -= k
	if q > 0:
		temp = q - 1
		arr.append(-na[temp])
		temp -= k
		while temp >= 0:
			arr.append(-na[temp])
			temp -= k
	ans = 0
	arr.sort()
	for i in range(len(arr) - 1):
		ans += arr[i] * 2
	ans += arr[-1]
	print(ans)


R = lambda : map(int, input().split())
(t,) = R()
while t:
	t -= 1
	(n, k) = R()
	a = (b, c) = [[], []]
	for x in sorted(R()):
		a[x > 0] += (abs(x),)
	print(2 * sum(b[::k] + c[::-k]) - max(b[:1] + c[-1:]))


for t in range(int(input())):
	(n, k) = tuple(map(int, input().split()))
	x = sorted(list(map(int, input().split())))
	negative = list(filter(lambda k: k < 0, x))
	positive = list(filter(lambda k: k > 0, x))
	cost = sum([2 * -negative[i] for i in range(0, len(negative), k)]) + sum([2 * positive[i] for i in range(len(positive) - 1, -1, -k)])
	if len(negative) and len(positive):
		cost -= max(-negative[0], positive[-1])
	elif len(negative):
		cost -= -negative[0]
	elif len(positive):
		cost -= positive[-1]
	print(cost)


for i in range(int(input())):
	nk = list(map(int, input().split()))
	k = nk[1]
	xi = list(map(int, input().split()))
	xi_p = [0]
	xi_n = [0]
	for i in xi:
		if i >= 0:
			xi_p.append(i)
		else:
			xi_n.append(i)
	xi_p.sort()
	xi_n.sort()
	min_distance = 0
	if xi_p[-1] >= -1 * xi_n[0]:
		min_distance = -1 * xi_p[-1]
	else:
		min_distance = xi_n[0]
	while len(xi_p) >= 1:
		min_distance += 2 * xi_p[-1]
		if len(xi_p) >= k:
			xi_p = xi_p[0:len(xi_p) - k]
		else:
			xi_p = []
	while len(xi_n) >= 1:
		min_distance += -2 * xi_n[0]
		if len(xi_n) >= k:
			xi_n = xi_n[k:]
		else:
			xi_n = []
	print(min_distance)


for _ in range(int(input())):
	(n, k) = map(int, input().strip().split())
	l = list(map(int, input().strip().split()))
	l.sort()
	a = n
	for i in range(n):
		if l[i] >= 0:
			a = i
			break
	ln = l[:a]
	lp = l[a:]
	ln = [abs(i) for i in ln]
	lp.reverse()
	co = 0
	for i in range(0, len(ln), k):
		co += ln[i]
	for i in range(0, len(lp), k):
		co += lp[i]
	co *= 2
	if len(ln) == 0:
		co -= lp[0]
	elif len(lp) == 0:
		co -= ln[0]
	else:
		co -= max(ln[0], lp[0])
	print(co)


t = int(input())
for e in range(t):
	l = list(map(int, input().split()))
	a = list(map(int, input().split()))
	k = l[1]
	pl = []
	nl = []

	def cost(l, k):
		c = 0
		i = len(l) - 1
		while i >= 0:
			if c == 0:
				c = c + l[i]
				i = i - k
			else:
				c = c + 2 * l[i]
				i = i - k
		return c
	for i in a:
		if i < 0:
			nl.append(-i)
		else:
			pl.append(i)
	pl.sort()
	nl.sort()
	qp = cost(pl, k)
	qn = cost(nl, k)
	if nl == []:
		nl.append(0)
	if pl == []:
		pl.append(0)
	qm = qp + qn + min(nl[-1], pl[-1])
	print(qm)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	pos = []
	neg = []
	dist = 0
	for item in arr:
		if item >= 0:
			pos.append(item)
		elif item < 0:
			neg.append(item * -1)
	pos.sort()
	neg.sort()
	if len(neg) == 0:
		for i in range(len(pos) - 1, -1, -k):
			dist += 2 * pos[i]
		dist -= pos[-1]
	elif len(pos) == 0:
		for i in range(len(neg) - 1, -1, -k):
			dist += 2 * neg[i]
		dist -= neg[-1]
	elif pos[-1] > neg[-1]:
		for i in range(len(neg) - 1, -1, -k):
			dist += 2 * neg[i]
		for i in range(len(pos) - 1, -1, -k):
			dist += 2 * pos[i]
		dist -= pos[-1]
	elif neg[-1] >= pos[-1]:
		for i in range(len(pos) - 1, -1, -k):
			dist += 2 * pos[i]
		for i in range(len(neg) - 1, -1, -k):
			dist += 2 * neg[i]
		dist -= neg[-1]
	print(dist)


def solve(depot, k):
	ind = -1
	l = len(depot)
	sm = 0
	if l % k:
		sm += depot[ind + l % k] * 2
		ind += l % k
	for i in range(l // k):
		sm += depot[ind + k] * 2
		ind += k
	return sm

def fndMax(p, n):
	if bool(p) and bool(n):
		return max(p[len(p) - 1], n[len(n) - 1])
	if not bool(p):
		return n[len(n) - 1]
	else:
		return p[len(p) - 1]
t = int(input())
while t:
	(n, k) = map(int, input().split())
	x = list(map(int, input().split()))
	neg = []
	pos = []
	for i in x:
		if i < 0:
			neg.append(-i)
		else:
			pos.append(i)
	neg.sort()
	pos.sort()
	print(solve(pos, k) + solve(neg, k) - fndMax(pos, neg))
	t -= 1


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	b = []
	c = []
	d = 0
	for i3 in range(n):
		if a[i3] < 0:
			b.append(-a[i3])
		else:
			c.append(a[i3])
	c.reverse()
	p1 = len(b)
	p2 = len(c)
	if p1 != 0:
		t1 = b[0]
	else:
		t1 = -1
	if p2 != 0:
		t2 = c[0]
	else:
		t2 = -1
	for i11 in range(0, p1, k):
		d += 2 * b[i11]
	for i12 in range(0, p2, k):
		d += 2 * c[i12]
	d = d - max(t1, t2)
	print(d)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = [0]
	b = [0]
	for i in input().split():
		j = int(i)
		if j < 0:
			a.append(abs(j))
		if j > 0:
			b.append(j)
	ans = -max(max(a), max(b))
	a.sort()
	b.sort()
	for i in a[::-k]:
		ans += 2 * i
	for i in b[::-k]:
		ans += 2 * i
	print(ans)


t = int(input())
for q in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.append(0)
	a.sort()
	a_1 = a[:a.index(0)] + [0]
	a_2 = a[a.index(0) + 1:] + [0]
	a_o = []
	for i in range(len(a_1)):
		if i % k == 0:
			a_o.append(a_1[i])
	a_p = []
	a_2.sort(reverse=True)
	for i in range(len(a_2)):
		if i % k == 0:
			a_p.append(a_2[i])
	mx = max(max(a_p), abs(min(a_o)))
	R = 2 * abs(sum(a_o)) + 2 * sum(a_p) - mx
	print(R)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(pos, neg) = ([], [])
	for i in list(map(int, input().split())):
		if i > 0:
			pos.append(i)
		else:
			neg.append(i)
	pos.sort(reverse=True)
	neg.sort()
	(posSize, negSize) = (len(pos), len(neg))
	maxDist = 0
	for i in range(posSize):
		if (i + 1) % k == 0:
			maxDist += 2 * pos[i + 1 - k]
	for i in range(negSize):
		if (i + 1) % k == 0:
			maxDist += 2 * abs(neg[i + 1 - k])
	if negSize % k:
		maxDist += 2 * abs(neg[-(negSize % k)])
	if posSize % k:
		maxDist += 2 * pos[-(posSize % k)]
	if negSize > 0 and posSize > 0:
		maxDist -= max(abs(neg[0]), pos[0])
	elif posSize > 0:
		maxDist -= pos[0]
	else:
		maxDist -= abs(neg[0])
	print(maxDist)


import collections

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

def readInt():
	return int(input())

def readString():
	return input()

def tcase():
	t = readInt()
	for _ in range(t):
		solve()

def solve():
	(n, k) = readLine()
	arr = readLine()
	pos = []
	neg = []
	for a in arr:
		if a >= 0:
			pos.append(a)
		else:
			neg.append(-a)
	pos.sort()
	neg.sort()
	maximum = 0
	if pos:
		maximum = max(maximum, max(pos))
	if neg:
		maximum = max(maximum, max(neg))

	def count(arr):
		na = len(arr)
		ans = 0
		for i in range(na - 1, -1, -k):
			ans += arr[i] * 2
		return ans
	print(count(pos) + count(neg) - maximum)
tcase()


from collections import Counter
from queue import Queue
import math
T = int(input())
for t in range(T):
	(n, k) = [int(s) for s in input().split(' ')]
	A = [int(s) for s in input().split(' ')]
	A.sort()
	ans = 0
	for idx in range((n + k - 1) // k):
		if A[idx * k] < 0:
			ans += -A[idx * k] * 2
		else:
			break
	A.reverse()
	for idx in range((n + k - 1) // k):
		if A[idx * k] >= 0:
			ans += A[idx * k] * 2
		else:
			break
	ans -= max(abs(A[0]), abs(A[-1]))
	print(ans)


def worst(X, K):
	if len(X) == 0:
		return 0
	L = len(X)
	total = 0
	total += X[-1] * 2
	L -= K
	while L > 0:
		total += X[L - 1] * 2
		L -= K
	return total

def best(X, K):
	if len(X) == 0:
		return 0
	L = len(X)
	total = 0
	total += X[-1]
	L -= K
	while L > 0:
		total += X[L - 1] * 2
		L -= K
	return total

def solve():
	[N, K] = [int(x) for x in input().split()]
	D = [int(x) for x in input().split()]
	POS = [x for x in D if x > 0]
	NEG = [-x for x in D if x < 0]
	POS.sort()
	NEG.sort()
	res = best(POS, K) + worst(NEG, K)
	res2 = best(NEG, K) + worst(POS, K)
	return min(res, res2)
T = int(input())
for i in range(T):
	print(solve())


t = int(input())
for h in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	l = 0
	(b, c) = ([], [])
	m = max(-1 * a[0], a[-1])
	if a[0] < 0 and a[-1] > 0:
		for i in range(1, n):
			if a[i - 1] < 0 and a[i] >= 0:
				b = a[:i]
				c = a[i:]
				break
	elif a[0] >= 0:
		c = a[:]
	else:
		b = a[:]
	i = 0
	while i < len(b):
		l += b[i] * -2
		i += k
	i = len(c) - 1
	while i >= 0:
		l += c[i] * 2
		i -= k
	print(l - m)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	j = -1
	k1 = -1
	s = 0
	m = 0
	for i in range(n):
		if a[i] < 0:
			j = i
		if a[i] > 0:
			k1 = i
			break
	if j != -1:
		b = a[:j + 1]
		r = 0
		while r <= len(b) - 1:
			s += 2 * abs(b[r])
			r += k
		if abs(b[0]) > m:
			m = abs(b[0])
	if k1 != -1:
		b = a[k1:]
		r = len(b) - 1
		while r >= 0:
			s += b[r] * 2
			r -= k
		if b[-1] > m:
			m = b[-1]
	s -= m
	print(s)


def readArr():
	return [int(x) for x in input().split()]

def solve():
	(n, k) = readArr()
	arr = readArr()
	(a, b) = ([], [])
	for el in arr:
		if el > 0:
			a.append(el)
		elif el < 0:
			b.append(-el)
	a.sort()
	b.sort()

	def get_min_dist(x):
		dist = 0
		for i in range(len(x) - 1, -1, -k):
			if i == len(x) - 1:
				dist += x[i]
			else:
				dist += 2 * x[i]
		return dist

	def get_max_dist(x):
		dist = 0
		for i in range(len(x) - 1, -1, -k):
			dist += 2 * x[i]
		return dist
	ans = min(get_min_dist(a) + get_max_dist(b), get_max_dist(a) + get_min_dist(b))
	print(ans)
T = int(input())
for _ in range(T):
	solve()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	x = list(map(int, input().split()))
	if k == 1:
		ans = 0
		m = 0
		for i in range(n):
			ans += abs(x[i])
			if abs(x[i]) > m:
				m = abs(x[i])
		ans *= 2
		ans -= m
	else:
		ans = 0
		a = []
		b = []
		for i in range(n):
			if x[i] > 0:
				a += [x[i]]
			elif x[i] < 0:
				b += [abs(x[i])]
		a = sorted(a, reverse=True)
		b = sorted(b, reverse=True)
		for i in range(0, len(a), k):
			ans += a[i]
		for i in range(0, len(b), k):
			ans += b[i]
		ans *= 2
		if len(a) > 0 and len(b) > 0:
			ans -= max(a[0], b[0])
		elif len(a) > 0:
			ans -= a[0]
		elif len(b) > 0:
			ans -= b[0]
	print(ans)


cases = int(input())
for i in range(cases):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	pos = list(filter(lambda x: x >= 0, arr))
	pos.sort()
	neg = list(map(abs, filter(lambda x: x < 0, arr)))
	neg.sort()
	if neg and pos:
		if max(pos) > max(neg):
			count = 0
			n = len(neg)
			p = n - 1
			while p >= 0:
				count += 2 * neg[p]
				p -= k
			n = len(pos)
			p = n - 1
			while p >= 0:
				if p == n - 1:
					count += pos[n - 1]
				else:
					count += 2 * pos[p]
				p -= k
		else:
			count = 0
			n = len(pos)
			p = n - 1
			while p >= 0:
				count += 2 * pos[p]
				p -= k
			n = len(neg)
			p = n - 1
			while p >= 0:
				if p == n - 1:
					count += neg[n - 1]
				else:
					count += 2 * neg[p]
				p -= k
	elif pos:
		count = 0
		p = n - 1
		while p >= 0:
			if p == n - 1:
				count += pos[n - 1]
			else:
				count += 2 * pos[p]
			p -= k
	else:
		count = 0
		p = n - 1
		while p >= 0:
			if p == n - 1:
				count += neg[n - 1]
			else:
				count += 2 * neg[p]
			p -= k
	print(count)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	pos_arr = []
	neg_arr = []
	for i in arr:
		if i >= 0:
			pos_arr.append(i)
		else:
			neg_arr.append(abs(i))
	pos_arr.sort(reverse=True)
	neg_arr.sort(reverse=True)
	ans = 0
	for i in range(0, len(pos_arr), k):
		ans += 2 * pos_arr[i]
	for i in range(0, len(neg_arr), k):
		ans += 2 * neg_arr[i]
	if len(pos_arr) == 0:
		ans -= abs(neg_arr[0])
	elif len(neg_arr) == 0:
		ans -= pos_arr[0]
	else:
		ans -= max(pos_arr[0], abs(neg_arr[0]))
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	a = []
	b = []
	for el in arr:
		if el >= 0:
			a.append(el)
		elif el < 0:
			b.append(el)
	if a:
		a.sort(reverse=True)
	if b:
		b.sort()
	ans = []
	i = 0
	while i < len(a):
		ans.append(a[i])
		i += k
	i = 0
	while i < len(b):
		ans.append(-b[i])
		i += k
	ans.sort()
	res = 0
	for el in ans:
		res += 2 * el
	res -= ans[-1]
	print(res)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	z = max(abs(l[0]), abs(l[-1]))
	a = []
	b = []
	for i in range(n):
		if l[i] < 0:
			a.append(l[i])
		else:
			b.append(l[i])
	s = 0
	for i in range(0, len(a), k):
		s = s + 2 * abs(a[i])
	b = b[::-1]
	for i in range(0, len(b), k):
		s = s + 2 * abs(b[i])
	print(s - z)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(otr, pol) = ([], [])
	s = []
	for x in input().split():
		x = int(x)
		if x < 0:
			otr.append(-x)
		elif x > 0:
			pol.append(x)
	pol.sort()
	a = len(pol) % k
	if a != 0:
		s.append(pol[a - 1] * 2)
		pol = pol[a:]
	pol = [pol[k * (i + 1) - 1] * 2 for i in range(len(pol) // k)]
	otr.sort()
	a = len(otr) % k
	if a != 0:
		s.append(2 * otr[a - 1])
		otr = otr[a:]
	otr = [otr[k * (i + 1) - 1] * 2 for i in range(len(otr) // k)]
	if len(otr + pol + s) == 0:
		print(0)
	else:
		print(sum(otr) + sum(pol) + sum(s) - max(otr + pol + s) // 2)


t = int(input())
for q in range(t):
	(n, k) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	pos = []
	neg = []
	ind = 0
	for i in range(n):
		if arr[i] < 0:
			neg.append(-arr[i])
		elif arr[i] > 0:
			pos.append(arr[i])
	g = len(neg)
	p = len(pos)
	pos.sort()
	neg.sort()
	ans = 0
	pans = 0
	pi = p - 1
	while pi >= 0:
		pans += 2 * pos[pi]
		pi -= k
	gans = 0
	gi = g - 1
	while gi >= 0:
		gans += 2 * neg[gi]
		gi -= k
	subp = 0
	if pos:
		subp = pos[p - 1]
	subn = 0
	if neg:
		subn = neg[g - 1]
	ans = gans + pans - max(subp, subn)
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	(*a,) = map(int, input().split())
	x = []
	h = []
	for i in a:
		if i >= 0:
			x.append(i)
		else:
			h.append(i)
	cnt = 0
	x.sort()
	p = 0
	neg = 0
	for i in range(len(x) - 1, -1, -k):
		cnt += x[i] * 2
		p = max(p, x[i])
	h.sort(reverse=True)
	for i in range(len(h) - 1, -1, -k):
		cnt += -h[i] * 2
		neg = max(neg, -h[i])
	cnt -= max(p, neg)
	print(cnt)


def solution(depots, k):
	pos_depots = sorted(list(filter(lambda x: x > 0, depots)))
	neg_depots = sorted(list(map(lambda x: -x, filter(lambda x: x < 0, depots))))
	return pos_solution(pos_depots, k) + pos_solution(neg_depots, k) - max(pos_depots[-1] if pos_depots else 0, neg_depots[-1] if neg_depots else 0)

def pos_solution(arr, k):
	if arr:
		traveled = 0
		for i in range(len(arr) - 1, -1, -k):
			traveled += 2 * arr[i]
		_hy_anon_var_1 = traveled
	else:
		_hy_anon_var_1 = 0
	return _hy_anon_var_1
t = int(input())
for _ in range(t):
	[n, k] = list(map(int, input().split()))
	depots = list(map(int, input().split()))
	print(solution(depots, k))


t = int(input())
while t:
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	lp = []
	ln = []
	for i in range(n):
		if l[i] > 0:
			lp.append(l[i])
		else:
			ln.append(abs(l[i]))
	lp.sort(reverse=True)
	ln.sort(reverse=True)
	ans = 0
	for i in range(0, len(lp), k):
		ans += 2 * lp[i]
	for i in range(0, len(ln), k):
		ans += 2 * ln[i]
	if len(lp):
		m1 = lp[0]
	else:
		m1 = 0
	if len(ln):
		m2 = ln[0]
	else:
		m2 = 0
	ans = ans - max(m1, m2)
	print(ans)
	t -= 1


for i in range(int(input())):
	(n, k) = map(int, input().split())
	nums = list(map(int, input().split()))
	pos_nums = [e for e in nums if e > 0]
	neg_nums = [abs(e) for e in nums if e < 0]
	pos_nums.sort()
	neg_nums.sort()
	temp_pos = pos_nums[-1] if pos_nums else 0
	temp_neg = neg_nums[-1] if neg_nums else 0
	ans = 0
	while pos_nums:
		add = pos_nums[-1]
		for e in range(k):
			if pos_nums:
				pos_nums.pop()
		ans += add * 2
	while neg_nums:
		add = neg_nums[-1]
		for e in range(k):
			if neg_nums:
				neg_nums.pop()
		ans += add * 2
	print(ans - max(temp_pos, temp_neg))


t = int(input())
for testcase_num in range(t):
	(n, k) = map(int, input().split())
	x = list(map(int, input().split()))
	plusx = []
	minusx = []
	for i in range(n):
		if x[i] > 0:
			plusx.append(x[i])
		elif x[i] < 0:
			minusx.append(-1 * x[i])
	plusx.sort(reverse=True)
	minusx.sort(reverse=True)
	distance = 0
	i = 0
	while i < len(plusx):
		distance += plusx[i] * 2
		i += k
	i = 0
	while i < len(minusx):
		distance += minusx[i] * 2
		i += k
	pmax = 0
	mmax = 0
	if not len(plusx) == 0:
		pmax = plusx[0]
	if not len(minusx) == 0:
		mmax = minusx[0]
	dismax = max(pmax, mmax)
	distance -= dismax
	print(distance)


h = int(input())
for o in range(h):
	b = list(map(int, input().split()))
	n = b[0]
	k = b[1]
	a = list(map(int, input().split()))
	plus = [0]
	neg = [0]
	for i in range(n):
		if a[i] > 0:
			plus.append(a[i])
		else:
			neg.append(a[i])
	plus.sort()
	neg.sort(reverse=True)
	l = len(plus)
	l1 = len(neg)
	i = 0
	sum1 = 0
	sum2 = 0
	while l - k * i > 0:
		sum1 += plus[l - k * i - 1]
		i += 1
	i = 0
	while l1 - k * i > 0:
		sum2 += abs(neg[l1 - k * i - 1])
		i += 1
	print((sum1 + sum2) * 2 - max(max(plus), abs(min(neg))))


def getvalue(alist, k, length):
	if length == 0:
		return 0
	current = length % k
	result = 0
	if current != 0:
		result += 2 * alist[current - 1]
	while True:
		if current == length:
			break
		current += k
		result += 2 * alist[current - 1]
	return result - alist[length - 1]
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	alist = list(map(int, input().split()))
	(p, o) = ([], [])
	(plen, olen) = (0, 0)
	for i in alist:
		if i < 0:
			o.append(-i)
			olen += 1
		else:
			p.append(i)
			plen += 1
	p.sort()
	o.sort()
	result = getvalue(p, k, plen) + getvalue(o, k, olen)
	if plen != 0 and olen != 0:
		result += min(p[plen - 1], o[olen - 1])
	print(result)


from collections import *
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	a = []
	b = []
	for i in range(n):
		if l[i] >= 0:
			a.append(l[i])
		else:
			b.append(l[i])
	ans = 0
	a.sort()
	b.sort(reverse=True)
	p = len(a)
	q = len(b)
	ks = []
	if p > 0:
		c = p - 1
		ks.append(a[c])
		c -= k
		while c >= 0:
			ks.append(a[c])
			c -= k
	if q > 0:
		c = q - 1
		ks.append(-b[c])
		c -= k
		while c >= 0:
			ks.append(-b[c])
			c -= k
	ans = 0
	ks.sort()
	x = len(ks)
	for i in range(x - 1):
		ans += ks[i] * 2
	ans += ks[-1]
	print(ans)


kl = int(input())
for kkl in range(kl):
	(n, k) = map(int, input().split())
	a = [int(i) for i in input().split()]
	b = []
	dl = 1
	for i in range(n):
		if a[i] != 0:
			b.append(a[i])
			dl += 1
	b.append(0)
	b.sort()
	i0 = b.index(0)
	kp = dl - i0 - 1
	kh = kp // k
	rs = kp % k
	rz = b[i0 + rs]
	for i in range(kh):
		rz += b[i0 + rs + k * (i + 1)]
	kh = i0 // k
	rs = i0 % k
	rz += abs(b[i0 - rs])
	for i in range(kh):
		rz += abs(b[i0 - (rs + k * (i + 1))])
	rz *= 2
	print(rz - max(-b[0], abs(b[dl - 1])))


problem_num = int(input())
for i in range(problem_num):
	nk = input()
	nk = nk.split()
	n = int(nk[0])
	k = int(nk[1])
	x = input()
	arr = x.split()
	pos = [int(a) for a in arr if int(a) >= 0]
	neg = [abs(int(a)) for a in arr if int(a) < 0]
	pos.sort(reverse=True)
	pos = pos[::k]
	neg.sort(reverse=True)
	neg = neg[::k]
	max_value = max(pos + neg)
	print(2 * sum(pos + neg) - max_value)


a = int(input())
for x in range(a):
	(b, c) = map(int, input().split())
	d = list(map(int, input().split()))
	pos = []
	poslen = 0
	neg = []
	neglen = 0
	for y in range(b):
		if d[y] >= 0:
			pos.append(d[y])
			poslen += 1
		else:
			neg.append(d[y] * -1)
			neglen += 1
	pos.sort()
	neg.sort()
	e = poslen
	f = neglen
	j = 0
	h = 0
	while poslen > 0:
		j += 2 * pos[poslen - 1]
		poslen -= c
	if neglen > 0:
		j += neg[neglen - 1]
		s = neglen - c
		while s > 0:
			j += 2 * neg[s - 1]
			s -= c
	while f > 0:
		h += 2 * neg[f - 1]
		f -= c
	if e > 0:
		h += pos[e - 1]
		k = e - c
		while k > 0:
			h += 2 * pos[k - 1]
			k -= c
	print(min(j, h))


t = int(input())

def solve():
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))

	def sim(locs):
		pos = []
		neg = []
		for i in locs:
			if i > 0:
				pos.append(i)
			else:
				neg.append(0 - i)
		pos.sort()
		neg.sort()
		ans = 0
		p = len(pos) - 1
		while p >= 0:
			ans += pos[p] * 2
			p -= k
		if len(pos) > 0:
			ans -= pos[-1]
		p = len(neg) - 1
		while p >= 0:
			ans += neg[p] * 2
			p -= k
		return ans
	ans1 = sim(arr)
	arr = [0 - i for i in arr]
	ans2 = sim(arr)
	print(min(ans1, ans2))
for i in range(t):
	solve()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = [int(i) for i in input().split()]
	(ne, po, last, res) = ([], [], -1, 0)
	for el in a:
		if el > 0:
			po += [el]
		elif el < 0:
			ne += [abs(el)]
	if len(po) == len(ne) == 0:
		print(0)
		continue
	po.sort()
	ne.sort()
	if len(po):
		last = po[-1]
		for i in range(len(po), 0, -k):
			res += 2 * po[i - 1]
	if len(ne):
		last = max(last, ne[-1])
		for i in range(len(ne), 0, -k):
			res += 2 * ne[i - 1]
	print(res - last)


t = int(input())
for _ in range(t):
	(n, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	a_1 = []
	a_2 = []
	m = a[0]
	for x in a:
		if x > 0:
			a_2.append(x)
		elif x < 0:
			a_1.append(-x)
		m = max(m, abs(x))
	a_1.sort()
	a_2.sort()
	way = 0
	i = len(a_1) - 1
	while i >= 0:
		way += 2 * a_1[i]
		i -= k
	i = len(a_2) - 1
	while i >= 0:
		way += 2 * a_2[i]
		i -= k
	way -= m
	print(way)


t = int(input())
import math
for e in range(t):
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	l2.sort()
	lp = []
	ln = []
	for i in l2:
		if i < 0:
			ln.append(-i)
		if i > 0:
			lp.append(i)
	lp.sort(reverse=True)
	cp = 0
	cn = 0
	if len(lp) == 0:
		cp = 0
	else:
		q1 = math.ceil(len(lp) / l1[1])
		p1 = 0
		for i in range(q1 - 1):
			cp += 2 * lp[p1]
			p1 += l1[1]
		cp += 2 * lp[p1]
	if len(ln) == 0:
		cn = 0
	else:
		q1 = math.ceil(len(ln) / l1[1])
		p1 = 0
		for i in range(q1 - 1):
			cn += 2 * ln[p1]
			p1 += l1[1]
		cn += 2 * ln[p1]
	if len(lp) > 0 and len(ln) > 0:
		c = min(cn + cp - lp[0], cn + cp - ln[0])
		print(c)
	if len(lp) == 0 and len(ln) > 0:
		c = cn - ln[0]
		print(c)
	if len(lp) > 0 and len(ln) == 0:
		c = cp - lp[0]
		print(c)
	if len(lp) == 0 and len(ln) == 0:
		print(0)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	array = list(map(int, input().split()))
	n = list(filter(lambda x: x < 0, array))
	p = list(filter(lambda x: x > 0, array))
	n = sorted(n)
	p = sorted(p, reverse=True)
	steps = 0
	c = k
	for i in range(max(len(n), len(p))):
		if i < len(n):
			if c == k:
				steps += abs(2 * n[i])
		if i < len(p):
			if c == k:
				steps += 2 * p[i]
		c -= 1
		if c == 0:
			c = k
	a = abs(n[0]) if len(n) > 0 else 0
	b = p[0] if len(p) > 0 else 0
	steps -= max(a, b)
	print(steps)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.sort()
	pos = list()
	neg = list()
	for i in arr:
		if i < 0:
			neg.append(i)
		else:
			pos.append(i)
	(p, n) = (0, 0)
	i = len(pos) - 1
	ini = k
	while i >= 0:
		if p == 0:
			p += 2 * pos[i]
		if ini == 0:
			p += 2 * pos[i]
			ini = k
		ini -= 1
		i -= 1
	i = 0
	ini = k
	while i < len(neg):
		if n == 0:
			n += 2 * abs(neg[i])
		if ini == 0:
			n += 2 * abs(neg[i])
			ini = k
		ini -= 1
		i += 1
	if len(neg) == 0:
		ans = p - max(pos)
	elif len(pos) == 0:
		ans = n + min(neg)
	else:
		ans = p + n - max(abs(min(neg)), max(pos))
	print(ans)


def fun(a):
	pos = []
	neg = []
	for j in range(n):
		if a[j] > 0:
			pos.append(a[j])
		else:
			neg.append(0 - a[j])
	cnt = 0
	pos.sort()
	neg.sort()
	b = len(pos) - 1
	while b >= 0:
		cnt += pos[b] * 2
		b -= k
	if len(pos) > 0:
		cnt -= pos[-1]
	b = len(neg) - 1
	while b >= 0:
		cnt += neg[b] * 2
		b -= k
	return cnt
for i in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = []
	for j in range(n):
		b.append(0 - a[j])
	print(min(fun(a), fun(b)))


def gt(a, b):
	ans = a[-1] if len(a) else 0
	while len(a):
		for _ in range(k):
			if len(a):
				a.pop()
		if len(a):
			ans += a[-1] * 2
	while len(b):
		if len(b):
			ans += b[-1] * 2
		for _ in range(k):
			if len(b):
				b.pop()
	return ans
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(a, b) = ([], [])
	for i in map(int, input().split()):
		if i > 0:
			a.append(i)
		else:
			b.append(-i)
	a.sort()
	b.sort()
	print(min(gt(a.copy(), b.copy()), gt(b.copy(), a.copy())))


cases = int(input())
for loop in range(cases):
	(n, k) = input().split(' ')
	posdata = input().split(' ')
	negdata = []
	for i in range(len(posdata) - 1, -1, -1):
		posdata[i] = int(posdata[i])
		if posdata[i] < -0:
			negdata.append(posdata.pop(i) * -1)
	posdata = sorted(posdata)
	negdata = sorted(negdata)
	tot = 0
	most = -1
	for data in [posdata, negdata]:
		while len(data) > 0:
			dist = data[-1]
			for i in range(int(k)):
				if len(data) > 0:
					del data[-1]
			tot += dist * 2
			if most == -1 or dist > most:
				most = dist
	tot -= most
	print(tot)