#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int a, b = 0, c, d;
  cin >> n;
  while (n--) {
    map<int, int> m;
    int q, ri = 0;
    cin >> q;
    for (int i = 0; i < q; i++) {
      cin >> a;
      m[a]++;
    }
    int res[m.size()];
    for (auto c : m) {
      res[ri++] = c.second;
    }
    map<int, int> s;
    int ans = 0;
    sort(res, res + ri);
    int mn = 2e8;
    for (int i = ri - 1; i >= 0; i--) {
      if (s[res[i]] == 0) {
        mn = min(mn, res[i]);
        ans += mn;
        s[mn]++;
        if (mn > 0) mn--;
      } else {
        ans += mn;
        s[mn]++;
        if (mn > 0) mn--;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
