#include <bits/stdc++.h>
using namespace std;
const int mod2 = 1e9 + 7;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (long long i = 0; i < n; i++) cin >> a[i];
    map<int, int> m;
    for (long long i = 0; i < n; i++) ++m[a[i]];
    vector<int> v;
    for (auto it = m.begin(); it != m.end(); ++it) v.push_back(it->second);
    sort(v.begin(), v.end(), greater<int>());
    int count = v[0];
    int i = 1;
    int ans = count;
    int x = count;
    while (--count && i < v.size()) {
      x = (v[i] > count) ? count : v[i];
      count = x;
      ans += x;
      ++i;
    }
    cout << ans << '\n';
  }
}
