#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<ll> A, B;
    for (int i = 0; i < n; i++) {
      ll a, b;
      cin >> a >> b;
      A.push_back(a);
      B.push_back(b);
    }
    ll col = 0LL;
    for (int i = 0; i < n; i++) {
      col += max(0LL, A[i] - B[(i - 1 + n) % n]);
    }
    ll ans = 1e18;
    for (int i = 0; i < n; i++) {
      ans = min(ans, col - max(0LL, A[i] - B[(i - 1 + n) % n]) + A[i]);
    }
    cout << ans << endl;
  }
}
