#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
int prev(int i) {
  if (i == 0) return n - 1;
  return i - 1;
}
int next(int i) {
  if (i == n - 1) return 0;
  return i + 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<ll> hp(n);
    vector<ll> dmg(n);
    ll all = 0;
    for (int i = 0; i < n; i++) {
      cin >> hp[i];
      cin >> dmg[i];
      all += hp[i];
    }
    ll mi = 12000000000001LL;
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      ll eff = min(dmg[prev(i)], hp[i]);
      sum += eff;
      if (eff < mi) {
        mi = eff;
      }
    }
    cout << all - (sum - mi) << "\n";
  }
  return 0;
}
