#include <bits/stdc++.h>
using namespace std;
long long t, n, a, b, c, rm, lm, k = 0;
int main() {
  cin.tie(0);
  cin >> t;
  for (int l = 0; l < t; ++l) {
    cin >> n;
    cin >> lm >> rm;
    for (int i = 0; i < n - 1; ++i) {
      cin >> a >> b;
      lm = max(lm, a);
      rm = min(rm, b);
    }
    cout << max(lm - rm, k) << "\n";
  }
}
