#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;
}