#include <bits/stdc++.h>
using namespace std;
void solve() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
// Sort the costs
sort(v.begin(), v.end());
// Compute prefix sums
vector<long long> prefix(n + 1, 0);
for (int i = 1; i <= n; i++) {
prefix[i] = prefix[i - 1] + v[i - 1];
}
// Calculate the minimum cost for each m
vector<long long> result(n);
for (int m = 1; m <= n; m++) {
int free_items = (m - 1) / (k + 1);
int paid_items = m - free_items;
result[m - 1] = prefix[paid_items];
}
// Output the result for this test case
for (int i = 0; i < n; i++) {
cout << result[i] << " ";
}
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIHNvbHZlKCkgewogICAgaW50IHQ7CiAgICBjaW4gPj4gdDsKICAgIHdoaWxlICh0LS0pIHsKICAgICAgICBpbnQgbiwgazsKICAgICAgICBjaW4gPj4gbiA+PiBrOwogICAgICAgIHZlY3RvcjxpbnQ+IHYobik7CiAgICAgICAgCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgY2luID4+IHZbaV07CiAgICAgICAgfQoKICAgICAgICAvLyBTb3J0IHRoZSBjb3N0cwogICAgICAgIHNvcnQodi5iZWdpbigpLCB2LmVuZCgpKTsKCiAgICAgICAgLy8gQ29tcHV0ZSBwcmVmaXggc3VtcwogICAgICAgIHZlY3Rvcjxsb25nIGxvbmc+IHByZWZpeChuICsgMSwgMCk7CiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgICAgIHByZWZpeFtpXSA9IHByZWZpeFtpIC0gMV0gKyB2W2kgLSAxXTsKICAgICAgICB9CgogICAgICAgIC8vIENhbGN1bGF0ZSB0aGUgbWluaW11bSBjb3N0IGZvciBlYWNoIG0KICAgICAgICB2ZWN0b3I8bG9uZyBsb25nPiByZXN1bHQobik7CiAgICAgICAgZm9yIChpbnQgbSA9IDE7IG0gPD0gbjsgbSsrKSB7CiAgICAgICAgICAgIGludCBmcmVlX2l0ZW1zID0gKG0gLSAxKSAvIChrICsgMSk7CiAgICAgICAgICAgIGludCBwYWlkX2l0ZW1zID0gbSAtIGZyZWVfaXRlbXM7CiAgICAgICAgICAgIHJlc3VsdFttIC0gMV0gPSBwcmVmaXhbcGFpZF9pdGVtc107CiAgICAgICAgfQoKICAgICAgICAvLyBPdXRwdXQgdGhlIHJlc3VsdCBmb3IgdGhpcyB0ZXN0IGNhc2UKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBjb3V0IDw8IHJlc3VsdFtpXSA8PCAiICI7CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgIlxuIjsKICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUobnVsbHB0cik7CiAgICBzb2x2ZSgpOwogICAgcmV0dXJuIDA7Cn0K