#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 4;
void solve() {
  int n;
  cin >> n;
  vector<pair<long long, long long> > val;
  for (int i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    val.push_back({a, b});
  }
  long long ans = 0, mn = 1e12 + 5;
  for (int i = 1; i < n; i++) {
    long long tmp = val[i].first - val[i - 1].second;
    if (tmp > 0) {
      ans += tmp;
      val[i].first -= tmp;
    }
    if (val[i].first < mn) mn = val[i].first;
  }
  long long tmp = val[0].first - val[n - 1].second;
  if (tmp > 0) {
    ans += tmp;
    val[0].first -= tmp;
  }
  if (val[0].first < mn) mn = val[0].first;
  ans += mn;
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
