#include <bits/stdc++.h>
using namespace std;
int n;
int cnt0[200010], cnt1[200010], cnt[200010];
int id[200010];
priority_queue<int> q;
int cmp(int a, int b) { return cnt[a] > cnt[b]; }
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
      cnt[i] = cnt0[i] = cnt1[i] = 0;
    }
    while (!q.empty()) q.pop();
    for (int i = 1, a, b; i <= n; ++i) {
      scanf("%d %d", &a, &b);
      if (b == 0) {
        cnt0[a]++;
      } else {
        cnt1[a]++;
      }
      cnt[a]++;
      id[i] = i;
    }
    sort(id + 1, id + 1 + n, cmp);
    int ans1 = 0, ans2 = 0;
    int pos = 1;
    for (int i = n; i >= 1; --i) {
      while (pos <= n && cnt[id[pos]] >= i) {
        q.push(cnt1[id[pos]]);
        pos++;
      }
      if (q.empty()) continue;
      ans1 += i;
      ans2 += min(i, q.top());
      q.pop();
    }
    printf("%d %d\n", ans1, ans2);
  }
  return 0;
}
