#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;
  map<long long int, long long int> make_pair;
  for (long long int i = 0; i <= n - 1; i++) {
    long long int t;
    cin >> t;
    make_pair[t]++;
  }
  map<long long int, long long int> m;
  for (auto it = make_pair.begin(); it != make_pair.end(); it++) {
    m[it->second]++;
  }
  long long int ans = 0;
  vector<bool> visited(1e6, false);
  for (auto x : m) {
    long long int num = x.first;
    long long int times = x.second;
    for (long long int i = num; i > 0; i--) {
      if (visited[i] == false && times > 0) {
        ans += i;
        times--;
        visited[i] = true;
      }
      if (times == 0) break;
    }
  }
  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;
}
