#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
mt19937 rnd(239);
const int N = 300002;
long long a[N], b[N];
void solve() {
  int n;
  cin >> n;
  long long sm = 0;
  for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
  a[n] = a[0], b[n] = b[0];
  long long x = 10000000000000000ll;
  for (int i = 1; i <= n; ++i) {
    sm += max(0ll, a[i] - b[i - 1]);
    x = min(x, min(a[i], b[i - 1]));
  }
  cout << sm + x << "\n";
}
void pre_calc() {}
int main() {
  pre_calc();
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tst = 10;
  if (tst != 1) cin >> tst;
  while (tst--) solve();
}
