#include <bits/stdc++.h>
using namespace std;
int main() {
  int a;
  cin >> a;
  for (int i = 0; i < a; i++) {
    int n;
    cin >> n;
    vector<int> l(n);
    vector<int> r(n);
    for (int j = 0; j < n; j++) {
      cin >> l[j] >> r[j];
    }
    sort(l.begin(), l.end());
    sort(r.begin(), r.end());
    reverse(l.begin(), l.end());
    if (r[0] >= l[0]) {
      cout << 0 << endl;
    } else {
      cout << l[0] - r[0] << endl;
    }
  }
}
