#include <bits/stdc++.h>
using namespace std;
struct node {
  long long m, p, sum;
} line[200005];
long long dui[200005];
int l = 0;
bool cmp(node a, node b) { return a.m < b.m; }
void push(int a) {
  l++;
  dui[l] = a;
  for (int i = l; i > 1; i /= 2) {
    if (dui[i] < dui[i / 2]) {
      swap(dui[i], dui[i / 2]);
    } else
      break;
  }
}
void pop() {
  dui[1] = dui[l];
  dui[l] = 10000000000000ll;
  l--;
  int i = 1;
  while (dui[i] > dui[i * 2] || dui[i] > dui[i * 2 + 1]) {
    if (dui[i] > dui[i * 2] && dui[i * 2 + 1] >= dui[i * 2]) {
      swap(dui[i], dui[i * 2]);
      i = i * 2;
    } else {
      swap(dui[i], dui[i * 2 + 1]);
      i = i * 2 + 1;
    }
  }
}
int main() {
  long long t, count, add, ans;
  long long n;
  cin >> t;
  while (t--) {
    cin >> n;
    memset(dui, 0x7f, sizeof(dui));
    for (int i = 1; i <= n; i++) {
      cin >> line[i].m >> line[i].p;
      line[i].m++;
    }
    line[0].m = 0;
    line[0].p = 0;
    line[0].sum = 1;
    sort(line + 1, line + 1 + n, cmp);
    for (int i = 1; i <= n; i++) {
      if (line[i].m - line[i - 1].m <= line[i - 1].sum) {
        line[i].m = line[i - 1].m;
        line[i].sum = line[i - 1].sum + 1;
      } else
        line[i].sum = 0;
    }
    add = 0;
    ans = 0;
    for (int i = n; line[i].m > 0; i--) {
      if (line[i].m >= n + 1) {
        ans += line[i].p;
        add++;
        continue;
      }
      count = line[i].sum;
      while (line[i].sum > 0) {
        push(line[i].p);
        i--;
      }
      push(line[i].p);
      while (line[i - 1].m + add + line[i - 1].sum < line[i].m) {
        ans += dui[1];
        pop();
        add++;
      }
    }
    while (l >= 1) dui[l--] = 10000000000000ll;
    cout << ans << endl;
  }
  return 0;
}
