#include <bits/stdc++.h>
using namespace std;
long long a[300010];
long long cmp(long long a, long long b) { return a > b; }
int main() {
  long long n;
  cin >> n;
  while (n--) {
    int q;
    cin >> q;
    for (int i = 0; i <= q; i++) a[i] = 0;
    long long b;
    long long maxx = -999;
    for (int i = 0; i < q; i++) {
      cin >> b;
      maxx = max(maxx, b);
      a[b]++;
    }
    sort(a + 1, a + maxx + 1, cmp);
    long long sum = 0;
    long long ans = a[1];
    for (int i = 1; i <= maxx; i++) {
      if (a[i] >= ans) {
        sum = sum + ans;
        ans--;
      }
      if (a[i] < ans) {
        ans = a[i];
        sum = sum + ans;
        ans--;
      }
      if (a[i] < 0) break;
      if (ans < 0) break;
    }
    cout << sum << endl;
  }
  return 0;
}
