#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, TC;
pair<int, int> cnt[N];
priority_queue<int> P;
int main() {
  if (fopen("exam.inp", "r"))
    freopen("exam.inp", "r", stdin), freopen("exam.out", "w", stdout);
  scanf("%d", &TC);
  while (TC--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      int type, x;
      scanf("%d%d", &x, &type);
      cnt[x].first++;
      cnt[x].second += type;
    }
    int ret0 = 0, ret1 = 0, suf = n, top = n;
    sort(cnt + 1, cnt + 1 + n);
    while (suf) {
      while (top && cnt[top].first >= suf) P.push(cnt[top--].second);
      if (P.size()) ret0 += suf, ret1 += min(suf, P.top()), P.pop();
      suf--;
    }
    printf("%d %d\n", ret0, ret1);
    for (int i = 1; i <= n; i++) cnt[i] = {0, 0};
    while (P.size()) P.pop();
  }
}
