#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<pair<long long, long long>> nums(n);
  long long end = INT_MAX, begin = 0;
  for (long long i = 0; i < n; i++) {
    cin >> nums[i].first >> nums[i].second;
    if (end > nums[i].second) {
      end = nums[i].second;
    }
    if (begin < nums[i].first) {
      begin = nums[i].first;
    }
  }
  if (begin - end <= 0) {
    cout << 0 << endl;
  } else {
    cout << begin - end << endl;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
