#include <bits/stdc++.h>
using namespace std;
inline int bit(int x, int i) { return (x >> i) & 1; }
void CountTriangles(vector<int> A) {
  int n = A.size();
  sort(A.begin(), A.end());
  int count = 0;
  for (int i = n - 1; i >= 1; i--) {
    int l = 0, r = i - 1;
    while (l < r) {
      if (A[l] + A[r] > A[i]) {
        count += r - l;
        r--;
      } else
        l++;
    }
  }
  cout << "No of possible solutions: " << count;
}
void dfs(int i, vector<vector<int>> gr, vector<bool> &vis,
         vector<int> &output) {
  vis[i] = true;
  for (int nodes : gr[i]) {
    if (!vis[nodes]) {
      dfs(nodes, gr, vis, output);
    }
  }
  output.push_back(i);
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    long long int n;
    cin >> n;
    map<int, int> v;
    for (long long int i = 0; i < n; i++) {
      int val;
      cin >> val;
      v[val]++;
    }
    vector<int> s;
    for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) {
      s.push_back(it->second);
    }
    sort(s.rbegin(), s.rend());
    int sum = s[0], top = s[0];
    for (long long int i = 1; i < s.size(); i++) {
      if (s[i] >= top) {
        if (top > 1) {
          top -= 1;
          sum += top;
        }
      } else {
        sum += s[i];
        top = s[i];
      }
    }
    cout << sum << endl;
  }
  return 0;
}
