#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 5;
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 ans = 0, mm = LLONG_MAX, x;
    vector<long long int> a(n), b(n);
    for (long long int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    for (long long int i = 0; i < n; i++) {
      x = max(0LL, a[i] - b[(i + n - 1) % n]);
      ans += x;
      a[i] -= x;
      mm = min(mm, a[i]);
    }
    cout << ans + mm << endl;
  }
  return 0;
}
