#include <bits/stdc++.h>
using namespace std;
bool comp(pair<long long int, long long int> a,
          pair<long long int, long long int> b) {
  if (a.first + a.second == b.first + b.second) {
    return a.first < b.first;
  }
  return (a.first + a.second) < (b.first + b.second);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int q;
  cin >> q;
  while (q--) {
    long long int n;
    cin >> n;
    vector<pair<long long int, long long int> > A(n + 1, {0, 0});
    for (long long int i = 0; i < n; i++) {
      long long int a, first;
      cin >> a >> first;
      if (first == 0) {
        A[a].second++;
      } else {
        A[a].first++;
      }
    }
    sort(A.rbegin(), A.rend(), comp);
    pair<long long int, long long int> sol = {A[0].first + A[0].second,
                                              A[0].first};
    for (long long int i = 1; i < n + 1; i++) {
      if (A[i - 1].first + A[i - 1].second <= 1) break;
      if (A[i].first + A[i].second < A[i - 1].first + A[i - 1].second) {
        sol.first += A[i].first + A[i].second;
        sol.second += A[i].first;
      } else if (A[i].first + A[i].second >= A[i - 1].first + A[i - 1].second) {
        long long int second = A[i - 1].first + A[i - 1].second - 1;
        long long int ma = 0, mai = -1;
        for (long long int j = i; j < n + 1; j++) {
          if (second <= A[j].first + A[j].second) {
            if (A[j].first >= ma) {
              mai = j;
              ma = A[j].first;
            }
          } else {
            break;
          }
        }
        if (A[mai].first >= second) {
          A[mai].first = second;
          A[mai].second = 0;
          sol.first += second;
          sol.second += second;
        } else {
          second = second - A[mai].first;
          sol.first += A[mai].first;
          sol.second += A[mai].first;
          A[mai].second = second;
          sol.first += second;
        }
        swap(A[i], A[mai]);
      }
    }
    cout << sol.first << " " << sol.second << "\n";
  }
  return 0;
}
