#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long n, i;
    scanf("%lld", &n);
    vector<long long> z;
    long long a[n], b[n], x = 0;
    for (i = 0; i < n; i++) {
      scanf("%lld %lld", &a[i], &b[i]);
      if (i != 0) {
        if (a[i] - b[i - 1] > 0) {
          x += (a[i] - b[i - 1]);
          z.push_back(b[i - 1]);
        } else
          z.push_back(a[i]);
      }
    }
    if (a[0] - b[n - 1] > 0) {
      x += (a[0] - b[n - 1]);
      z.push_back(b[n - 1]);
    } else
      z.push_back(a[0]);
    sort(z.begin(), z.end());
    x += z[0];
    printf("%lld\n", x);
  }
}
