#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)2e9;
const long long inf = (long long)2e18;
const long double eps = (long double)1e-6;
const long long mod = (long long)1e9 + 7;
const long long MAXN = (long long)5e5 + 1;
const long long MAXC = (long long)6e6;
const long long MAXLOG = 19;
const long long maxlen = (long long)1e5;
const long long asci = (long long)256;
const long long block = 707;
template <class T>
istream &operator>>(istream &in, vector<T> &arr) {
  for (T &cnt : arr) {
    in >> cnt;
  }
  return in;
};
void solve() {
  int n;
  cin >> n;
  int mn = INF, mx = -1;
  for (int i = 0; i < n; ++i) {
    int l, r;
    cin >> l >> r;
    mn = min(mn, r);
    mx = max(l, mx);
  }
  if (mx <= mn)
    cout << "0\n";
  else
    cout << mx - mn << "\n";
}
int main() {
  srand(time(0));
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.precision(30);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
