#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 50;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long maxL = 0;
    long long minR = LLONG_MAX;
    while (n--) {
      long long l, r;
      cin >> l >> r;
      maxL = max(l, maxL);
      minR = min(r, minR);
    }
    cout << max(0ll, -minR + maxL) << '\n';
  }
}
