#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n;
long long a[N];
long long b[N];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    vector<pair<long long, long long> > v;
    scanf("%d", &n);
    v.clear();
    for (int i = 0; i < n; i++) {
      long long x, y;
      scanf("%I64d%I64d", &x, &y);
      v.push_back(make_pair(x, y));
    }
    long long ans = 0;
    long long maxx = 0, minn = 1e18 + 10;
    long long t;
    for (int i = 0; i < n; i++) {
      maxx = v[(i + 1) % n].first - v[i].second;
      if (maxx < 0) {
        maxx = 0;
      }
      ans += maxx;
    }
    for (int i = 0; i < n; i++) {
      t = v[(i + 1) % n].first - v[i].second;
      if (t < 0) {
        t = 0;
      }
      if (minn > ans + v[(i + 1) % n].first - t) {
        minn = ans + v[(i + 1) % n].first - t;
      }
    }
    cout << minn << endl;
  }
  return 0;
}
