#include <bits/stdc++.h>
using namespace std;
long long n, t, q, w, sumh = 0, sumd = 0, mn, a[300010], b[300010];
vector<long long> dam;
void input() {
  cin >> t;
  while (t--) {
    dam.clear();
    sumh = 0, sumd = 0, mn = 1e12 + 1;
    cin >> n;
    cin >> a[0] >> b[0];
    sumh = a[0];
    for (int i = 1; i < n; i++) {
      cin >> a[i] >> b[i];
      sumh += a[i];
      b[i - 1] = min(b[i - 1], a[i]);
      dam.push_back(b[i - 1]);
      sumd += b[i - 1];
      mn = min(mn, b[i - 1]);
    }
    b[n - 1] = min(b[n - 1], a[0]);
    sumd += b[n - 1];
    dam.push_back(b[n - 1]);
    mn = min(mn, b[n - 1]);
    long long ans = sumh - sumd + mn;
    cout << ans << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  input();
  return 0;
}
