#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<long long> a(2 * n), b(2 * n);
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    for (int i = 0; i < n; i++) {
      a[n + i] = a[i];
      b[n + i] = b[i];
    }
    vector<long long> need(2 * n);
    for (int i = 1; i < 2 * n; i++) {
      if (a[i] - b[i - 1] > 0) {
        need[i] = a[i] - b[i - 1];
      }
      need[i] += need[i - 1];
    }
    long long ans = -1;
    for (int i = 0; i < n; i++) {
      long long v = a[i] + need[i + n - 1] - need[i];
      if (ans == -1 || ans > v) {
        ans = v;
      }
    }
    cout << ans << "\n";
  }
}
