#include <bits/stdc++.h>
using namespace std;
const long long OO = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const double pi = 3.14159265359;
void skrrrt() { ios_base::sync_with_stdio(0), cin.tie(0); }
long long n, a[300009], b[300009];
int main() {
  skrrrt();
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      sum += max(0ll, a[i] - (b[(i - 1 + n) % n]));
    }
    long long ans = 1e18;
    for (int i = 0; i < n; i++) {
      ans = min(ans, sum + (a[i] - max(0ll, a[i] - b[(i - 1 + n) % n])));
    }
    cout << ans << "\n";
  }
}
