#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    map<long long, long long> mp, mpp;
    set<int> s;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      mp[a[i]]++;
      s.insert(a[i]);
    }
    priority_queue<long long> pq;
    for (int i : s) {
      pq.push(mp[i]);
    }
    long long ans = 0;
    while (!pq.empty()) {
      long long top = pq.top();
      pq.pop();
      while (mpp[top] && top >= 1) {
        top--;
      }
      if (!mpp[top]) {
        ans += top;
        mpp[top] = 1;
      }
    }
    cout << ans << "\n";
  }
}
