#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int cost[N], people[N];
bool done[N];
vector<int> ids[N];
int a[N];
int tr[4 * N];
void build(int u, int st, int en) {
  if (st == en) {
    tr[u] = st;
  } else {
    int mid = (st + en) / 2;
    build(2 * u, st, mid);
    build(2 * u + 1, mid + 1, en);
    tr[u] = max(tr[2 * u], tr[2 * u + 1]);
  }
}
void update(int u, int st, int en, int idx, int x) {
  if (st == en)
    tr[u] = x;
  else {
    int mid = (st + en) / 2;
    if (idx <= mid)
      update(2 * u, st, mid, idx, x);
    else
      update(2 * u + 1, mid + 1, en, idx, x);
    tr[u] = max(tr[2 * u], tr[2 * u + 1]);
  }
}
int query(int u, int st, int en, int l, int r) {
  if (r < st || en < l)
    return 0;
  else if (l <= st && en <= r)
    return tr[u];
  else {
    int mid = (st + en) / 2;
    return max(query(2 * u, st, mid, l, r),
               query(2 * u + 1, mid + 1, en, l, r));
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long sum = 0;
    vector<pair<int, int>> v;
    for (int i = 1; i <= n; i++) {
      cin >> people[i] >> cost[i];
      sum += cost[i];
      v.emplace_back(cost[i], i);
    }
    sort(v.rbegin(), v.rend());
    build(1, 1, n);
    for (auto pr : v) {
      int u = pr.second;
      int v = query(1, 1, n, 1, n - people[u]);
      if (v == 0) continue;
      update(1, 1, n, v, 0);
      sum -= cost[u];
    }
    cout << sum << "\n";
  }
}
