#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, i, r;
  vector<int> a, b;
  cin >> t;
  while (t > 0) {
    cin >> n;
    for (i = 0; i < n; i++) {
      cin >> r;
      a.push_back(r);
      cin >> r;
      b.push_back(r);
    }
    cout << max(0, (*max_element(a.begin(), a.end()) -
                    *min_element(b.begin(), b.end())))
         << endl;
    t--;
    a.clear();
    b.clear();
  }
}
