#include <bits/stdc++.h>
using namespace std;
const int N = 301000;
int n;
long long extra[N], a[N], b[N];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%lld%lld", &a[i], &b[i]);
    long long s = 0, d = 1ll << 60;
    for (int i = 0; i < n; i++) {
      extra[i] = max(0ll, a[i] - b[(i + n - 1) % n]);
      s += extra[i];
    }
    for (int i = 0; i < n; i++) d = min(d, s - extra[i] + a[i]);
    printf("%lld\n", d);
  }
}
