#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> ar(n);
    vector<long long> br(n);
    for (long long i = 0; i < n; i++) {
      cin >> ar[i];
      cin >> br[i];
      ar.push_back(ar[i]);
      br.push_back(br[i]);
    }
    long long fans = 1e18;
    long long cur = 0;
    for (long long i = 0; i < n; i++) {
      if (i == 0) {
        cur += ar[i];
      } else {
        cur += max(0ll, ar[i] - br[i - 1]);
      }
    }
    fans = min(fans, cur);
    long long l = 0, r = n - 1;
    while (l < n) {
      fans = min(fans, cur);
      cur -= ar[l];
      cur -= max(0ll, ar[l + 1] - br[l]);
      cur += ar[l + 1];
      cur += max(0ll, ar[r + 1] - br[r]);
      l++, r++;
    }
    cout << fans << "\n";
  }
  return 0;
}
