#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
void init_code() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int main() {
init_code();
long long t;
cin >> t;
while (t--) {
long long n, k;
cin >> n >> k;
vector<long long> a(n);
vector<long long> hold;
map<long long, long long> check;
for (long long i = 0; i < n; i++) {
cin >> a[i];
check[a[i]]++;
}
for (auto it : check) {
hold.push_back(it.second);
}
sort(hold.begin(), hold.end());
for (long long i = hold.size() - 1; i >= 0 && k > 0; i--) {
if (k >= hold[i] - 1) {
k -= (hold[i] - 1);
hold[i] = 1;
} else {
hold[i] -= k;
k = 0;
}
}
long long ans = (n * (n - 1)) / 2;
for (size_t i = 0; i < hold.size(); i++) {
if (hold[i] > 1) {
ans -= (hold[i] * (hold[i] - 1)) / 2;
}
}
cout << ans << "\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGxvbmcgbG9uZyBNT0QgPSAxMDAwMDAwMDA3Owp2b2lkIGluaXRfY29kZSgpIHsKI2lmbmRlZiBPTkxJTkVfSlVER0UKICAgIGZyZW9wZW4oImlucHV0LnR4dCIsICJyIiwgc3RkaW4pOwogICAgZnJlb3Blbigib3V0cHV0LnR4dCIsICJ3Iiwgc3Rkb3V0KTsKI2VuZGlmCn0KaW50IG1haW4oKSB7CiAgICBpbml0X2NvZGUoKTsKICAgIGxvbmcgbG9uZyB0OwogICAgY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgbG9uZyBsb25nIG4sIGs7CiAgICAgICAgY2luID4+IG4gPj4gazsKICAgICAgICB2ZWN0b3I8bG9uZyBsb25nPiBhKG4pOwogICAgICAgIHZlY3Rvcjxsb25nIGxvbmc+IGhvbGQ7CiAgICAgICAgbWFwPGxvbmcgbG9uZywgbG9uZyBsb25nPiBjaGVjazsKICAgICAgICBmb3IgKGxvbmcgbG9uZyBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBjaW4gPj4gYVtpXTsKICAgICAgICAgICAgY2hlY2tbYVtpXV0rKzsKICAgICAgICB9CiAgICAgICAgZm9yIChhdXRvIGl0IDogY2hlY2spIHsKICAgICAgICAgICAgaG9sZC5wdXNoX2JhY2soaXQuc2Vjb25kKTsKICAgICAgICB9CiAgICAgICAgc29ydChob2xkLmJlZ2luKCksIGhvbGQuZW5kKCkpOwogICAgICAgIGZvciAobG9uZyBsb25nIGkgPSBob2xkLnNpemUoKSAtIDE7IGkgPj0gMCAmJiBrID4gMDsgaS0tKSB7CiAgICAgICAgICAgIGlmIChrID49IGhvbGRbaV0gLSAxKSB7CiAgICAgICAgICAgICAgICBrIC09IChob2xkW2ldIC0gMSk7CiAgICAgICAgICAgICAgICBob2xkW2ldID0gMTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIGhvbGRbaV0gLT0gazsKICAgICAgICAgICAgICAgIGsgPSAwOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGxvbmcgbG9uZyBhbnMgPSAobiAqIChuIC0gMSkpIC8gMjsKICAgICAgICBmb3IgKHNpemVfdCBpID0gMDsgaSA8IGhvbGQuc2l6ZSgpOyBpKyspIHsKICAgICAgICAgICAgaWYgKGhvbGRbaV0gPiAxKSB7CiAgICAgICAgICAgICAgICBhbnMgLT0gKGhvbGRbaV0gKiAoaG9sZFtpXSAtIDEpKSAvIDI7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dCA8PCBhbnMgPDwgIlxuIjsKICAgIH0KICAgIHJldHVybiAwOwp9