#include <bits/stdc++.h>
using namespace std;
long long n, x, m, a, b, c, d, t, y, k;
vector<long long> v;
vector<vector<int> > vv;
vector<bool> visited;
int main() {
  ios::sync_with_stdio(false);
  cin >> t;
  for (long long w = 0; w < t; w++) {
    cin >> n;
    vector<pair<long long, long long> > in;
    for (long long i = 0; i < n; i++) {
      cin >> a >> b;
      in.push_back(make_pair(a, b));
    }
    vector<pair<long long, long long> > ce;
    ce.push_back(
        make_pair(max(in[0].first - in[n - 1].second, 0LL), in[0].first));
    for (long long i = 1; i < n; i++) {
      ce.push_back(
          make_pair(max(in[i].first - in[i - 1].second, 0LL), in[i].first));
    }
    long long sum = 0;
    for (long long i = 0; i < n; i++) {
      sum += ce[i].first;
    }
    long long mn = 1000000000000000000;
    for (long long i = 0; i < n; i++) {
      long long sum1 = sum;
      sum1 -= ce[i].first;
      sum1 += ce[i].second;
      mn = min(mn, sum1);
    }
    cout << mn << endl;
  }
}
