#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n], b[n], final = 0;
    cin >> a[0] >> b[0];
    for (int i = 1; i < n; i++) {
      cin >> a[i] >> b[i];
      final += (max((long long)(0), a[i] - b[i - 1]));
    }
    final += (max((long long)(0), a[0] - b[n - 1]));
    long long mini = final - max(0ll, a[0] - b[n - 1]) + a[0];
    for (int i = 1; i < n; i++) {
      mini = min(mini, final - max(0ll, a[i] - b[i - 1]) + a[i]);
    }
    cout << mini << '\n';
  }
  return 0;
}
