#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 100;
const int INF = 1e9;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int Q;
  cin >> Q;
  while (Q--) {
    int n;
    cin >> n;
    vector<pair<long long, long long>> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i].first >> a[i].second;
    }
    long long sb = 0LL;
    long long mb = LLONG_MAX;
    long long sa = 0LL;
    for (int i = 0; i < n; i++) {
      sa += a[i].first;
      int ni = (i + 1);
      if (ni >= n) ni -= n;
      long long na = min(a[ni].first, a[i].second);
      sb += na;
      mb = min(na, mb);
    }
    cout << sa - sb + mb << '\n';
  }
  return 0;
}
