#include <bits/stdc++.h>
const int N = 300010;
const long long INF = 1ll << 60;
const int inf = 0x3f3f3f3f;
int t;
int n, m;
int a[N];
int b[N];
std::multiset<int> se[N];
std::multiset<int> se2;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
      se[i].clear();
    }
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", &a[i], &b[i]);
      se[a[i]].insert(b[i]);
    }
    int pre = 0;
    int id_st = 0;
    int num = 0;
    for (int i = 0; i < n; ++i) {
      pre += se[i].size();
      if (pre <= i) {
        id_st = i + 1;
        if (!se[i].size()) {
          ++num;
        }
      }
    }
    printf("%d??\n", num);
    if (!id_st) {
      printf("0\n");
    } else {
      se2.clear();
      for (int i = id_st; i < n; ++i) {
        for (auto& x : se[i]) {
          se2.insert(x);
        }
      }
      long long ans = 0ll;
      while (num) {
        --num;
        ans += *se2.begin();
        se2.erase(se2.begin());
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}
