#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long n;
    scanf("%lld", &n);
    long long life[n];
    long long harm[n];
    long long i, j;
    for (i = 0; i < n; i++) {
      scanf("%lld %lld", &life[i], &harm[i]);
    }
    long long need[n];
    long long nsum = 0;
    need[0] = life[0] - harm[n - 1];
    if (need[0] >= 0) {
      nsum += need[0];
    }
    for (i = 1; i < n; i++) {
      need[i] = life[i] - harm[i - 1];
      if (need[i] >= 0) {
        nsum += need[i];
      }
    }
    long long mini = 9223372036854775000, sum = 0;
    for (i = 0; i < n; i++) {
      sum = life[i];
      sum += nsum;
      if (need[i] >= 0) {
        sum -= need[i];
      }
      mini = min(mini, sum);
    }
    printf("%lld\n", mini);
  }
  return 0;
}
