#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 5;
const int M = 2e3 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
template <class T>
inline void read(T &x) {
  char c;
  x = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') x = -1;
  T res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
  x *= res;
}
int n, m;
char str[N];
pair<long long, long long> f[N];
long long sum[N];
void solve(int cas) {
  read(n);
  for (int i = 1; i <= n; i++) {
    long long a, b;
    read(a);
    read(b);
    f[i] = f[i + n] = make_pair(a, b);
  }
  for (int i = 2; i <= 2 * n; i++) {
    sum[i] = sum[i - 1] + max(0ll, f[i].first - f[i - 1].second);
  }
  long long ans = 5e18;
  for (int i = 1; i <= n; i++) {
    ans = min(ans, f[i].first + sum[i + n - 1] - sum[i]);
  }
  cout << ans << endl;
}
int main() {
  int t;
  read(t);
  for (int i = 1; i <= t; i++) {
    solve(i);
  }
  return 0;
}
