#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<long long> a(n), b(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    vector<long long> upcost(n), downcost(n, -1);
    long long ret = 0;
    upcost[0] = b[0];
    for (int i = 1; i < n; i++) {
      if (a[i] == a[i - 1]) {
        long long c1 = upcost[i - 1], c2 = b[i];
        if (c1 > c2) swap(c1, c2);
        if (downcost[i - 1] >= 0) {
          c2 = min(c2, downcost[i - 1]);
          if (c1 > c2) swap(c1, c2);
        }
        if (c1 == b[i]) {
          a[i]++;
          ret += b[i];
          downcost[i] = c2 - b[i];
          upcost[i] = b[i];
        } else {
          ret += c1;
          upcost[i] = b[i] - c1;
        }
      } else {
        if (a[i] == a[i - 1] - 1) {
          upcost[i] = min(2 * b[i], upcost[i - 1] + b[i]);
          if (downcost[i - 1] >= 0) {
            upcost[i] = min(upcost[i], b[i] + downcost[i - 1]);
          }
        } else {
          upcost[i] = b[i];
        }
      }
    }
    cout << ret << endl;
  }
}
