#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, l, r, n;
  cin >> t;
  while (t--) {
    cin >> n;
    int mx = 0, mn = 1000000001;
    while (n--) {
      cin >> l >> r;
      mx = max(l, mx);
      mn = min(mn, r);
    }
    if (mn >= mx)
      cout << 0 << endl;
    else
      cout << mx - mn << endl;
  }
}
