#include <bits/stdc++.h>
long long a[300005], b[300005], node[300005], edge[300005];
int t, n;
int main() {
  scanf("%d", &t);
  while (t--) {
    long long ans = 1000000000000000000, sum = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%lld %lld", &a[i], &b[i]);
      node[i] = a[i];
      if (i ^ 1) edge[i - 1] = (0 > a[i] - b[i - 1] ? 0 : a[i] - b[i - 1]);
    }
    edge[n] = (0 > a[1] - b[n] ? 0 : a[1] - b[n]);
    for (int i = 1; i <= n; i++) sum += edge[i];
    for (int i = 1; i <= n; i++) {
      if (i ^ 1)
        ans = (ans < node[i] + sum - edge[i - 1] ? ans
                                                 : node[i] + sum - edge[i - 1]);
      else
        ans = (ans < node[i] + sum - edge[n] ? ans : node[i] + sum - edge[n]);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
