#include <bits/stdc++.h>
const int N = 3e5 + 10;
const int mod = 1e5 + 7;
using namespace std;
int a[N], b[N];
pair<long long, int> dp[N][2];
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i], &b[i]);
  dp[1][0] = {0, a[1]};
  dp[1][1] = {b[1], a[1] + 1};
  for (int i = 2; i <= n; ++i) {
    pair<long long, int> p[4];
    p[0] = {b[i] * (a[i] == dp[i - 1][0].second) + dp[i - 1][0].first,
            a[i] + (a[i] == dp[i - 1][0].second)};
    p[1] = {p[0].first + b[i] * (1 + (p[0].second + 1 == dp[i - 1][0].second)),
            p[0].second + 1 + (p[0].second + 1 == dp[i - 1][0].second)};
    p[2] = {b[i] * (a[i] == dp[i - 1][1].second) + dp[i - 1][1].first,
            a[i] + (a[i] == dp[i - 1][1].second)};
    p[3] = {p[2].first + b[i] * (1 + (p[2].second + 1 == dp[i - 1][1].second)),
            p[2].second + 1 + (p[2].second + 1 == dp[i - 1][1].second)};
    sort(p, p + 4);
    dp[i][0] = p[0];
    for (int ii = 1; ii <= 3; ++ii)
      if (p[ii].second != p[0].second) dp[i][1] = p[ii], ii = 3;
  }
  printf("%lld\n", min(dp[n][0].first, dp[n][1].first));
}
int main() {
  int t;
  scanf("%d", &t);
  for (int i = 1; i <= t; ++i) solve();
  return 0;
}
