#include <bits/stdc++.h>
using namespace std;
static bool comp(int a, int b) {
  if (a > b) return true;
  return false;
}
int main() {
  int t;
  long long int n;
  vector<long long int> nums;
  cin >> t;
  while (t--) {
    nums.clear();
    cin >> n;
    nums.resize(n);
    vector<int> v;
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
      cin >> nums[i];
      mp[nums[i]]++;
    }
    for (std::map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
      v.push_back(it->second);
    }
    sort(v.begin(), v.end(), comp);
    long long int ans = 0;
    int pre = INT_MAX;
    if (v.size() > 0) {
      ans += v[0];
      pre = v[0] - 1;
    }
    for (int i = 1; i < v.size(); i++) {
      if (pre < 0) break;
      if (v[i] >= pre) {
        ans += pre;
        pre = pre - 1;
      } else if (v[i] < pre) {
        ans += v[i];
        pre = v[i] - 1;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
