#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int cnt[N], good[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 0; i <= n; i++) cnt[i] = good[i] = 0;
    for (int i = 0; i < n; i++) {
      int x, t;
      cin >> x >> t;
      cnt[x]++;
      if (t) good[x]++;
    }
    vector<pair<int, int>> types;
    for (int i = 1; i <= n; i++)
      if (cnt[i]) types.push_back({cnt[i], good[i]});
    sort(types.begin(), types.end());
    priority_queue<int> pq;
    int ans = 0, ans2 = 0;
    for (int i = n; i >= 0; i--) {
      while (types.size() && types.back().first >= i) {
        pq.push(types.back().second);
        types.pop_back();
      }
      if (pq.empty()) continue;
      ans += i;
      ans2 += min(i, pq.top());
      pq.pop();
    }
    cout << ans << " " << ans2 << "\n";
  }
}
