#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
const long long int MX = 10010896;
const long long int lmt = 3164;
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " = " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " = " << arg1 << " ";
  __f(comma + 1, args...);
}
void solve() {
  long long int n;
  cin >> n;
  pair<long long int, long long int> arr[n];
  for (long long int i = (long long int)0; i < (long long int)n; ++i) {
    cin >> arr[i].first >> arr[i].second;
  }
  vector<long long int> left, right;
  for (long long int i = (long long int)0; i < (long long int)n; ++i) {
    left.push_back(arr[i].first);
    right.push_back(arr[i].second);
  }
  sort(left.begin(), left.end());
  sort(right.begin(), right.end());
  long long int ll = left[left.size() - 1];
  long long int rr = right[0];
  cout << max(0LL, (ll - rr)) << "\n";
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
