#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);
  long long t, n, l, r;
  cin >> t;
  while (t--) {
    cin >> n;
    long long mn = 1e18, mx = -1e18;
    for (int i = (0); i <= ((n)-1); i += (+1)) {
      cin >> l >> r;
      mx = max(mx, l);
      mn = min(mn, r);
    }
    cout << max(0ll, mx - mn) << '\n';
  }
  return 0;
}
