#include <bits/stdc++.h>
int solve(const std::vector<std::pair<int, int> >& segments);
int main() {
  int testCasesCount;
  std::cin >> testCasesCount;
  std::vector<int> results;
  for (auto i = 0; i < testCasesCount; ++i) {
    int n;
    std::cin >> n;
    std::vector<std::pair<int, int> > segments;
    for (auto j = 0; j < n; ++j) {
      int l, r;
      std::cin >> l >> r;
      segments.push_back({l, r});
    }
    results.push_back(solve(segments));
  }
  for (auto result : results) {
    std::cout << result << std::endl;
  }
  return 0;
}
int solve(const std::vector<std::pair<int, int> >& segments) {
  if (segments.size() < 2) {
    return 0;
  }
  int lmaxPos = 0, rminPos = 0;
  for (auto i = 1; i < segments.size(); ++i) {
    if (segments[i].first > segments[lmaxPos].first) {
      lmaxPos = i;
    }
    if (segments[i].second < segments[rminPos].second) {
      rminPos = i;
    }
  }
  if (segments[lmaxPos].first < segments[rminPos].second) {
    return 0;
  }
  return segments[lmaxPos].first - segments[rminPos].second;
}
