#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t; cin >> t;
while(t-->0) {
int n,x; cin >> n >> x;
priority_queue<int> a;
for(int i=0;i<n;++i) {
int temp; cin >> temp;
a.push(temp);
}
a.push(0);
int result=0;
while (1){
int val=0; vector<int> b;
for(int i=0;i<x;++i) {
if(a.top()==0) break;
val=a.top();
b.push_back(val);
a.pop();
}
if(val==0) break;
result+=val;
for(int k:b) {
a.push(k-val);
}
}
cout << result << "\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKCS8vIHlvdXIgY29kZSBnb2VzIGhlcmUKICAgIGludCB0OyBjaW4gPj4gdDsKICAgIHdoaWxlKHQtLT4wKSB7CiAgICAgICAgaW50IG4seDsgY2luID4+IG4gPj4geDsKICAgICAgICBwcmlvcml0eV9xdWV1ZTxpbnQ+IGE7CiAgICAgICAgZm9yKGludCBpPTA7aTxuOysraSkgewogICAgICAgICAgICBpbnQgdGVtcDsgY2luID4+IHRlbXA7CiAgICAgICAgICAgIGEucHVzaCh0ZW1wKTsKICAgICAgICB9CiAgICAgICAgYS5wdXNoKDApOwogICAgICAgIGludCByZXN1bHQ9MDsKICAgICAgICB3aGlsZSAoMSl7CiAgICAgICAgICAgIGludCB2YWw9MDsgdmVjdG9yPGludD4gYjsKICAgICAgICAgICAgZm9yKGludCBpPTA7aTx4OysraSkgewogICAgICAgICAgICAgICAgaWYoYS50b3AoKT09MCkgYnJlYWs7CiAgICAgICAgICAgICAgICB2YWw9YS50b3AoKTsKICAgICAgICAgICAgICAgIGIucHVzaF9iYWNrKHZhbCk7CiAgICAgICAgICAgICAgICBhLnBvcCgpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKHZhbD09MCkgYnJlYWs7CiAgICAgICAgICAgIHJlc3VsdCs9dmFsOwogICAgICAgICAgICBmb3IoaW50IGs6YikgewogICAgICAgICAgICAgICAgYS5wdXNoKGstdmFsKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBjb3V0IDw8IHJlc3VsdCA8PCAiXG4iOwogICAgfQoJcmV0dXJuIDA7Cn0=