#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
const long long mod = 1e9 + 7;
const int inf = 2147483647;
long long a[N], b[N];
long long sum[N];
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", a + i, b + i);
  }
  long long ans;
  long long num = 0;
  for (int i = 1; i <= n; i++) {
    int id = i - 1;
    if (id == 0) id = n;
    sum[i] = a[i] - b[id];
    if (sum[i] > 0) {
      num += sum[i];
    }
  }
  int flag = 1;
  for (int i = 1; i <= n; i++) {
    if (sum[i] < 0) {
      if (flag) ans = num + a[i];
      flag = 0;
      ans = min(ans, num + a[i]);
    } else {
      if (flag) ans = num - sum[i] + a[i];
      flag = 0;
      ans = min(ans, num - sum[i] + a[i]);
    }
  }
  printf("%lld\n", ans);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}
