#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<long long int> a(n), b(n);
    long long int m = 1e12;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    long long int ans = 0;
    for (int i = 0; i < n; i++) {
      int j = (n + i - 1) % n;
      long long int x = max(a[i] - b[j], (long long int)0);
      ans += x;
      m = min(m, a[i] - x);
    }
    ans += m;
    cout << ans << "\n";
  }
}
