#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7;
const long long INF = 1e12;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    long long sum = 0;
    long long minv = INF;
    long long x, y, k, a, b, q;
    for (int i = 0; i < n; i++) {
      scanf("%lld%lld", &a, &b);
      if (i == 0) x = a;
      if (i == n - 1) k = x - b;
      minv = min(minv, a);
      if (i > 0) {
        long long t = a - q;
        if (t > 0)
          minv = min(minv, a - t);
        else
          t = 0;
        sum += t;
      }
      q = b;
    }
    if (n > 1) {
      if (k > 0)
        minv = min(minv, x - k);
      else
        k = 0;
      sum += k;
    }
    cout << sum + minv << endl;
  }
  return 0;
}
