#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
long long a[1000010], b[1000010], f[1000010];
int main() {
  fast();
  long long t, n, x;
  cin >> t;
  long long sum = 0;
  long long mx;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i] >> b[i];
    sum = 0;
    mx = 1000000000000;
    for (int i = 1; i <= n; ++i)
      if (i == 1)
        f[i] = max(sum, a[i] - b[n]);
      else
        f[i] = max(sum, a[i] - b[i - 1]);
    for (int i = 1; i <= n; ++i) {
      mx = min(mx, a[i] - f[i]);
      sum += f[i];
    }
    cout << sum + mx << "\n";
  }
  return 0;
}
