#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  for (int i = 0; i < T; ++i) {
    int n;
    cin >> n;
    long long int preva, prevb;
    long long int tmpa, tmpb, za, zb, z;
    long long int mini = 1e13, sum_chain = 0;
    for (int j = 0; j < n; ++j) {
      cin >> tmpa >> tmpb;
      if (j != 0) {
        z = ((prevb > tmpa) ? 0 : (tmpa - prevb));
        sum_chain += z;
        mini = ((mini < tmpa - z) ? mini : (tmpa - z));
      } else {
        za = tmpa;
        zb = tmpb;
      }
      preva = tmpa;
      prevb = tmpb;
    }
    tmpa = za;
    tmpb = zb;
    z = ((prevb > tmpa) ? 0 : (tmpa - prevb));
    sum_chain += z;
    mini = ((mini < tmpa - z) ? mini : (tmpa - z));
    sum_chain += mini;
    cout << sum_chain << '\n';
  }
}
