#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    long long a[n], b[n], c[n];
    for (int i = 0; i < (n); ++i) {
      cin >> a[i] >> b[i];
    }
    int j;
    long long ans = 0;
    for (int i = 0; i < (n); ++i) {
      j = (i + 1) % n;
      c[j] = max(a[j] - b[i], 0LL);
      ans += c[j];
    }
    long long add = 1e13;
    for (int i = 0; i < (n); ++i) {
      add = min(add, a[i] - c[i]);
    }
    ans += add;
    cout << ans << endl;
  }
}
