#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v(n + 1, 0);
    int i, j, k, l;
    for (i = 0; i < n; i++) {
      cin >> j;
      v[j]++;
    }
    sort(v.begin(), v.end(), greater<int>());
    long long last = v[0];
    long long ans = v[0];
    for (i = 1; i <= n; i++) {
      if (v[i] == 0 || last == 1) break;
      if (v[i] < last) {
        ans += v[i];
        last = v[i];
      } else {
        ans += last - 1;
        last = last - 1;
      }
    }
    cout << ans << endl;
  }
}
