#include <bits/stdc++.h>
using namespace std;
int t, n;
int main() {
  cin >> t;
  while (t--) {
    int l, r, k = 2000000000, m = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> l >> r, k = min(k, r), m = max(m, l);
    cout << max(m - k, 0) << endl;
  }
  return 0;
}
