#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    pair<long long, long long> p[n];
    long long a[n];
    for (long long i = 0; i < n; i++) {
      long long x, y;
      cin >> x >> y;
      p[i] = {x, y};
    }
    if (n == 1) {
      cout << p[0].first << "\n";
      continue;
    }
    long long sum = 0;
    for (long long i = 1; i < n; i++) {
      a[i] = max(p[i].first - p[i - 1].second, (long long)0);
      sum += a[i];
    }
    a[0] = max(p[0].first - p[n - 1].second, (long long)0);
    sum += a[0];
    long long ans = 1e18;
    for (long long i = 0; i < n; i++) {
      ans = min(ans, sum - a[i] + p[i].first);
    }
    cout << ans << "\n";
  }
}
