#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t t;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    int64_t n;
    cin >> n;
    int64_t a[n];
    int64_t b[n];
    int64_t aa, bb;
    for (int64_t i = 0; i < n; i++) {
      cin >> aa >> bb;
      a[i] = aa;
      b[i] = bb;
    }
    int64_t pre1[n];
    int64_t pre2[n];
    int64_t arr[n];
    int64_t miin = 1000000000000000000;
    for (int64_t i = 0; i < n; i++) {
      if (i == n - 1) {
        arr[i] = max((int64_t)0, a[0] - b[n - 1]);
      } else
        arr[i] = max((int64_t)0, a[i + 1] - b[i]);
    }
    pre1[0] = arr[0];
    for (int64_t i = (1); i <= (n - 1); i++) {
      pre1[i] = pre1[i - 1] + arr[i];
    }
    pre2[n - 1] = arr[n - 1];
    for (int64_t i = n - 2; i >= 0; i--) {
      pre2[i] = pre2[i + 1] + arr[i];
    }
    int64_t index = n - 1;
    int64_t tmp;
    for (int64_t i = 0; i < n; i++) {
      if (i == 0) {
        tmp = a[0] + pre1[n - 2];
      } else if (i == 1) {
        tmp = a[1] + pre2[1];
      } else {
        tmp = a[i] + pre1[index - 1] + pre2[index + 1];
      }
      miin = min(miin, tmp);
      index = (index + 1) % n;
    }
    cout << miin << endl;
  }
}
