#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    int n;
    cin >> n;
    int lefter = 1000000000, righter = 0;
    for (int j = 0; j < n; ++j) {
      int l, r;
      cin >> l >> r;
      lefter = min(lefter, r);
      righter = max(righter, l);
    }
    cout << max(0, righter - lefter) << '\n';
  }
}
