#include <bits/stdc++.h>
using namespace std;
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
long long int suma(long long int n) {
  long long int temp = n;
  long long int sum1 = 0;
  while (temp != 0) {
    sum1 += (temp % 10);
    temp = temp / 10;
  }
  return sum1;
}
int main() {
  fastio();
  long long int q;
  cin >> q;
  while (q--) {
    long long int n;
    cin >> n;
    unordered_map<long long int, long long int> mp, count;
    std::vector<long long int> arr(n);
    for (long long int i = 0; i < n; i++) {
      cin >> arr[i];
      mp[arr[i]]++;
    }
    priority_queue<long long int> pq;
    for (auto it = mp.begin(); it != mp.end(); it++) pq.push(it->second);
    long long int sum = 0;
    while (!pq.empty()) {
      if (count[pq.top()] == 0) {
        count[pq.top()]++;
        sum += pq.top();
        pq.pop();
      } else {
        long long int temp = pq.top();
        pq.pop();
        if (temp > 1) pq.push(temp - 1);
      }
    }
    cout << sum << endl;
  }
}
