#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int INF = 2e9;
int n;
pair<int, int> a[N];
void solve() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].first >> a[i].second;
  }
  int mnR = INF, mxL = -INF;
  for (int i = 1; i <= n; ++i) {
    mxL = max(mxL, a[i].first);
    mnR = min(mnR, a[i].second);
  }
  cout << max(0, mxL - mnR) << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
