#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int main() {
long long t;
cin >> t;
while (t--) {
long long n, k;
cin >> n >> k;
vector<long long> a(n);
map<long long, long long> check;
for (long long i = 0; i < n; i++) {
cin >> a[i];
check[a[i]]++;
}
// If k >= n, we can make all elements different
if (k >= n) {
cout << (n * (n - 1)) / 2 << "\n";
continue;
}
// Calculate initial pairs that are different
long long res = (n * (n - 1)) / 2;
// Subtract pairs that are same
for (auto it : check) {
if (it.second > 1) {
res -= (it.second * (it.second - 1)) / 2;
}
}
// Create multiset of frequencies > 1
multiset<long long> ans;
for (auto it : check) {
if (it.second > 1) {
ans.insert(it.second);
}
}
// Process k operations
while (!ans.empty() && k > 0) {
long long temp = *prev(ans.end());
ans.erase(temp);
if (temp > 1) {
k--;
temp--;
res += temp; // Add new pairs formed
}
if (temp > 1) {
ans.insert(temp); // Insert reduced frequency if still > 1
}
}
cout << res << "\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGxvbmcgbG9uZyBNT0QgPSAxMDAwMDAwMDA3OwppbnQgbWFpbigpIHsKICAgIGxvbmcgbG9uZyB0OwogICAgY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgbG9uZyBsb25nIG4sIGs7CiAgICAgICAgY2luID4+IG4gPj4gazsKICAgICAgICB2ZWN0b3I8bG9uZyBsb25nPiBhKG4pOwogICAgICAgIG1hcDxsb25nIGxvbmcsIGxvbmcgbG9uZz4gY2hlY2s7CiAgICAgICAgCiAgICAgICAgZm9yIChsb25nIGxvbmcgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgY2luID4+IGFbaV07CiAgICAgICAgICAgIGNoZWNrW2FbaV1dKys7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIC8vIElmIGsgPj0gbiwgd2UgY2FuIG1ha2UgYWxsIGVsZW1lbnRzIGRpZmZlcmVudAogICAgICAgIGlmIChrID49IG4pIHsKICAgICAgICAgICAgY291dCA8PCAobiAqIChuIC0gMSkpIC8gMiA8PCAiXG4iOwogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgLy8gQ2FsY3VsYXRlIGluaXRpYWwgcGFpcnMgdGhhdCBhcmUgZGlmZmVyZW50CiAgICAgICAgbG9uZyBsb25nIHJlcyA9IChuICogKG4gLSAxKSkgLyAyOwogICAgICAgIAogICAgICAgIC8vIFN1YnRyYWN0IHBhaXJzIHRoYXQgYXJlIHNhbWUKICAgICAgICBmb3IgKGF1dG8gaXQgOiBjaGVjaykgewogICAgICAgICAgICBpZiAoaXQuc2Vjb25kID4gMSkgewogICAgICAgICAgICAgICAgcmVzIC09IChpdC5zZWNvbmQgKiAoaXQuc2Vjb25kIC0gMSkpIC8gMjsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAKICAgICAgICAvLyBDcmVhdGUgbXVsdGlzZXQgb2YgZnJlcXVlbmNpZXMgPiAxCiAgICAgICAgbXVsdGlzZXQ8bG9uZyBsb25nPiBhbnM7CiAgICAgICAgZm9yIChhdXRvIGl0IDogY2hlY2spIHsKICAgICAgICAgICAgaWYgKGl0LnNlY29uZCA+IDEpIHsKICAgICAgICAgICAgICAgIGFucy5pbnNlcnQoaXQuc2Vjb25kKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAKICAgICAgICAvLyBQcm9jZXNzIGsgb3BlcmF0aW9ucwogICAgICAgIHdoaWxlICghYW5zLmVtcHR5KCkgJiYgayA+IDApIHsKICAgICAgICAgICAgbG9uZyBsb25nIHRlbXAgPSAqcHJldihhbnMuZW5kKCkpOwogICAgICAgICAgICBhbnMuZXJhc2UodGVtcCk7CiAgICAgICAgICAgIAogICAgICAgICAgICBpZiAodGVtcCA+IDEpIHsKICAgICAgICAgICAgICAgIGstLTsKICAgICAgICAgICAgICAgIHRlbXAtLTsKICAgICAgICAgICAgICAgIHJlcyArPSB0ZW1wOyAgLy8gQWRkIG5ldyBwYWlycyBmb3JtZWQKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodGVtcCA+IDEpIHsKICAgICAgICAgICAgICAgIGFucy5pbnNlcnQodGVtcCk7ICAvLyBJbnNlcnQgcmVkdWNlZCBmcmVxdWVuY3kgaWYgc3RpbGwgPiAxCiAgICAgICAgCX0KICAgICAgICB9CiAgICAgICAgCiAgICAgICAgY291dCA8PCByZXMgPDwgIlxuIjsKICAgIH0KICAgIHJldHVybiAwOwp9