#include <bits/stdc++.h>
using namespace std;
long long a[300010], b[300010];
int main() {
  long long t;
  scanf("%lld", &t);
  while (t--) {
    long long n;
    scanf("%lld", &n);
    for (long long i = 0; i < n; i++) scanf("%lld %lld", a + i, b + i);
    long long ans = 0;
    long long mn = INT64_MAX;
    for (long long i = 0; i < n; i++) {
      long long ni = (i + 1) % n;
      ans += a[ni] - min(a[ni], b[i]);
      mn = min(mn, min(a[ni], b[i]));
    }
    ans += mn;
    printf("%lld\n", ans);
  }
}
