#include <bits/stdc++.h>
using namespace std;
long long ans, t, i, j, a[300300], b[300300], m, n;
int main() {
  cin >> t;
  for (j = 1; j <= t; j++) {
    cin >> n;
    ans = 0;
    m = 1000000000111;
    for (i = 1; i <= n; i++) scanf("%lld %lld", &a[i], &b[i]);
    a[n + 1] = a[1];
    for (i = 1; i <= n; i++) {
      if (a[i + 1] > b[i]) {
        ans = ans + a[i + 1] - b[i];
        if (b[i] < m) m = b[i];
      } else if (a[i + 1] < m)
        m = a[i + 1];
    }
    ans = ans + m;
    cout << ans << endl;
  }
}
