#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double EPS = 1e-9;
const long long MOD = 1e9 + 7;
const long long INF = (long long)1e15;
long long addMod(long long a, long long b) {
  if (a + b >= MOD) return a + b - MOD;
  return a + b;
}
long long mulMod(long long a, long long b) { return (a * b) % MOD; }
long long fast(long long a, long long b) {
  if (b == 0) return 1;
  long long tmp = fast(a, b / 2);
  tmp = mulMod(tmp, tmp);
  if (b % 2 == 1) tmp = mulMod(tmp, a);
  return tmp;
}
long long GCD(long long a, long long b) {
  if (a > b) return GCD(b, a);
  if (a == 0) return b;
  return GCD(b % a, a);
}
template <typename T>
T getnum() {
  int sign = 1;
  T ret = 0;
  char c;
  do {
    c = getchar();
  } while (c == ' ' || c == '\n');
  if (c == '-')
    sign = -1;
  else
    ret = c - '0';
  while (1) {
    c = getchar();
    if (c < '0' || c > '9') break;
    ret = 10 * ret + c - '0';
  }
  return sign * ret;
}
inline void ini(int& x) { x = getnum<int>(); }
const int N = 3e5;
long long a[N + 5], b[N + 5];
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 0; i <= (int)n - 1; i += 1) {
      cin >> a[i] >> b[i];
    }
    long long ans = 0, mini = INF;
    for (int i = 0; i <= (int)n - 1; i += 1) {
      int id = (i + n - 1) % n;
      ans += max((long long)0, a[i] - b[id]);
      mini = min(mini, a[i]);
      mini = min(mini, b[i]);
    }
    cout << ans + mini << endl;
  }
  return 0;
}
