#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n, l = 0, r = LLONG_MAX, ans = 0;
    cin >> n;
    while (n--) {
      long long x, y;
      cin >> x >> y;
      l = max(l, x);
      r = min(r, y);
    }
    cout << max(ans, l - r) << endl;
  }
}
