#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int test_cases;
  cin >> test_cases;
  while (test_cases--) {
    long long int n;
    cin >> n;
    vector<long long int> v(n);
    for (long long int i = 0; i < n; i++) cin >> v[i];
    map<long long int, long long int> m;
    for (long long int i = 0; i <= n - 1; i++) {
      m[v[i]]++;
    }
    vector<pair<long long int, long long int> > vvv;
    for (auto i : m) {
      vvv.push_back({i.second, i.first});
    }
    sort(vvv.begin(), vvv.end());
    long long int index = vvv.size() - 1;
    pair<long long int, long long int> p = vvv[index];
    long long int cur = p.first;
    long long int ans = 0;
    while (cur > 0) {
      ans += cur;
      index--;
      if (index >= 0) {
        pair<long long int, long long int> q = vvv[index];
        cur = min(cur - 1, q.first);
      } else
        break;
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
