#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int N = 300010;
pll a[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
    ll best = a[0].first;
    for (int i = 1; i < n; i++) {
      best += max(0ll, a[i].first - a[i - 1].second);
    }
    ll prev = best;
    for (int i = 1; i < n; i++) {
      prev = prev - a[i - 1].first - max(0ll, a[i].first - a[i - 1].second) +
             a[i].first + max(0ll, a[i - 1].first - a[(i - 2 + n) % n].second);
      best = min(best, prev);
    }
    cout << best << endl;
  }
  return 0;
}
