#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int a[n], b[n], bullet = 10000000000000000, sum = 0;
    cin >> a[0] >> b[0];
    long long int health[n];
    for (long long int i = 1; i < n; i++) {
      cin >> a[i] >> b[i];
      if (b[i - 1] >= a[i]) {
        health[i] = 0;
      } else {
        health[i] = a[i] - b[i - 1];
      }
    }
    if (b[n - 1] >= a[0]) {
      health[0] = 0;
    } else {
      health[0] = a[0] - b[n - 1];
    }
    for (long long int i = 0; i < n; i++) {
      sum += health[i];
      bullet = min(bullet, a[i] - health[i]);
    }
    cout << bullet + sum << '\n';
  }
}
