#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "inline", "no-stack-protector")
using namespace std;
long long read() {
  long long x;
  char c(getchar());
  bool k;
  while (!isdigit(c) && c ^ '-')
    if ((c = getchar()) == EOF) exit(0);
  c ^ '-' ? (k = 1, x = c & 15) : k = x = 0;
  while (isdigit((c = getchar()))) x = x * 10 + (c & 15);
  return k ? x : -x;
}
void wr(long long a) {
  if (a < 0) putchar('-'), a = -a;
  if (a <= 9)
    putchar(a | '0');
  else
    wr(a / 10), putchar((a % 10) | '0');
}
signed const INF(0x3f3f3f3f), NINF(0xc3c3c3c3);
long long const LINF(0x3f3f3f3f3f3f3f3fLL), LNINF(0xc3c3c3c3c3c3c3c3LL);
long long t, n, a[(300010)], b[(300010)], c[(300010)], ans;
signed main() {
  (t = read());
  while (t--) {
    (n = read()), ans = LINF;
    for (long long i(1); i <= (n); ++i) (a[i] = read()), (b[i] = read());
    c[1] = max(0LL, a[1] - b[n]);
    for (long long i(2); i <= (n); ++i)
      c[i] = max(0LL, a[i] - b[i - 1]) + c[i - 1];
    for (long long i(1); i <= (n); ++i)
      ans = min(ans, a[i] + c[n] - c[i] + c[i - 1]);
    wr(ans), putchar('\n');
  }
  exit(0);
}
