#include <bits/stdc++.h>
using namespace std;
long long ara[300002][2];
int main() {
  long long test;
  scanf("%lld", &test);
  while (test--) {
    long long n;
    scanf("%lld", &n);
    for (long long i = 1; i <= n; i++) {
      scanf("%lld", &ara[i][0]);
      scanf("%lld", &ara[i][1]);
    }
    long long dif = ara[1][0] - ara[n][1];
    long long tot = 0;
    for (long long i = 1; i <= n; i++) {
      if (i == 1) {
        if (dif < 0) {
          dif = 0;
        }
        tot += dif;
      } else {
        dif = ara[i][0] - ara[i - 1][1];
        if (dif < 0) dif = 0;
        tot += dif;
      }
    }
    dif = ara[1][0] - ara[n][1];
    if (dif < 0) dif = 0;
    long long nw = tot - dif + ara[1][0];
    long long ans = nw;
    for (long long i = 1; i <= n; i++) {
      if (i == 1) continue;
      dif = ara[i][0] - ara[i - 1][1];
      if (dif < 0) dif = 0;
      nw = tot - dif + ara[i][0];
      ans = min(ans, nw);
    }
    printf("%lld\n", ans);
  }
}
