#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
long long t, n, a[maxn], b[maxn], c[maxn];
int main() {
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld", &n);
    for (int i = 1; i <= n; ++i) scanf("%lld%lld", &a[i], &b[i]);
    long long sum = 0;
    for (int i = 1; i <= n; ++i) {
      if (i == 1)
        c[i] = max(0ll, a[i] - b[n]);
      else
        c[i] = max(0ll, a[i] - b[i - 1]);
      sum += c[i];
    }
    long long ans = 0x3f3f3f3f3f3f3f3f;
    for (int i = 1; i <= n; ++i) ans = min(ans, sum - c[i] + a[i]);
    printf("%lld\n", ans);
  }
  return 0;
}
