#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  long long a, b;
  if (n == 1) {
    cin >> a >> b;
    cout << "0" << endl;
    return;
  }
  vector<pair<int, int> > f;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    f.push_back({a, -1});
    f.push_back({b, 0});
  }
  sort(f.begin(), f.end());
  long long zakr = 0, otkr = 0, l = 0, r = 0;
  for (int i = 0; i < f.size(); i++) {
    if (f[i].second == -1) {
      otkr++;
      if (otkr == n) {
        if (zakr == 0) {
          cout << "0" << endl;
          return;
        } else {
          r = f[i].first;
          break;
        }
      }
      if (zakr == 0) {
        l = f[i].first;
      }
    } else {
      if (zakr == 0) {
        l = f[i].first;
      }
      zakr++;
    }
  }
  cout << r - l << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  while (n--) {
    solve();
  }
}
