#include <bits/stdc++.h>
using namespace std;
long long hcf(long long a, long long b) {
  if (b == 0) return a;
  return hcf(b, a % b);
}
bool compare(pair<int, int> p, pair<int, int> q) {
  if (p.first == q.first)
    return p.second > q.second;
  else
    return p.first > q.first;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int>> count(n + 1, {0, 0});
    for (int i = 0; i < n; i++) {
      int index, keep;
      cin >> index >> keep;
      count[index].first++;
      count[index].second += keep;
    }
    sort(count.begin(), count.end(), compare);
    long long ans = 0;
    long long kitne = 0;
    int till = n;
    for (int i = 0; i < n; i++) {
      if (count[i].first == 0) break;
      sort(count.begin() + i, count.begin() + till, compare);
      ans += count[i].first;
      kitne += min(count[i].first, count[i].second);
      int j = i + 1;
      while (j < n && count[j].first == count[i].first) {
        count[j].first--;
        j++;
      }
      while (j < n && count[j].first + 1 == count[i].first) j++;
      till = j;
    }
    cout << ans << " " << kitne << endl;
  }
  return 0;
}
