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