#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    pair<int, int> a[n];
    int mn = INT_MAX, mx = INT_MIN;
    for (int i = 0; i < n; i++) {
      cin >> a[i].first >> a[i].second;
      mx = max(mx, a[i].first);
      mn = min(mn, a[i].second);
    }
    cout << max(0, mx - mn) << endl;
  }
  return 0;
}
