#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
long long a[N], b[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long ans = 0, help = LONG_LONG_MAX;
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    for (int i = 0; i < n; ++i) {
      b[i] = min(b[i], a[(i + 1) % n]);
      help = min(help, b[i]);
      ans += a[i] - b[i];
    }
    ans += help;
    cout << ans << '\n';
  }
  return 0;
}
