#include <bits/stdc++.h>
using namespace std;
long long int dp[300005][3];
long long int a[300005];
long long int w[300005];
int main() {
  std::ios::sync_with_stdio(false);
  long long int q;
  cin >> q;
  while (q--) {
    long long int n;
    cin >> n;
    for (long long int i = 1; i <= n; i++) {
      cin >> a[i] >> w[i];
    }
    a[0] = -1e18;
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 0; j <= 2; j++) {
        dp[i][j] = 1e18;
      }
    }
    for (long long int i = 1; i <= n; i++) {
      if (abs(a[i] - a[i - 1]) > 2) {
        dp[i][0] = min(dp[i - 1][0], min(dp[i - 1][1], (dp[i - 1][2])));
        dp[i][1] = min(dp[i - 1][0], min(dp[i - 1][1], (dp[i - 1][2]))) + w[i];
        dp[i][2] =
            min(dp[i - 1][0], min(dp[i - 1][1], (dp[i - 1][2]))) + 2 * w[i];
      }
      if ((a[i] - a[i - 1]) == 2) {
        dp[i][0] = min(dp[i - 1][0], (dp[i - 1][1]));
        dp[i][1] = min(dp[i - 1][0], min(dp[i - 1][1], (dp[i - 1][2]))) + w[i];
        dp[i][2] =
            min(dp[i - 1][0], min(dp[i - 1][1], (dp[i - 1][2]))) + 2 * w[i];
      }
      if ((a[i] - a[i - 1]) == 1) {
        dp[i][0] = min(dp[i - 1][0], (dp[i - 1][2]));
        dp[i][1] = min(dp[i - 1][0], (dp[i - 1][1])) + w[i];
        dp[i][2] =
            min(dp[i - 1][0], min(dp[i - 1][1], (dp[i - 1][2]))) + 2 * w[i];
      }
      if ((a[i] - a[i - 1]) == 0) {
        dp[i][0] = min(dp[i - 1][1], (dp[i - 1][2]));
        dp[i][1] = min(dp[i - 1][0], (dp[i - 1][2])) + w[i];
        dp[i][2] = min(dp[i - 1][0], (dp[i - 1][1])) + 2 * w[i];
      }
      if ((a[i] - a[i - 1]) == -1) {
        dp[i][0] = min(dp[i - 1][0], min(dp[i - 1][1], (dp[i - 1][2])));
        dp[i][1] = min(dp[i - 1][1], (dp[i - 1][2])) + w[i];
        dp[i][2] = min(dp[i - 1][0], (dp[i - 1][2])) + 2 * w[i];
      }
      if ((a[i] - a[i - 1]) == -2) {
        dp[i][0] = min(dp[i - 1][0], min(dp[i - 1][1], (dp[i - 1][2])));
        dp[i][1] = min(dp[i - 1][0], min(dp[i - 1][1], (dp[i - 1][2]))) + w[i];
        dp[i][2] = min(dp[i - 1][1], (dp[i - 1][2])) + 2 * w[i];
      }
    }
    long long int ans = min(dp[n][0], min(dp[n][1], dp[n][2]));
    cout << ans << endl;
  }
  return 0;
}
