#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 5;
int n;
long long a[N], b[N];
long long cnt[N], sum[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i] >> b[i];
      a[i + n] = a[i];
      b[i + n] = b[i];
    }
    n <<= 1;
    for (int i = 1; i <= n; i++) {
      cnt[i] = a[i] - b[i - 1] > 0 ? a[i] - b[i - 1] : 0;
      sum[i] = sum[i - 1] + cnt[i];
    }
    long long res = 1e18;
    for (int i = 1; i <= n >> 1; i++) {
      long long ans = sum[i + (n >> 1) - 1] - sum[i - 1];
      ans += a[i] - cnt[i];
      res = min(res, ans);
    }
    cout << res << endl;
  }
  return 0;
}
