#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<long long> a(n), b(n), c(n);
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    for (int i = 0; i < n; i++)
      c[(i + 1) % n] = max(a[(i + 1) % n] - b[i], 0LL);
    long long sum = accumulate(c.begin(), c.end(), 0LL), ans = 1e18;
    for (int i = 0; i < n; i++) ans = min(ans, sum - c[i] + a[i]);
    cout << ans << endl;
  }
  return 0;
}
