#include <bits/stdc++.h>
using namespace std;
void solve();
void YN(bool b) {
  if (b) {
    cout << "YES"
         << "\n";
    ;
  } else {
    cout << "NO"
         << "\n";
    ;
  }
};
void yn(bool b) {
  if (b) {
    cout << "Yes"
         << "\n";
    ;
  } else {
    cout << "No"
         << "\n";
    ;
  }
};
void queries() {
  int n;
  cin >> n;
  while (n--) solve();
}
const int maxn = 200200;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
  }
  for (auto i : m) {
    b.push_back(i.second);
  }
  map<int, bool> taken;
  long long sum = 0;
  sort(b.begin(), b.end());
  for (int i = b.size() - 1; i >= 0; i--) {
    if (!taken[b[i]]) {
      sum += b[i];
      taken[b[i]] = 1;
    } else {
      for (int j = b[i]; j > 0; j--) {
        if (!taken[j]) {
          sum += j;
          taken[j] = 1;
          break;
        }
      }
    }
  }
  cout << sum << "\n";
  ;
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  queries();
  ;
  return 0;
}
