#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  vector<int> freq(200000 + 1);
  while (t--) {
    int n;
    cin >> n;
    vector<int> v(n);
    set<int> s;
    for (int i = 0; i < n; i++) {
      cin >> v[i];
      freq[v[i]]++;
      s.insert(v[i]);
    }
    vector<int> g;
    for (auto i : s) {
      g.push_back(freq[i]);
    }
    sort(g.begin(), g.end());
    long long last_taken = 1e9, total = 0;
    for (int i = g.size() - 1; i >= 0; i--) {
      if (g[i] < last_taken) {
        total += g[i];
        last_taken = g[i];
      } else {
        total += (last_taken - 1);
        last_taken--;
      }
      if (last_taken == 1) break;
    }
    cout << total << "\n";
    for (auto i : s) freq[i] = 0;
  }
}
