#include <bits/stdc++.h>
const int N = (int)(1 * 1e6 + 10);
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    long long int a[n], b[n], mn = 1e18, ans = 0;
    for (long long int i = 0; i < n; i++) cin >> a[i] >> b[i];
    for (long long int i = 0; i < n; i++) {
      long long int j = (i + 1) % n;
      long long int x = min(a[j], b[i]);
      ans += a[j] - x;
      mn = min(mn, x);
    }
    cout << ans + mn << endl;
  }
}
