#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const double PI = acos(-1.0);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n], b[n];
    long long mi = 1e18;
    for (long long i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      if (a[(i + 1) % n] > b[i]) {
        ans += a[(i + 1) % n] - b[i];
      }
      a[(i + 1) % n] = min(b[i], a[(i + 1) % n]);
      mi = min(mi, a[(i + 1) % n]);
    }
    cout << ans + mi << "\n";
  }
}
