#include <bits/stdc++.h>
using namespace std;
long long dp[300005][3];
long long a[300005], v[300005];
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      cin >> a[i] >> v[i];
    }
    dp[1][0] = 0;
    dp[1][1] = v[1];
    dp[1][2] = 2 * v[1];
    for (int i = 2; i <= n; i++) {
      if (a[i] == a[i - 1]) {
        dp[i][0] = min(dp[i - 1][1], dp[i - 1][2]);
        dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + v[i];
        dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + 2 * v[i];
        continue;
      }
      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]) + v[i];
        dp[i][2] =
            min(min(dp[i - 1][0], dp[i - 1][1]), dp[i - 1][2]) + 2 * v[i];
        continue;
      }
      if (a[i] - a[i - 1] == 2) {
        dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]);
        dp[i][1] = min(min(dp[i - 1][0], dp[i - 1][2]), dp[i - 1][1]) + v[i];
        dp[i][2] =
            min(min(dp[i - 1][0], dp[i - 1][2]), dp[i - 1][1]) + 2 * v[i];
        continue;
      }
      if (a[i - 1] - a[i] == 1) {
        dp[i][0] = min(min(dp[i - 1][0], dp[i - 1][2]), dp[i - 1][1]);
        dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) + v[i];
        dp[i][2] = min(dp[i - 1][0], dp[i - 1][2]) + 2 * v[i];
        continue;
      }
      if (a[i - 1] - a[i] == 2) {
        dp[i][0] = min(min(dp[i - 1][0], dp[i - 1][2]), dp[i - 1][1]);
        dp[i][1] = min(min(dp[i - 1][0], dp[i - 1][2]), dp[i - 1][1]) + v[i];
        dp[i][2] = min(dp[i - 1][1], dp[i - 1][2]) + 2 * v[i];
        continue;
      }
      dp[i][0] = min(min(dp[i - 1][0], dp[i - 1][2]), dp[i - 1][1]);
      dp[i][1] = min(min(dp[i - 1][0], dp[i - 1][2]), dp[i - 1][1]) + v[i];
      dp[i][2] = min(min(dp[i - 1][0], dp[i - 1][2]), dp[i - 1][1]) + 2 * v[i];
    }
    long long ans = min(min(dp[n][0], dp[n][2]), dp[n][1]);
    cout << ans << endl;
  }
  return 0;
}
