#include <bits/stdc++.h>
using namespace std;
bool same(int l1, int r1, int l2, int r2) {
  if (r1 < l2 || r2 < l1) {
    return 0;
  }
  return 1;
}
signed main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    int l = 1e9 + 5, r = 0;
    for (int i = 0; i < n; i++) {
      int l3, r3;
      cin >> l3 >> r3;
      r = max(r, l3);
      l = min(l, r3);
    }
    cout << max(0, r - l) << "\n";
  }
  return 0;
}
