#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void Read(T& x) {
  bool Neg = false;
  char c;
  for (c = getchar(); c<'0' | c> '9'; c = getchar())
    if (c == '-') Neg = !Neg;
  x = c - '0';
  for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
  if (Neg) x = -x;
}
template <typename T>
inline void Write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  T p = 1;
  for (T temp = x / 10; temp > 0; temp /= 10) p *= 10;
  for (; p > 0; x %= p, p /= 10) putchar(x / p + '0');
}
const int oo = 2e9;
const long long inf = 1e15;
const int N = 300005;
long long a[N], b[N], dp[N];
int n;
void solve() {
  Read(n);
  for (int i = 1; i <= n; ++i) {
    Read(a[i]);
    Read(b[i]);
  }
  a[0] = a[n];
  b[0] = b[n];
  long long s = 0;
  for (int i = 1; i <= n; ++i) {
    dp[i] = max(0LL, a[i] - b[i - 1]);
    s += dp[i];
  }
  long long ans = 1e18;
  for (int i = 1; i <= n; ++i) {
    ans = min(ans, a[i] + s - dp[i]);
  }
  Write(ans);
  putchar('\n');
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  Read(t);
  while (t--) solve();
}
