#include <bits/stdc++.h>
using namespace std;
long long fpow(long long n, long long k, long long p = 1000000007) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
long long inv(long long a, long long p = 1000000007) {
  return fpow(a, p - 2, p);
}
long long power(long long x, long long y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
  long long t, n, ans;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    long long a[n], b[n], c[n];
    for (long long i = (0); i < (n); i++) cin >> a[i] >> b[i];
    for (long long i = (1); i < (n); i++) {
      c[i] = max(0ll, a[i] - b[i - 1]);
    }
    c[0] = max(0ll, a[0] - b[n - 1]);
    ans = 0;
    long long ma = 1e15;
    for (long long i = (0); i < (n); i++) {
      ma = min(ma, a[i] - c[i]);
    }
    ans = ans + ma;
    for (long long i = (0); i < (n); i++) ans += c[i];
    cout << ans << "\n";
  }
  return 0;
}
