#include <bits/stdc++.h>
using namespace std;
using Z = int64_t;
const Z INF = (Z)1e18 + 5;
const Z maxn = (Z)3e5 + 1;
Z d0[maxn], d1[maxn], d2[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<Z> a(n), b(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i] >> b[i];
    }
    d0[0] = 0;
    d1[0] = b[0];
    d2[0] = 2 * b[0];
    for (int i = 1; i < n; ++i) {
      Z ans0 = INF, ans1 = INF, ans2 = INF;
      if (a[i] != a[i - 1]) {
        ans0 = min(d0[i - 1], ans0);
        ans1 = min(d1[i - 1] + b[i], ans1);
        ans2 = min(d2[i - 1] + 2 * b[i], ans2);
      }
      if (a[i] != a[i - 1] + 1) {
        ans0 = min(d1[i - 1], ans0);
        ans1 = min(d2[i - 1] + b[i], ans1);
      }
      if (a[i] != a[i - 1] + 2) {
        ans0 = min(d2[i - 1], ans0);
      }
      if (a[i] != a[i - 1] - 1) {
        ans1 = min(d0[i - 1] + b[i], ans1);
        ans2 = min(d1[i - 1] + 2 * b[i], ans2);
      }
      if (a[i] != a[i - 1] - 2) {
        ans2 = min(d0[i - 1] + 2 * b[i], ans2);
      }
      d0[i] = ans0;
      d1[i] = ans1;
      d2[i] = ans2;
    }
    Z res = min(d0[n - 1], min(d1[n - 1], d2[n - 1]));
    cout << res << endl;
  }
}
