#include <bits/stdc++.h>
using namespace std;
const long long inf = 2100000000;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc;
  cin >> tc;
  for (int ks = 1; ks <= tc; ks++) {
    int n;
    cin >> n;
    long long mxL = 0, mnR = inf;
    for (int i = 0; i < n; i++) {
      long long l, r;
      cin >> l >> r;
      mxL = max(mxL, l);
      mnR = min(mnR, r);
    }
    if (mnR >= mxL) {
      cout << "0\n";
    } else {
      cout << mxL - mnR << "\n";
    }
  }
  return 0;
}
