#include <bits/stdc++.h>
using namespace std;
long long int MOD1 = 1000000007;
long long int MOD2 = 998244353;
void yes() { cout << "YES" << endl; }
void no() { cout << "NO" << endl; }
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int lcm(long long int a, long long int b) {
  return (a * b / (gcd(a, b)));
}
void solve() {
  long long int n;
  cin >> n;
  vector<long long int> cnt(n + 1, 0);
  for (long long int i = 0; i <= n - 1; i++) {
    long long int t;
    cin >> t;
    cnt[t]++;
  }
  vector<long long int> store;
  for (long long int x : cnt) {
    if (x != 0) store.emplace_back(x);
  }
  sort(store.rbegin(), store.rend());
  long long int ans = store[0], lst = store[0];
  for (long long int i = 1; i < store.size(); i++) {
    if (lst == 0) break;
    if (store[i] >= lst) {
      ans += lst - 1;
      lst--;
    } else {
      ans += store[i];
      lst = store[i];
    }
  }
  cout << ans << endl;
}
int main(int argc, const char* argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int tests = 1;
  cin >> tests;
  while (tests--) {
    solve();
  }
  return 0;
}
