#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long mnr = 1e9, mxl = -1e9;
    for (long long i = 0; i < n; i++) {
      long long l, r;
      cin >> l >> r;
      mnr = min(mnr, r);
      mxl = max(mxl, l);
    }
    cout << (n == 1 ? 0 : max(mxl - mnr, 0ll)) << "\n";
  }
}
