#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    pair<long long int, long long int> p[n];
    for (long long int i = 0; i < n; i++) cin >> p[i].first >> p[i].second;
    long long int mn = -1;
    for (long long int i = 1; i < n; i++) {
      p[i].first -= p[i - 1].second;
      if (p[i].first <= 0) {
        if (mn > p[i].first + p[i - 1].second || mn == -1) {
          mn = p[i].first + p[i - 1].second;
        }
      } else {
        if (mn > p[i - 1].second || mn == -1) {
          mn = p[i - 1].second;
        }
      }
    }
    p[0].first -= p[n - 1].second;
    if (p[0].first <= 0) {
      if (mn > p[0].first + p[n - 1].second || mn == -1) {
        mn = p[0].first + p[n - 1].second;
      }
    } else {
      if (mn > p[n - 1].second || mn == -1) {
        mn = p[n - 1].second;
      }
    }
    long long int ans = 0;
    for (long long int i = 0; i < n; i++) {
      if (p[i].first > 0) ans += p[i].first;
    }
    ans += mn;
    cout << ans << endl;
  }
}
