#include <bits/stdc++.h>
using namespace std;
const long long inf = 2e9 + 7;
const double eps = 1e-8;
const long long MOD = 1e9 + 7;
const long long N = 110;
const long long M = 10010;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  for (long long j = 0; j < t; ++j) {
    long long n;
    cin >> n;
    vector<pair<long long, long long> > a(n);
    for (long long i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
    sort(a.begin(), a.end());
    long long l = a[0].second, r = a[n - 1].first;
    for (long long i = 0; i < n; ++i) {
      if (a[i].second < l) l = a[i].second;
      if (a[i].first > r) r = a[i].first;
    }
    long long ans = r - l;
    if (ans < 0) ans = 0;
    cout << ans << "\n";
  }
  return 0;
}
