#include <bits/stdc++.h>
using namespace std;
long long min(long long a, long long b) {
  if (a > b)
    return b;
  else
    return a;
}
long long max(long long a, long long b) {
  if (a > b)
    return a;
  else
    return b;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n], b[n];
    long long sum = 0;
    long long flag = 0;
    long long mini = 10000000000000000;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
      if (a[i] < mini) mini = a[i];
    }
    long long mindif = 1000000000000000;
    for (int i = 1; i < n; i++) {
      if (b[i - 1] < a[i]) {
        sum += a[i] - b[i - 1];
        if (a[i] == mini) flag = 1;
        if (b[i - 1] < mindif) mindif = b[i - 1];
      }
    }
    if (b[n - 1] < a[0]) {
      sum += a[0] - b[n - 1];
      if (a[0] == mini) flag = 1;
      if (b[n - 1] < mindif) mindif = b[n - 1];
    }
    sum += min(mindif, mini);
    cout << sum << "\n";
  }
  return 0;
}
