#include <bits/stdc++.h>
using namespace std;
long long f[10010];
map<long long, long long> vis;
long long a[300005], b[300005], c[300005];
signed main() {
  std::ios::sync_with_stdio(false);
  long long t, n, i, j, m;
  cin >> t;
  while (t--) {
    cin >> n;
    for (i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    long long sum = 0;
    for (i = 0; i < n; i++) {
      if (i == 0)
        c[i] = max(0ll, a[0] - b[n - 1]);
      else
        c[i] = max(0ll, a[i] - b[i - 1]);
      sum += c[i];
    }
    long long ans = 1e18;
    for (i = 0; i < n; i++) {
      ans = min(ans, sum - c[i] + a[i]);
    }
    cout << ans << endl;
  }
}
