#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<pair<int, int>> customers(n);
vector<int> all_prices;
for (int i = 0; i < n; i++) {
cin >> customers[i].first;
}
for (int i = 0; i < n; i++) {
cin >> customers[i].second;
}
for (int i = 0; i < n; i++) {
all_prices.push_back(customers[i].first);
all_prices.push_back(customers[i].second);
}
sort(all_prices.begin(), all_prices.end());
all_prices.erase(unique(all_prices.begin(), all_prices.end()), all_prices.end());
long long max_revenue = 0;
for (int p : all_prices) {
long long revenue = 0;
priority_queue<int, vector<int>, greater<int>> negative_reviews;
for (const auto& customer : customers) {
if (p <= customer.first) {
// Positive reviewer
revenue += p;
} else if (p <= customer.second) {
// Negative reviewer
revenue += p;
negative_reviews.push(p);
if (negative_reviews.size() > k) {
revenue -= negative_reviews.top();
negative_reviews.pop();
}
}
}
max_revenue = max(max_revenue, revenue);
}
cout << max_revenue << "\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShOVUxMKTsKCiAgICBpbnQgdDsKICAgIGNpbiA+PiB0OwoKICAgIHdoaWxlICh0LS0pIHsKICAgICAgICBpbnQgbiwgazsKICAgICAgICBjaW4gPj4gbiA+PiBrOwoKICAgICAgICB2ZWN0b3I8cGFpcjxpbnQsIGludD4+IGN1c3RvbWVycyhuKTsKICAgICAgICB2ZWN0b3I8aW50PiBhbGxfcHJpY2VzOwoKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBjaW4gPj4gY3VzdG9tZXJzW2ldLmZpcnN0OwogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBjaW4gPj4gY3VzdG9tZXJzW2ldLnNlY29uZDsKICAgICAgICB9CgogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGFsbF9wcmljZXMucHVzaF9iYWNrKGN1c3RvbWVyc1tpXS5maXJzdCk7CiAgICAgICAgICAgIGFsbF9wcmljZXMucHVzaF9iYWNrKGN1c3RvbWVyc1tpXS5zZWNvbmQpOwogICAgICAgIH0KCiAgICAgICAgc29ydChhbGxfcHJpY2VzLmJlZ2luKCksIGFsbF9wcmljZXMuZW5kKCkpOwogICAgICAgIGFsbF9wcmljZXMuZXJhc2UodW5pcXVlKGFsbF9wcmljZXMuYmVnaW4oKSwgYWxsX3ByaWNlcy5lbmQoKSksIGFsbF9wcmljZXMuZW5kKCkpOwoKICAgICAgICBsb25nIGxvbmcgbWF4X3JldmVudWUgPSAwOwoKICAgICAgICBmb3IgKGludCBwIDogYWxsX3ByaWNlcykgewogICAgICAgICAgICBsb25nIGxvbmcgcmV2ZW51ZSA9IDA7CiAgICAgICAgICAgIHByaW9yaXR5X3F1ZXVlPGludCwgdmVjdG9yPGludD4sIGdyZWF0ZXI8aW50Pj4gbmVnYXRpdmVfcmV2aWV3czsKCiAgICAgICAgICAgIGZvciAoY29uc3QgYXV0byYgY3VzdG9tZXIgOiBjdXN0b21lcnMpIHsKICAgICAgICAgICAgICAgIGlmIChwIDw9IGN1c3RvbWVyLmZpcnN0KSB7CiAgICAgICAgICAgICAgICAgICAgLy8gUG9zaXRpdmUgcmV2aWV3ZXIKICAgICAgICAgICAgICAgICAgICByZXZlbnVlICs9IHA7CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKHAgPD0gY3VzdG9tZXIuc2Vjb25kKSB7CiAgICAgICAgICAgICAgICAgICAgLy8gTmVnYXRpdmUgcmV2aWV3ZXIKICAgICAgICAgICAgICAgICAgICByZXZlbnVlICs9IHA7CiAgICAgICAgICAgICAgICAgICAgbmVnYXRpdmVfcmV2aWV3cy5wdXNoKHApOwoKICAgICAgICAgICAgICAgICAgICBpZiAobmVnYXRpdmVfcmV2aWV3cy5zaXplKCkgPiBrKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHJldmVudWUgLT0gbmVnYXRpdmVfcmV2aWV3cy50b3AoKTsKICAgICAgICAgICAgICAgICAgICAgICAgbmVnYXRpdmVfcmV2aWV3cy5wb3AoKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIG1heF9yZXZlbnVlID0gbWF4KG1heF9yZXZlbnVlLCByZXZlbnVlKTsKICAgICAgICB9CgogICAgICAgIGNvdXQgPDwgbWF4X3JldmVudWUgPDwgIlxuIjsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=