#include <bits/stdc++.h>
using namespace std;
long long A[303030];
long long B[303030];
long long D[303030];
int 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;
    for (int i = 0; i < N; i++) {
      int bef = (i + N - 1) % N;
      D[i] = max(A[i] - B[bef], 0LL);
      s += D[i];
    }
    long long ans = (1LL << 60);
    for (int i = 0; i < N; i++) {
      ans = min(ans, s - D[i] + A[i]);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
