#include <bits/stdc++.h>
using namespace std;
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
const ll mod = 1000000007;
void solve() {
  int n;
  cin >> n;
  vector<ll> A(n), B(n);
  for (int _n = n, i = 0; i < _n; ++i) {
    cin >> A[i];
    cin >> B[i];
  }
  vector<ll> H = A;
  ll sum = 0;
  for (int _n = n, i = 0; i < _n; ++i) {
    H[i] -= min(H[i], B[(i - 1 + n) % n]);
    sum += H[i];
  }
  ll ans = LLONG_MAX;
  for (int _n = n, i = 0; i < _n; ++i) {
    ans = min(ans, sum + A[i] - H[i]);
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    solve();
  }
}
