#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(), cout.tie();
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    pair<long long, long long> a[n];
    long long b[n];
    for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
    if (n == 1) {
      cout << a[0].first << "\n";
      continue;
    }
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        long long x = a[i].first - a[n - 1].second;
        if (x <= 0)
          b[i] = 0;
        else
          b[i] = x;
      } else {
        long long x = a[i].first - a[i - 1].second;
        if (x <= 0)
          b[i] = 0;
        else
          b[i] = x;
      }
    }
    for (int i = 1; i < n; i++) b[i] += b[i - 1];
    long long ans = 1e18;
    for (int i = 0; i < n; i++) {
      long long x = a[i].first;
      if (i != 0) x += b[i - 1];
      if (i != (n - 1)) x += (b[n - 1] - b[i]);
      ans = min(ans, x);
    }
    cout << ans << "\n";
  }
  return 0;
}
