#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> arr(n, 0);
    int in;
    for (int i = 0; i < n; i++) {
      cin >> in;
      arr[in - 1]++;
    }
    sort(arr.rbegin(), arr.rend());
    int i = 0;
    int chocs = arr[0];
    long long int ans = 0;
    while (i < n && arr[i] > 0 && chocs > 0) {
      if (arr[i] >= chocs) {
        ans += chocs;
      } else {
        chocs = arr[i];
        ans += chocs;
      }
      chocs--;
      i++;
    }
    cout << ans << endl;
  }
}
