#include <bits/stdc++.h>
using namespace std;
const int mxN = 3e5;
long long dp[mxN + 1][3];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> a(n + 1), b(n + 1);
    for (long long i = 1; i <= n; ++i) cin >> a[i] >> b[i];
    for (long long i = 1; i <= n; ++i)
      for (long long j = 0; j < 3; ++j) dp[i][j] = 1e18;
    for (long long i = 1; i <= n; ++i)
      for (long long j = 0; j < 3; ++j)
        for (long long k = 0; k < 3; ++k)
          if (a[i] + j ^ a[i - 1] + k)
            dp[i][j] = min(dp[i][j], dp[i - 1][k] + j * b[i]);
    long long ans = min({dp[n][0], dp[n][1], dp[n][2]});
    cout << ans << "\n";
  }
}
