#include <bits/stdc++.h>
using namespace std;
long long q, n, a[222222], col[222222];
bool w[222222];
int main() {
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 1; i <= n; i++) col[i] = 0, w[i] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      col[a[i]]++;
    }
    vector<long long> v;
    for (int i = 1; i <= n; i++) {
      if (col[i]) v.push_back(col[i]);
    }
    sort(v.begin(), v.end());
    long long ans = 0;
    for (int i = 0; i < v.size(); i++) {
      if (w[v[i]]) {
        for (int j = v[i]; j >= 1; j--) {
          if (!w[j]) {
            w[j] = 1;
            ans += j;
            break;
          }
        }
      } else {
        ans += v[i];
        w[v[i]] = 1;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
