#include <bits/stdc++.h>
using namespace std;
const int mn = 3e5 + 7;
long long a[mn], b[mn], c[mn], ans, tmp;
int main() {
  int tests = 1;
  int n;
  scanf("%d", &tests);
  while (tests--) {
    ans = 0;
    tmp = 1e13;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
      scanf("%lld%lld", &a[i], &b[i]);
    }
    for (int i = 1; i <= n; ++i) c[i] = a[i] - b[(i - 1 == 0 ? n : i - 1)];
    for (int i = 1; i <= n; ++i) ans += max(c[i], 0ll);
    for (int i = 1; i <= n; ++i) {
      if (c[i] <= 0)
        tmp = min(tmp, a[i]);
      else
        tmp = min(tmp, b[(i - 1 == 0 ? n : i - 1)]);
    }
    printf("%lld\n", ans + tmp);
  }
}
