#include <bits/stdc++.h>
using namespace std;
int buff1, buff2;
void solution(vector<int> v) {
  map<int, int> counter;
  for (int n : v) counter[n]++;
  vector<int> counts;
  for (auto it : counter) {
    counts.push_back(it.second);
  }
  sort(counts.begin(), counts.end(), greater<int>());
  int cur = INT_MAX, ret = 0;
  for (int n : counts) {
    if (cur <= 1) {
      break;
    }
    cur = min(cur - 1, n);
    ret += cur;
  }
  cout << ret << endl;
}
int main() {
  int nTest;
  cin >> nTest;
  for (int i = 0; i < nTest; i++) {
    int N;
    cin >> N;
    vector<int> v;
    for (int i = 0; i < N; i++) {
      cin >> buff1;
      v.push_back(buff1);
    }
    solution(v);
  }
  return 0;
}
