#include <bits/stdc++.h>
using namespace std;
const int Max = 3e5 + 5;
long long a[Max], b[Max];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long T, n;
  cin >> T;
  while (T--) {
    long long bullets = 0, ans = LLONG_MAX;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i] >> b[i];
      if (i > 1) bullets += max(0ll, a[i] - b[i - 1]);
    }
    bullets += max(0ll, a[1] - b[n]);
    for (int i = 1; i <= n; i++) {
      long long prev = i - 1;
      if (!prev) prev = n;
      long long st = max(0ll, a[i] - b[prev]);
      ans = min(ans, bullets - st + a[i]);
    }
    cout << ans << endl;
  }
}
