#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  long long ans = 0;
  cin >> n;
  vector<int> cnt(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    cnt[--x]++;
  }
  sort(cnt.begin(), cnt.end());
  int amt = cnt[n - 1], i = n - 1;
  while (amt > 0 && i >= 0) {
    ans += 1ll * min(amt, cnt[i]);
    amt = min(amt, cnt[i]) - 1;
    i--;
  }
  cout << ans << '\n';
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    solve();
  }
}
