#include <bits/stdc++.h>
#pragma optimize("SEX_ON_THE_BEACH")
using namespace std;
const long double pi = 3.1415926;
const int MOD = (int)(1e9) + 7;
const int INF = (int)(1e9) + 1e3;
const long long LINF = (long long)(1e18) + 1e6;
const int MAXN = (int)(1e5);
const int MAXQ = (int)(1e5);
void solve() {
  int n;
  cin >> n;
  vector<int> l(n);
  vector<int> r(n);
  for (int i = 0; i < n; ++i) {
    cin >> l[i] >> r[i];
  }
  sort(l.begin(), l.end());
  sort(r.rbegin(), r.rend());
  cout << max(0, l.back() - r.back()) << '\n';
}
void isolve() {}
void generator() {}
void validator() {}
void stress() {}
signed main() {
  ios_base::sync_with_stdio(false);
  srand(time(nullptr));
  cout.setf(ios::fixed);
  cout.precision(10);
  cout.tie(nullptr);
  cin.tie(nullptr);
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
}
