#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
void check() {
  int n;
  cin >> n;
  map<long long, long long> mp, mp2;
  for (int i = 0, x; i < n; ++i) {
    cin >> x;
    mp[x]++;
  }
  long long mxx = -1;
  for (auto e : mp) {
    mp2[e.second]++;
  }
  long long ans = 0ll, mx = 0x3f3f3f3f;
  for (map<long long, long long>::reverse_iterator rit = mp2.rbegin();
       rit != mp2.rend(); rit++) {
    rit->second = min(rit->first, min(mx, rit->second));
    long long res = rit->second * (rit->second - 1ll) / 2ll;
    if (mx > rit->first) {
      mx = rit->first;
    }
    ans += max(mx * rit->second - res, 0ll);
    mx = mx - rit->second;
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    check();
  }
}
