#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
  int n;
  cin >> n;
  int lef = 1e9 + 1, rig = 0;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    lef = min(lef, y);
    rig = max(rig, x);
  }
  if (lef <= rig) {
    cout << rig - lef << "\n";
  } else {
    cout << 0 << "\n";
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t-- > 0) {
    solve();
  }
  return 0;
}
