#include <bits/stdc++.h>
using namespace std;
int read() {
  char ch = 0;
  int w = 0, ff = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    w = w * 10 + ch - '0';
    ch = getchar();
  }
  return ff * w;
}
int val[200005], val2[200005];
int sta[200005], T, x, f, n;
bool cmp(int a, int b) { return val[a] + val2[a] > val[b] + val2[b]; }
signed main() {
  T = read();
  while (T--) {
    n = read();
    for (int i = 1; i <= n; ++i) {
      x = read(), f = read();
      sta[i] = i;
      f ? val[x]++ : val2[x]++;
    }
    sort(sta + 1, sta + n + 1, cmp);
    priority_queue<int, vector<int>, less<int> > que;
    int now = 0, ans = 0, sum = 0;
    for (int i = n; i >= 1; --i) {
      while (val[sta[now + 1]] + val2[sta[now + 1]] >= i)
        now++, que.push(val[sta[now]]);
      if (que.empty()) continue;
      int p = que.top();
      que.pop();
      sum += i, ans += (i < p ? i : p);
    }
    printf("%d %d\n", sum, ans);
    for (int i = 1; i <= n; ++i) val[i] = val2[i] = 0;
  }
  return 0;
}
