#include <bits/stdc++.h>
using namespace std;

int main(int argc, char **argv) {
  cin.sync_with_stdio(false), cin.tie(nullptr);

  int tests;
  cin >> tests;

  while (tests--) {
    int n;
    cin >> n;

    map<int, int> cnt;

    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      cnt[x] += 1;
    }

    const int m = cnt.size();

    vector<int> a;
    a.reserve(m);

    for (auto x : cnt) {
      a.push_back(x.second);
    }
    
    sort(a.begin(), a.end());

    vector<int64_t> sum(m + 1, 0);

    for (int i = 0; i < m; i++) {
      sum[i + 1] = a[i] + sum[i];
    }

    int64_t result = n;

    for (int i = 0; i < m; i++) {
      result = min(result, sum[i] + sum[m] - sum[i + 1] - int64_t(m - i - 1) * a[i]);
    }

    cout << result << "\n";
  }
}
