#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
long long int powmod(long long int a, long long int b) {
  long long int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int nxt() {
  int x;
  scanf("%d", &x);
  return x;
}
int main() {
  int tc = nxt();
  while (tc--) {
    int n = nxt();
    map<int, int> m, c;
    for (int i = 0; i < n; i++) {
      int a = nxt(), b = nxt();
      m[a]++;
      if (b) c[a]++;
    }
    int high = 0;
    vector<pair<int, int> > order;
    for (auto x : m) {
      high = max(high, x.second);
      order.push_back({x.second, c[x.first]});
    }
    sort((order).begin(), (order).end());
    reverse((order).begin(), (order).end());
    int ptr = 0;
    int a1 = 0, a2 = 0;
    multiset<int> cnt;
    for (int height = high; height > 0; height--) {
      while (ptr < order.size() && order[ptr].first >= height) {
        cnt.insert(order[ptr].second);
        ptr++;
      }
      if (cnt.size()) {
        a1 += height;
        int tmp = *cnt.rbegin();
        a2 += min(height, tmp);
        cnt.erase(cnt.find(tmp));
      }
    }
    printf("%d %d\n", a1, a2);
  }
  return 0;
}
