#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    map<int, int> mp;
    vector<int> v;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      mp[x]++;
    }
    for (auto it : mp) {
      int e = it.second;
      v.push_back(e);
    }
    sort(v.rbegin(), v.rend());
    int sum = v[0];
    int flag = v[0] - 1;
    if (flag >= 1) {
      for (int i = 1; i < v.size(); i++) {
        if (v[i] >= flag) {
          sum += (flag);
          flag--;
          if (flag == 0) {
            break;
          }
        } else {
          sum += v[i];
          flag = v[i] - 1;
          if (flag == 0) {
            break;
          }
        }
      }
    }
    cout << sum << endl;
  }
}
