#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    map<int, int> mop;
    int arr[200005];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      mop[arr[i]]++;
    }
    vector<int> vec;
    for (auto e : mop) {
      vec.push_back(e.second);
    }
    sort(vec.begin(), vec.end());
    reverse(vec.begin(), vec.end());
    long long ans = vec[0];
    long long curMax = vec[0];
    for (int i = 1; i < vec.size(); i++) {
      if (vec[i] >= curMax) {
        curMax = max(curMax - 1, 0LL);
        ans += curMax;
      } else {
        ans += vec[i];
        curMax = vec[i];
      }
      if (curMax == 1) break;
    }
    cout << ans << endl;
  }
  return 0;
}
