#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > VVI;
typedef priority_queue<int, vector<int>, greater<int> > PQ;
bool ok(vector<int> V, int k, int x) {
  for (int y : V) {
    int df = abs(x - y);
    if (df > k) return 0;
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> V, V2;
    vector<pair<int, int> > Ar;
    unordered_map<int, int> Map, Map2, Map3;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      V.push_back(x);
      Map[x]++;
    }
    for (int i = 0; i < n; i++) {
      if (Map3[V[i]] == 0) {
        Map3[V[i]] = 1;
        Ar.push_back({Map[V[i]], V[i]});
      }
    }
    sort(Ar.rbegin(), Ar.rend());
    int last = 1e9;
    for (int i = 0; i < Ar.size(); i++) {
      int x = Ar[i].second;
      int f = Map[Ar[i].second];
      if (!Map2[f]) {
        V2.push_back(f);
        Map2[f] = 1;
        last = f;
      } else {
        if (last > 1) {
          last--;
          V2.push_back(last);
          Map2[last] = 1;
        }
      }
    }
    int ans = accumulate(V2.begin(), V2.end(), 0);
    cout << ans << "\n";
  }
  return 0;
}
