#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 7;
int test, n, s[MAXN];
vector<int> val[MAXN];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> test;
  while (test--) {
    cin >> n;
    for (int i = 0; i < n; ++i) val[i].clear();
    for (int i = 1; i <= n; ++i) {
      int x, y;
      cin >> x >> y;
      val[x].push_back(y);
    }
    s[0] = 0;
    for (int i = 1; i < n; ++i) s[i] = s[i - 1] + (int)val[i - 1].size();
    multiset<int> ms;
    long long res = 0;
    int cur = 1;
    for (int i = n - 1; i >= 1; --i) {
      for (auto v : val[i]) ms.insert(v);
      int dist = i - s[i];
      for (; cur <= dist; ++cur)
        res += 1LL * (*ms.begin()), ms.erase(ms.begin());
    }
    cout << res << '\n';
  }
  return 0;
}
