#include <bits/stdc++.h>
using namespace std;
const long long N = 6e5 + 5;
long long n;
long long a[300005];
long long b[300005];
long long sum = 0;
long long ps[N];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  long long one = 1;
  long long two = 2;
  long long zero = 0;
  while (t--) {
    cin >> n;
    vector<long long> a_new;
    vector<long long> b_new;
    a_new.clear();
    b_new.clear();
    for (long long i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
      a_new.push_back(a[i]);
      b_new.push_back(b[i]);
    }
    for (long long i = 0; i < n; i++) {
      a_new.push_back(a[i]);
      b_new.push_back(b[i]);
    }
    vector<long long> kill;
    long long n1 = two * n;
    for (long long i = 0; i < (n1); i++) {
      if (i == 0) {
        kill.push_back(0);
      } else {
        long long d = a_new[i] - b_new[i - 1];
        kill.push_back(max(zero, d));
      }
    }
    for (long long i = (n1)-1; i >= 0; i--) {
      if (i == (n1)-1) {
        ps[i] = kill[(n1)-1];
      } else {
        long long d = ps[i + 1] + kill[i];
        ps[i] = d;
      }
    }
    long long minm_bullets = LLONG_MAX;
    long long bullets = 0;
    for (long long i = 0; i < (n); i++) {
      bullets = a_new[i] + ps[i + 1] - ps[i + n];
      minm_bullets = min(minm_bullets, bullets);
    }
    cout << minm_bullets << '\n';
  }
}
