#include <bits/stdc++.h>

#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define eb emplace_back
#define endl '\n'
using namespace std;
ll const N = 103;

void task_a() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  int c = 0;
  for (int i = 0; i < n - 1; ++i) {
    if (min(v[i], v[i + 1]) * 2 < max(v[i], v[i + 1])) {
      int x = max(v[i], v[i + 1]);
      while (min(v[i], v[i + 1]) * 2 < x) {
        if (x % 2 != 0) {
          x /= 2;
          x++;
        } else x /= 2;
        c++;
      }
    }
  }
  cout << c << endl;
}

void task_b() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  int nm = n / 3;
  vector<pair<int, int>> ans;
  int c0 = 0, c1 = 0, c2 = 0;
  for (int i = 0; i < n; ++i) {
    int rem = v[i] % 3;
    if (rem == 0)c0++;
    else if (rem == 1)c1++;
    else if (rem == 2)c2++;
  }
  ans.eb(c0, 0);
  ans.eb(c1, 1);
  ans.eb(c2, 2);
  sort(ans.begin(), ans.end());
  int c = 0;
  while (true) {
    if (ans.back().first == nm)break;
    int dif = 0;
    if (ans.back().first > nm) {
      dif = ans.back().first - nm;
      c += dif;
      ans.back().first = nm;
    }
    for (int j = 0; j < ans.size(); ++j) {
      if (ans[j].second == (ans.back().second + 1) % 3) {
        ans[j].first += dif;
        break;
      }
    }
    sort(ans.begin(), ans.end());
  }
  cout << c << endl;
}

void task_c() {
  int n;
  cin >> n;
  vector<ll> v(n);
  map<ll, ll> mp;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    mp[v[i]]++;
  }
  map<ll, ll> res;
  for (auto i : mp) {
    res[mp[i.first]]++;
  }
  vector<pair<ll, ll>> a;
  for (auto i : res) {
    a.eb(i.first, i.second);
  }
  sort(a.rbegin(), a.rend());
  ll minn = 1e18;
  for (int i = 0; i < a.size(); ++i) {
    ll cnt = 0;
    for (int j = 0; j < i; ++j) {
      ll k = a[j].first - a[i].first;
      k *= a[j].second;
      cnt += k;
    }
    for (int j = i + 1; j < a.size(); ++j) {
      ll k = a[j].first;
      k *= a[j].second;
      cnt += k;
    }
   // cout << cnt << endl;
    minn = min(minn, cnt);
  }
  cout << minn << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tt;
  tt = 1;
  cin >> tt;
  for (int i = 1; i <= tt; ++i) {
    //task_a();
    //task_b();
    task_c();
  }
  return 0;
}