#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> av, bv;
    for (long long i = 0; i < n; i++) {
      long long a, b;
      cin >> a >> b;
      av.push_back(a);
      bv.push_back(b);
    }
    long long ans = 0, mn = 1e18;
    for (long long i = 0; i < n; i++) {
      long long ind = (i + 1) % n;
      long long a = av[ind];
      long long b = min(a, bv[i]);
      mn = min(mn, b);
      ans += (a - b);
    }
    ans += mn;
    cout << ans << "\n";
  }
  return 0;
}
