#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long double error = 2e-6;
const long double PI = acosl(-1);
inline long long int MOD(long long int x, long long int m = mod) {
  long long int y = x % m;
  return (y >= 0) ? y : y + m;
}
const int inf = 1e9;
const long long int infl = 1061109567;
const int nmax = 1000 + 10;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  scanf("%d", &t);
  while (t--) {
    long long int n;
    scanf("%lld", &n);
    long long int i;
    std::vector<pair<long long int, long long int> > vc;
    std::vector<long long int> playboy(n + 2);
    for (i = 0; i < n; i++) {
      long long int ai, bi;
      scanf("%lld%lld", &ai, &bi);
      vc.push_back({ai, bi});
      if (i > 0) {
        long long int pelam = max(0ll, ai - vc[i - 1].second);
        playboy[0] += pelam;
      }
    }
    long long int mn = 4e18;
    for (i = 0; i < n; i++) {
      if (i == 0) {
        mn = min(mn, vc[i].first + playboy[0]);
        continue;
      }
      if (i == 1) {
        long long int pelam = playboy[0] -
                              max(0ll, vc[i].first - vc[i - 1].second) +
                              max(0ll, vc[0].first - vc[n - 1].second);
        playboy[i] = pelam;
        mn = min(mn, pelam + vc[i].first);
        continue;
      }
      long long int pelam = playboy[i - 1] -
                            max(0ll, vc[i].first - vc[i - 1].second) +
                            max(0ll, vc[i - 1].first - vc[i - 2].second);
      playboy[i] = pelam;
      mn = min(mn, pelam + vc[i].first);
    }
    printf("%lld\n", mn);
  }
  return 0;
}
