#include <bits/stdc++.h>
using namespace std;
struct ii {
  long long first, second, id;
  ii(){};
  ii(long long first, long long second, long long id)
      : first(first), second(second), id(id){};
  bool operator<(const ii &a) const {
    if (first != a.first) return first < a.first;
    if (second != a.second) return second < a.second;
    return id < a.id;
  }
};
const long long N = 2e5 + 4, oo = 1e15;
long long T, n, Time, mark[N];
ii a[N];
vector<ii> V;
long long Ipos[N];
bool check(long long mid) {
  V.clear();
  for (long long i = mid + 1; i <= (long long)n; ++i) V.push_back(a[i]);
  sort(V.begin(), V.end());
  long long cur = mid;
  for (ii x : V) {
    if (cur < x.first) return false;
    ++cur;
  }
  return true;
}
struct Interval_Tree {
  long long node[4 * N], lazy[4 * N], CC[4 * N];
  void Down(long long i) {
    node[2 * i] += lazy[i];
    node[2 * i + 1] += lazy[i];
    lazy[2 * i] += lazy[i];
    lazy[2 * i + 1] += lazy[i];
    lazy[i] = 0;
  }
  void update(long long i, long long l, long long r, long long x, long long y,
              long long val, long long cc) {
    if (l > y || x > r) return;
    if (x <= l && r <= y) {
      node[i] += val;
      lazy[i] += val;
      if (cc != -1) CC[i] = cc;
      return;
    }
    Down(i);
    long long mid = (l + r) / 2;
    update(2 * i, l, mid, x, y, val, cc);
    update(2 * i + 1, mid + 1, r, x, y, val, cc);
    node[i] = min(node[2 * i], node[2 * i + 1]);
    CC[i] = CC[2 * i] + CC[2 * i + 1];
  }
  long long get(long long i, long long l, long long r, long long x,
                long long y) {
    if (l > y || x > r) return oo;
    if (x <= l && r <= y) return node[i];
    Down(i);
    long long mid = (l + r) / 2;
    long long tmp1 = get(2 * i, l, mid, x, y),
              tmp2 = get(2 * i + 1, mid + 1, r, x, y);
    return min(tmp1, tmp2);
  }
  long long getCC(long long i, long long l, long long r, long long x,
                  long long y) {
    if (l > y || x > r) return 0;
    if (x <= l && r <= y) return CC[i];
    long long mid = (l + r) / 2;
    long long tmp1 = getCC(2 * i, l, mid, x, y),
              tmp2 = getCC(2 * i + 1, mid + 1, r, x, y);
    return tmp1 + tmp2;
  }
  void Reset(long long i, long long l, long long r) {
    node[i] = lazy[i] = CC[i] = 0;
    if (l == r) return;
    long long mid = (l + r) / 2;
    Reset(2 * i, l, mid);
    Reset(2 * i + 1, mid + 1, r);
  }
} IT;
void sol() {
  sort(a + 1, a + n + 1, [](ii u, ii v) { return u.second < v.second; });
  long long l = 0, r = n, take = -1;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (check(mid)) {
      take = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  long long res = 0;
  V.clear();
  ++Time;
  for (long long i = 1; i <= (long long)take; ++i)
    res += a[i].second, V.push_back(a[i]), mark[a[i].id] = Time;
  reverse(V.begin(), V.end());
  sort(a + 1, a + n + 1);
  long long cnt = 0;
  for (long long i = 1; i <= (long long)n; ++i) {
    if (mark[a[i].id] == Time) {
      IT.update(1, 1, n, i, i, 2 * oo, 0);
    } else {
      IT.update(1, 1, n, i, i, cnt + take - a[i].first, 1);
      Ipos[a[i].id] = i;
      ++cnt;
    }
    Ipos[a[i].id] = i;
  }
  long long cur = take;
  for (ii x : V) {
    long long second = Ipos[x.id];
    IT.update(1, 1, n, 1, second - 1, -1, -1);
    long long minVal = IT.node[1];
    long long cc = IT.getCC(1, 1, n, 1, second) + cur - 1;
    if (minVal >= 0 && cc >= x.first) {
      res -= x.second;
      --cur;
      IT.update(1, 1, n, second, second, cc - x.first, 1);
    } else
      IT.update(1, 1, n, 1, second - 1, 1, -1);
  }
  if (T <= 11) cout << res << '\n';
  IT.Reset(1, 1, n);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  cin >> T;
  for (long long idT = 1; idT <= (long long)T; ++idT) {
    cin >> n;
    for (long long i = 1; i <= (long long)n; ++i) {
      cin >> a[i].first >> a[i].second;
      a[i].id = i;
    }
    if (idT == 201) {
      cout << n << '\n';
      for (long long i = 1; i <= (long long)n; ++i)
        cout << a[i].first << " " << a[i].second << '\n';
    }
    sol();
  }
  return 0;
}
