#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 9;
vector<multiset<int>> v(N);
int n, cnt[N], can[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int ans = 0, ans1 = 0;
    cin >> n;
    for (int i = 0; i <= n; i++) {
      v[i].clear();
      cnt[i] = 0;
      can[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      cnt[x]++;
      if (y == 1) {
        can[x]++;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (cnt[i] == 0) continue;
      v[cnt[i]].insert(can[i]);
    }
    for (int i = n; i >= 1; i--) {
      if (v[i].size() == 0) continue;
      ans += i;
      ans1 += min(i, *v[i].rbegin());
      v[i].erase(v[i].find(*v[i].rbegin()));
      for (auto x : v[i]) {
        v[i - 1].insert(x);
      }
    }
    cout << ans << " " << ans1 << '\n';
  }
}
