#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    vector<ll> a(n), b(n);
    vector<ll> needed(n);
    for (int i = 0; i < n; ++i) {
      scanf("%lld%lld", &a[i], &b[i]);
    }
    ll totneeded = 0;
    for (int i = 0; i < n; ++i) {
      int prev = i - 1;
      if (prev < 0) prev = n - 1;
      needed[i] = max(0LL, a[i] - b[prev]);
      totneeded += needed[i];
    }
    ll ans = 1e18;
    for (int i = 0; i < n; ++i) {
      ans = min(ans, totneeded - needed[i] + a[i]);
    }
    printf("%lld\n", ans);
  }
}
