#include <bits/stdc++.h>
using namespace std;
int n;
struct candeletip {
  int nrt, nrs;
};
const int N = 200005;
candeletip frv[N];
vector<int> tp[N];
priority_queue<int> nr;
void solve() {
  int x, tip;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x, &tip);
    frv[x].nrt++;
    if (tip) frv[x].nrs++;
  }
  long long solt = 0, solf = 0;
  for (int i = 1; i <= n; i++) {
    int nrt = frv[i].nrt, nrs = frv[i].nrs;
    frv[i].nrt = frv[i].nrs = 0;
    if (nrt) tp[nrt].push_back(nrs);
  }
  for (int i = n; i > 0; i--) {
    for (int j = 0; j < tp[i].size(); j++) {
      nr.push(tp[i][j]);
    }
    tp[i].clear();
    if (!nr.empty()) {
      solt += i;
      solf += min(nr.top(), i);
      nr.pop();
    }
  }
  printf("%lld %lld\n", solt, solf);
  nr = priority_queue<int>();
}
int main() {
  int nrt;
  scanf("%d", &nrt);
  while (nrt--) solve();
  return 0;
}
