#include <bits/stdc++.h>
using namespace std;
#define int long long int
const int MOD = 1000000007;
// Fenwick Tree for efficient prefix sum queries and updates
class FenwickTree {
public:
vector<int> fenwick;
FenwickTree(int n) {
fenwick.resize(n + 1, 0);
}
void update(int idx, int val) {
for (; idx < fenwick.size(); idx += idx & -idx) {
fenwick[idx] += val;
}
}
int query(int idx) {
int sum = 0;
for (; idx > 0; idx -= idx & -idx) {
sum += fenwick[idx];
}
return sum;
}
};
int32_t main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> p(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
}
sort(p.begin(), p.end());
// Initialize Fenwick Tree for prefix sums
FenwickTree fenwick(n);
for (int i = 0; i < n; i++) {
fenwick.update(i + 1, p[i]); // 1-based indexing for Fenwick Tree
}
// Process each length m
for (int m = 1; m <= n; m++) {
int rem = m, cost = 0, idx = m;
// Calculate segments required
int segments = (rem + k) / (k + 1);
if (segments * (k + 1) < rem) segments++;
for (int i = 0; i < segments && rem > 0; i++) {
int take = min(k, rem);
if (idx - take >= 0) {
cost += fenwick.query(idx) - fenwick.query(idx - take);
}
idx -= (take + 1);
rem -= (take + 1);
}
cout << cost << " ";
}
cout << "\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgaW50IGxvbmcgbG9uZyBpbnQKY29uc3QgaW50IE1PRCA9IDEwMDAwMDAwMDc7CgovLyBGZW53aWNrIFRyZWUgZm9yIGVmZmljaWVudCBwcmVmaXggc3VtIHF1ZXJpZXMgYW5kIHVwZGF0ZXMKY2xhc3MgRmVud2lja1RyZWUgewpwdWJsaWM6CiAgICB2ZWN0b3I8aW50PiBmZW53aWNrOwogICAgRmVud2lja1RyZWUoaW50IG4pIHsKICAgICAgICBmZW53aWNrLnJlc2l6ZShuICsgMSwgMCk7CiAgICB9CgogICAgdm9pZCB1cGRhdGUoaW50IGlkeCwgaW50IHZhbCkgewogICAgICAgIGZvciAoOyBpZHggPCBmZW53aWNrLnNpemUoKTsgaWR4ICs9IGlkeCAmIC1pZHgpIHsKICAgICAgICAgICAgZmVud2lja1tpZHhdICs9IHZhbDsKICAgICAgICB9CiAgICB9CgogICAgaW50IHF1ZXJ5KGludCBpZHgpIHsKICAgICAgICBpbnQgc3VtID0gMDsKICAgICAgICBmb3IgKDsgaWR4ID4gMDsgaWR4IC09IGlkeCAmIC1pZHgpIHsKICAgICAgICAgICAgc3VtICs9IGZlbndpY2tbaWR4XTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHN1bTsKICAgIH0KfTsKCmludDMyX3QgbWFpbigpIHsKICAgIGludCB0OwogICAgY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgaW50IG4sIGs7CiAgICAgICAgY2luID4+IG4gPj4gazsKICAgICAgICAKICAgICAgICB2ZWN0b3I8aW50PiBwKG4pOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGNpbiA+PiBwW2ldOwogICAgICAgIH0KICAgICAgICAKICAgICAgICBzb3J0KHAuYmVnaW4oKSwgcC5lbmQoKSk7CgogICAgICAgIC8vIEluaXRpYWxpemUgRmVud2ljayBUcmVlIGZvciBwcmVmaXggc3VtcwogICAgICAgIEZlbndpY2tUcmVlIGZlbndpY2sobik7CgogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGZlbndpY2sudXBkYXRlKGkgKyAxLCBwW2ldKTsgIC8vIDEtYmFzZWQgaW5kZXhpbmcgZm9yIEZlbndpY2sgVHJlZQogICAgICAgIH0KICAgICAgICAKICAgICAgICAvLyBQcm9jZXNzIGVhY2ggbGVuZ3RoIG0KICAgICAgICBmb3IgKGludCBtID0gMTsgbSA8PSBuOyBtKyspIHsKICAgICAgICAgICAgaW50IHJlbSA9IG0sIGNvc3QgPSAwLCBpZHggPSBtOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gQ2FsY3VsYXRlIHNlZ21lbnRzIHJlcXVpcmVkCiAgICAgICAgICAgIGludCBzZWdtZW50cyA9IChyZW0gKyBrKSAvIChrICsgMSk7CiAgICAgICAgICAgIGlmIChzZWdtZW50cyAqIChrICsgMSkgPCByZW0pIHNlZ21lbnRzKys7CiAgICAgICAgICAgIAogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHNlZ21lbnRzICYmIHJlbSA+IDA7IGkrKykgewogICAgICAgICAgICAgICAgaW50IHRha2UgPSBtaW4oaywgcmVtKTsKICAgICAgICAgICAgICAgIGlmIChpZHggLSB0YWtlID49IDApIHsKICAgICAgICAgICAgICAgICAgICBjb3N0ICs9IGZlbndpY2sucXVlcnkoaWR4KSAtIGZlbndpY2sucXVlcnkoaWR4IC0gdGFrZSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZHggLT0gKHRha2UgKyAxKTsKICAgICAgICAgICAgICAgIHJlbSAtPSAodGFrZSArIDEpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICBjb3V0IDw8IGNvc3QgPDwgIiAiOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8ICJcbiI7CiAgICB9CiAgICByZXR1cm4gMDsKfQo=