#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  scanf("%lld", &t);
  while (t--) {
    long long n, par = 0, temp = 1000000000000000000;
    scanf("%lld", &n);
    long long a[n + 5], b[n + 5], minus[n + 5];
    for (long long i = 0; i < n; i++) scanf("%lld %lld", &a[i], &b[i]);
    for (long long i = 0; i < n; i++)
      minus[i] = max((a[i] - b[(i - 1 + n) % n]), 0ll), par += minus[i];
    for (long long i = 0; i < n; i++) temp = min(a[i] + (par - minus[i]), temp);
    cout << temp << endl;
  }
}
