#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 123;
const long long oo = 1e18 + 123;
int n;
long long a[N], b[N], f[N * 4];
long long cal(int l, int r) {
  long long x = a[l];
  for (int i = l + 1; i <= r; ++i) {
    if (b[i - 1] >= a[i])
      ;
    else
      x += a[i] - b[i - 1];
  }
  cout << x << endl;
  return x;
}
void build(int k, int l, int r) {
  if (l == r) {
    f[k] = a[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
  if (b[mid] >= a[mid + 1])
    f[k] = f[k << 1] + f[k << 1 | 1] - a[mid + 1];
  else
    f[k] = f[k << 1] + f[k << 1 | 1] - b[mid];
}
long long find(int k, int l, int r, int L, int R) {
  if (l > R || L > r) return 0;
  if (L <= l && r <= R) return f[k];
  int mid = (l + r) >> 1;
  if (R <= mid) return find(k << 1, l, mid, L, R);
  if (L >= mid + 1) return find(k << 1 | 1, mid + 1, r, L, R);
  long long x = find(k << 1, l, mid, L, R),
            y = find(k << 1 | 1, mid + 1, r, L, R);
  if (b[mid] >= a[mid + 1]) return x + y - a[mid + 1];
  return x + y - b[mid];
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%lli%lli", &a[i], &b[i]);
    build(1, 1, n);
    long long res = f[1];
    for (int i = 2; i <= n; ++i) {
      long long x = find(1, 1, n, i, n), y = find(1, 1, n, 1, i - 1);
      if (b[n] >= a[1])
        res = min(res, x + y - a[1]);
      else
        res = min(res, x + y - b[n]);
    }
    cout << res << "\n";
  }
  return 0;
}
