#include <bits/stdc++.h>
using namespace std;
const int maxm = 2e5 + 10;
int cnt0[maxm], cnt1[maxm], a[maxm];
priority_queue<int> q;
bool cmp(int a, int b) { return cnt0[a] + cnt1[a] > cnt0[b] + cnt1[b]; }
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    while (!q.empty()) q.pop();
    int n, x, y;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d %d", &x, &y);
      if (y == 0)
        cnt0[x]++;
      else
        cnt1[x]++;
      a[i] = i;
    }
    sort(a + 1, a + n + 1, cmp);
    int ans = 0, num = 0, now = 1;
    for (int i = n; i >= 1; i--) {
      while (cnt0[a[now]] + cnt1[a[now]] >= i) {
        q.push(cnt1[a[now]]);
        now++;
      }
      if (q.empty()) continue;
      int dd = q.top();
      q.pop();
      ans += i;
      num += min(i, dd);
    }
    printf("%d %d\n", ans, num);
    for (int i = 1; i <= n; i++) cnt0[i] = cnt1[i] = 0;
  }
  return 0;
}
