#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, l = 1e9, r = -1e9;
    cin >> n;
    pair<int, int> tp[n];
    for (int i = 0; i < n; i++) {
      cin >> tp[i].first >> tp[i].second;
      l = min(l, tp[i].second);
      r = max(r, tp[i].first);
    }
    cout << max(r - l, 0) << endl;
  }
}
