#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    int maxl, minr, l, r;
    cin >> maxl >> minr;
    while (--n) {
      cin >> l >> r;
      maxl = max(l, maxl);
      minr = min(r, minr);
    }
    cout << max(0, maxl - minr) << '\n';
  }
  return 0;
}
