#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long mM(long long a, long long b, long long mod) {
  long long res = 0;
  a %= mod;
  while (b) {
    if (b & 1) res = (res + a) % mod;
    a = (2 * a) % mod;
    b >>= 1;
  }
  return res;
}
long long fpow(long long x, long long y, long long p) {
  x = x % p;
  long long res = 1;
  while (y) {
    if (y & 1) res = (res * x) % p;
    res %= p;
    if (res < 0) res = (res + p) % p;
    y = y >> 1;
    x = (x * x) % p;
    if (x < 0) x = (x + p) % p;
    x %= p;
  }
  return res;
}
long long inv(long long n, long long p) { return fpow(n, p - 2, p); }
long long modmul(long long x, long long y) { return (x * y) % mod; }
long long modadd(long long x, long long y) {
  if (x >= mod) x -= mod;
  if (y >= mod) y -= mod;
  long long temp = x + y;
  if (temp >= mod) temp -= mod;
  if (temp < 0) temp = temp + mod;
  return temp;
}
long long power(long long a, long long b) {
  long long ans = 1;
  if (b == 0) return 1;
  if (b % 2 == 0) {
    ans = (ans * power((a * a) % mod, b / 2)) % mod;
  } else {
    ans = (ans * a) % mod;
    ans = (ans * power((a * a) % mod, b / 2)) % mod;
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    pair<long long, long long> arr[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i].first;
      cin >> arr[i].second;
    }
    long long flag[n];
    long long sum = 0;
    long long mn = arr[0].first;
    for (int i = 0; i < n; i++) {
      flag[i] = max((long long)0, arr[i].first - arr[(i - 1 + n) % n].second);
      sum += flag[i];
      mn = min(mn, arr[i].first - flag[i]);
    }
    cout << sum + mn << endl;
  }
}
