#include <bits/stdc++.h>
std::vector<long long> a;
int main() {
  long long t;
  std::cin >> t;
  for (int i = 0; i < t; i++) {
    long long n;
    std::cin >> n;
    std::vector<std::pair<long long, long long>> a(n), b(n);
    for (int i = 0; i < n; i++) {
      std::cin >> a[i].first >> a[i].second;
      b[i].first = a[i].second;
      b[i].second = a[i].first;
    }
    std::sort(a.begin(), a.end());
    std::sort(b.begin(), b.end());
    if (n == 1) {
      std::cout << 0 << '\n';
      continue;
    }
    long long r = a[n - 1].first;
    long long l = b[0].first;
    if (l >= r) {
      std::cout << 0 << '\n';
    } else {
      std::cout << r - l << '\n';
    }
  }
}
