#include <bits/stdc++.h>
using namespace std;
int n, t;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    int min_ = 1000000005, max_ = 0;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      min_ = min(min_, b);
      max_ = max(max_, a);
    }
    cout << max(0, max_ - min_) << endl;
  }
}
