#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<long long, long long> > v;
    v.push_back(make_pair(0LL, 0LL));
    long long prefix = 0;
    for (int i = 1; i <= n; i++) {
      long long x, y;
      cin >> x >> y;
      v.push_back(make_pair(x, y));
    }
    for (int i = 2; i <= n; i++)
      prefix += max(0LL, v[i].first - v[i - 1].second);
    prefix += max(0LL, v[1].first - v[n].second);
    long long mn = LLONG_MAX;
    for (int i = 2; i <= n; i++)
      mn =
          min(mn, v[i].first + prefix - max(0LL, v[i].first - v[i - 1].second));
    mn = min(mn, v[1].first + prefix - max(0LL, v[1].first - v[n].second));
    cout << mn << '\n';
  }
  return 0;
}
