#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<pair<long long, long long>> v;
  long long po = 0;
  long long pr = 0;
  for (long long i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    v.push_back({a, b});
    po += max((long long)0, (v[i].first - pr));
    pr = v[i].second;
  }
  long long mi = po;
  long long oi = po;
  for (long long i = 1; i < n; i++) {
    long long cs = oi;
    cs -= v[i - 1].first;
    cs += v[i].first;
    cs -= max((long long)0, v[i].first - v[i - 1].second);
    long long j = i - 2;
    if (j < 0) {
      j = n - 1;
    }
    cs += max((long long)0, v[i - 1].first - v[j].second);
    oi = cs;
    mi = min(cs, mi);
  }
  cout << (mi) << endl;
}
bool p[100005];
long long lst[100005];
void s() {
  memset(p, true, sizeof p);
  p[0] = p[1] = false;
  for (int i = 2; i < 100005; i++) {
    if (p[i]) {
      lst[i] = i;
      for (int j = i + i; j < 100005; j += i) {
        p[j] = false;
        lst[j] = i;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  s();
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
