#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, ans, d, in, t, mi, ma;
  cin >> t;
  for (int T = 1; T <= t; T++) {
    cin >> n;
    ans = 0;
    d = 0;
    mi = LLONG_MAX;
    long long int a[n], b[n], aa = 0;
    vector<long long int> v(n);
    for (long long int i = 0; i <= n - 1; i++) {
      cin >> a[i] >> b[i];
    }
    v[0] = a[0] - min(b[n - 1], a[0]);
    aa += v[0];
    for (long long int i = 1; i <= n - 1; i++) {
      v[i] = a[i] - min(b[i - 1], a[i]);
      aa += v[i];
    }
    for (long long int i = 0; i <= n - 1; i++) {
      if (aa - v[i] + a[i] < mi) mi = aa - v[i] + a[i];
    }
    cout << mi << endl;
  }
  return 0;
}
