#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int N = 1e5 + 7, M = 1e7, OO = 0x3f3f3f3f;
bool cmp(int a, int b) { return a > b; }
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, num, i, counter = 0;
    scanf("%d", &n);
    vector<int> freq(n + 10, 0);
    for (i = 0; i < n; ++i) {
      scanf("%d", &num);
      freq[num]++;
    }
    sort(freq.begin(), freq.end(), cmp);
    int mini = freq[0];
    counter += freq[0];
    for (i = 1; i < freq.size(); ++i) {
      mini--;
      mini = min(mini, freq[i]);
      counter += mini;
      if (mini <= 1) {
        break;
      }
    }
    printf("%d\n", counter);
  }
  return 0;
}
