#include <bits/stdc++.h>
using namespace std;
long long bigmod(long long b, long long p, long long md) {
  if (p == 0) return 1;
  if (p % 2 == 1) {
    return ((b % md) * bigmod(b, p - 1, md)) % md;
  } else {
    long long y = bigmod(b, p / 2, md);
    return (y * y) % md;
  }
}
int cnt[200005];
int f[200005];
vector<int> v[200005];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    int mx = 0;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      if (b) f[a]++;
      cnt[a]++;
      mx = max(mx, cnt[a]);
    }
    for (int i = 1; i <= n; i++) {
      v[cnt[i]].push_back(f[i]);
    }
    long long ans1 = 0;
    long long ans2 = 0;
    priority_queue<int> pq;
    for (int i = mx; i >= 1; i--) {
      for (auto c : v[i]) {
        pq.push(c);
      }
      if (pq.size()) {
        ans1 += i;
        ans2 += min(i, pq.top());
        pq.pop();
      }
    }
    cout << ans1 << " " << ans2 << endl;
    ;
    for (int i = 1; i <= mx; i++) v[i].clear();
    for (int i = 1; i <= n; i++) {
      cnt[i] = 0;
      f[i] = 0;
    }
  }
  return 0;
}
