#include <bits/stdc++.h>
using namespace std;
long long a[300005][2];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long sum = 0;
    long long m = 1e14;
    for (int i = 0; i < n; i++) {
      scanf("%lld%lld", &a[i][0], &a[i][1]);
    }
    if (n == 1) {
      cout << a[0][0] << endl;
      continue;
    }
    for (int i = 0; i < n - 1; i++) {
      m = min(m, min(a[i][1], a[i + 1][0]));
    }
    m = min(m, min(a[n - 1][1], a[0][0]));
    for (int i = 0; i < n - 1; i++) {
      if (a[i][1] < a[i + 1][0]) {
        sum += a[i + 1][0] - a[i][1];
      }
    }
    if (n >= 2) {
      if (a[n - 1][1] < a[0][0]) {
        sum += a[0][0] - a[n - 1][1];
      }
    }
    sum += m;
    cout << sum << endl;
  }
}
